how should I rotate this tree to change it into balanced AVL tree? - binary-search-tree

How could I change this linear tree to balanced tree in single traversal?
1
\
2
\
3
\
4
\
5
\
6
How many rotations should I do to change this into AVL tree?.

Create a new empty AVL tree and insert each of the elements of your tree into this. Follow the example on wikipedia for more information: https://en.wikipedia.org/wiki/AVL_tree?wprov=sfti1

At least 3 rotations are needed.
Because the root has a bad balance 5, rotate the root 2 times.
Go up to the second floor.
4 has a bad balance 2, so rotate it 1 time.

Related

The definition of black-height in Red-Black tree

I am reading CLRS page 309, and I am confused by the definition of black-height of RBT.
The definition in in this book is
the number of black nodes on any simple path from, but not including,
a node x down to a leaf
I also referred to wikipedia, black-height is defined as
the uniform number of black nodes in all paths from root to the leaves
and geeksforgeeks.org
Black height is number of black nodes on a path from a node to a leaf.
Leaf nodes are also counted black nodes.
But none of them give examples, especially no edge cases.
Can you explain it with the following cases?
What is the black-height of a RBT:
nil
B-(nil nil)
B-R-B-(nil nil)
\ \
\ B-(nil nil)
B-(nil nil)
By the definition of CLRS, I calculate the black-height of root is
0
1
2
(In my opinion, am I right?)
And in CLRS lemma 13.1's proof, height is also used. What is the height of the above three trees?
0
1
2
(In my opinion, am I right?)
And, what is the black-height of a tree?
1
2
3
(not sure, I doubt the wikipedia's definition is different from the definition from CLRS)
And also, Exercise 13.1-1 uses this definition.

neo4j breadth first traversal memory issue

I have a graph with million nodes and 3 million edges loaded into Neo4j. It crashes while doing a breadth first traversal over it complaining of insufficient memory on a 8 GB machine. Each node label string has an average length of 40 characters.
What kind of internal representation does Neo4j's uses which requires so much memory, esp. for traversal? Given that Neo4j is able to represent the entire graph, why does it fail while trying to maintain the set of visited nodes required for breadth first traversal.
As per my understanding, a graph representation of the above graph in an adjacency list format should be in MBs.
Calculation assuming 64-bit representation of the node and edge
3 million edges * 8 bytes * 3 = 72 M (node link node)
1 million nodes * (40 + 8 + 8 bytes) = 56 M ( 64-bit hash links to node string label )
You might have 8 GB available, but are you configuring Neo4j to allow it to use that space? Can you see how much it's taking up when it's working?
Here are some resources:
http://neo4j.com/developer/guide-performance-tuning/
http://neo4j.com/docs/stable/server-performance.html
http://neo4j.com/docs/stable/configuration-settings.html#config_neostore.nodestore.db.mapped_memory
http://neo4j.com/developer/guide-sizing-and-hardware-calculator/
bingo #brian-underwood! you are right.
I hadn't configured Neo4J to use more memory.
Since the issue was related to nodes only, following is what I modified
neostore.nodestore.db.mapped_memory=256M # increased
neostore.relationshipstore.db.mapped_memory=3G # unchanged
neostore.propertystore.db.mapped_memory=256M # increased
neostore.propertystore.db.strings.mapped_memory=200M # unchanged
neostore.propertystore.db.arrays.mapped_memory=200M # unchanged
Also enabled, auto indexing for nodes and their keys
node_auto_indexing=true
node_keys_indexable=key_name

How to model this geometry in ABAQUS CAE? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I am modeling a composite cube block with spherical particle and spherical interphase around particle. I can simplify two spheres to two cubes. I am trying to model particle inclusion and interphase in a composite material. I am taking matrix as a cube and particle as a small cube at the center of matrix cube. Interphase layer will be another cube at the center of matrix cube but bit bigger than particle cube. Matrix, interphase, matrix all have different properties. But problem is I do not know how to model a cube inside a cube.
When I assembled three parts, they can get 'separated' during simulation. I cannot model all 3 cubes as a single part. If I do boolean, the innermost geometry gets deleted. Importantly, I also want to make different sets for different parts.
Any suggestions on how to model this?
You don't say what kind of elements you plan to use.
If they're 8 node linear bricks or 20 node quadratic bricks it's an "easy" problem: just create cubic regions for each different material and create your mesh.
I'm guessing that the interphase/interface layer is "thin". You'll have some aspect ratio/transtion problems to sort in the matrix and inclusion models.
This can also be done with a spherical inclusion with a shell interface/interphase between inclusion and matrix, but it'd be easier with tetrahedral elements and an automatic octree meshing solution.
You can exploit planes of symmetry: three in this case. Symmetry/antisymmetry boundary conditions can help you combine different loading methods.
You should not be using boolean operations to create this. Just create a cube with regions for each material. It's straightforward.
Can't show in 3D, but 2D will give you a hint:
+--------+---+---------+
| 1 | 2 | 3 |
| | | |
+--------+---+---------+
| 4 | 5 | 6 |
+--------+---+---------+
| 7 | 8 | 9 |
+--------+-------------+
Inclusion: region 7
Interface: regions 4, 5, and 8
Matrix : regions 1, 2, 3, 6, and 9.
Set material properties in each region appropriately.
Add boundary conditions to the left vertical and bottom horizontal surfaces. Loadings are applied to right vertical and top horizontal surfaces.
Now you should see what I'm getting at: the interface regions are probably thin, and their material less stiff than the inclusion or matrix. You'll have very small elements in that area in order to maintain good aspect ratios, which means lots of matrix and inclusion elements.
I'd recommend doing a simpler case in 2D, like the one I have here, to get a sense for the behavior before you try 3D. You'll be able to turn it around quickly and even check convergence before you tackle the bigger problem.

Continuous Attribute - Distribution in Naive Bayes Algorithm

I am trying to implement Naive Bayes Algorithm - by writing my own code in MATLAB. I was confused what distribution to choose for one of the continuous attributes. It has values as follows:
MovieAge :
1
2
3
4
..
10
1
11
2
12
1
3
13
2
1
4
14
3
2
5
15
4
3
6
16
5
4
....
32
9
3
15
Please let me know which distribution to use for such data? and in my test set, this attribute will contain values (some times) that are not included in training data. how to handle this problem? Thanks
15
Like #Ben's answer, starting with Histogram sounds good.
I take your input, and the histogram looks like below:
Save your data into a text file called histdata, one line per value:
Python code used to generate the plot:
import matplotlib.pyplot as plt
data = []
for line in file('./histdata'):
data.append(int(line))
plt.hist(data, bins=10)
plt.xlabel('Movie Age')
plt.ylabel('Counts')
plt.show()
Assuming this variable takes integer values, rather than being continuous (based on the example), the simplest method is a histogram-type approach: the probability of some value is the fraction of times it occurs in the training data. Consider a final bin for all values above some number (maybe 20 or so based on your example). If you have problems with zero counts, add one to all of them (can be seen as a Dirichlet prior if you're that way inclined).
As for a parametric form, if you prefer one, the Poisson distribution is a possibility. A qq plot, or even a goodness of fit test, will suggest how appropriate this is in your case, but I suspect you're going to be better with the histogram based method.

how to tesselate bezier triangles?

My concern are quadratic bezier triangles which I'm trying to tesselate for rendering them.
I've managed to implement this by subdividing the triangle recursively like described in a wikipedia page. Though I'd like to get more precision to subdivision. The problem is that I'll either get too few subdivisions or too many because the amount of surfaces doubles on every iteration of that algorithm.
In particular I would need an adaptive tesselation algorithm that allows me to define the amount of segments at the edges. I'm not sure whether I can get that though so I'd also like to hear about uniform tesselation techniques.
Hardest trouble I have trouble with calculating normals for a point in bezier surface, which I'm not sure whether I need, but been trying to solve out.
Adaptive tesselation. There are many algorithms to this. But here's one:
def line_angle((x0,y0),(x1,y1)):
return atan2(y1-y0,x1-x0)
def adaptive_bezier(p0,p1,p2,lev=32):
p01 = midpoint(p0,p1)
p12 = midpoint(p1,p2)
m = midpoint(p01, p12)
da = abs(line_angle(p0,p1) - line_angle(p1,p2))
if da <= max_tolerance or lev <= 0:
yield m
else:
for p in adaptive_bezier(p0,p01,m,lev-1): yield p
for p in adaptive_bezier(m,p12,p2,lev-1): yield p
For tesselating triangles this way there are complications to the matter. You need to drive the adaptive tesselator algorithm according to the angles of the edge beziers. There's three unique ways how your triangle can split when tesselating.
2 edges one edge 3 edges
-------- --------- --------
\ ...// \ | / \ / \ /
\/___/ \ | / \____/
\ / \ | / \ /
\/ \|/ \/
Define tesselation results for these patterns and you're well off. Only the tesselation with one edge is described in wikipedia article.
Two other tesselation results can be obtained by studying the case of one edge split.
"2 edges" can be obtained straight out by splitting first one edge and then another.
"3 edges" is a bit more work to find out. But you can see the "2 edges" -case brings you a mid-edge. In the case of quadratic bezier triangle it is an averaged sum of diamond appearing there:
-------- /\
\ / / \
\____/ -____-
\ / \ /
\/ \/