I am reading about AVL trees in Data structures and algorithms by Mark Allen Wesis
Suppose the node to be rebalanced is X. There are 4 cases that we
might have to fix (two are the mirror images of the other two): An
insertion in the left subtree of the left child of X, An insertion in
the right subtree of the left child of X, An insertion in the left
subtree of the right child of X, or An insertion in the right subtree
of the right child of X.
Balance is restored by tree rotations.
Following are questions i have on above text snippet.
What does author mean by mirror images of other two?
What is symmetric case in single rotation and double rotation?
Thanks
Suppose the node being inserted is I. The book says there are 4 cases. Let's take the one where I is the left child of the left child of X:
X
/ \
? ?
/ \ / \
I ? ? ?
The "mirror" of this is when I is the right child of the right child of X:
X
/ \
? ?
/ \ / \
? ? ? I
The reason this is a "mirror" is that the rotations you have to do for both cases are the same, just with left and right reversed. The same goes for the other two cases where I is the left child of the right child of X and where I is the right child of the left child of X.
As for your second question, the idea is the same. In the symmetric case, (ie the mirror case), you do the same rotations, just with left and right reversed.
Related
For an AVL tree, when deleting a node from the tree that requires restructuring, the book i am reading states that there are certain rules to following to select which nodes to restructure. An example is as such:
44
/ \
17 62
/ \
50 78
/ \ \
48 54 88
This is the AVL tree after a node(child of node 17) has been deleted and the height-balance property has been violated.
The book i read states that it will let z be the first unbalanced position encountered going up from node 17, y be the child of z with the greater height and finally x be the child of y with the greater height. However, if the children of y both have the same height, then x will be the same side as y. In this case, x is 78, y is 62, z is 44.
Now here is the question posed. Why do we select x such that it is the same side as y? Will there be any issues with the AVL tree if i select x to not be the same side as y? I have tried to give myself examples and tried selecting both types of x and, restructure the AVL tree. However, i cannot seem to find any issues that will arise from selecting x as either child. Any help is appreciated to help me solve this.
Interestingly this topic has so many variations of answers in the various web forums. I tried creating all possible AVL tree node deletion scenario and I observed that if the node is deleted from left side of the AVL tree to make tree imbalanced, perform LL or LR (any of possible rotation) based on node availability, and the tree gets balanced.
Viceversa for right node deletion.
Also, need to be sure if the moving node due to rotation has children, the children to be inserted on left or right if they were if left or right side before rotation, respectively.
I need help understanding a specific example of zig-zag and zig-zig rotations in splay trees. I have read about them in a book, and on Wikipedia as well as a few other online resources, and whilst I can understand them when they are applied on simple trees (i.e. trees with very few nodes), I get lost when I see examples of them applied to bigger trees (i.e. trees with more nodes).
In particular, I don't understand at least one of these 2 examples:
Example 1
(this is not the full example but this is the part of it I don't understand)
We can see in (1) that the node which is about to be splayed is the right child of a left child and therefore we have to do a zig-zag. I can therefore understand what happens between (1) and (2), where the node being splayed has now taken the place of its parent. In my understanding, the entire zig-zag operation has taken place there. So the first splay is over, and we still need to splay the node until it is at the root of the tree.
I don't understand what happens between (2) and (3). In (2), the node being splayed is the left child of a left-child, so I expected a zig-zig operation whereby the node being splayed rotates around its grandparent, which is (20, Z). Instead, the slide shows a rotation with its parent, (10, A). Why? What I would have expected there is that our splaying node (8, N) does a zig-zig and thereby takes the places of (20, N) which is its grandparent, and also the root. Why is the parent involved instead?
In the resource where I found this example (my lecturer's slides), a zig-zag rotation is described as "rotate node with its parents, then rotate node with its grand-parent". Is that what happens here? Is that the reason why between (2) and (3), (8, N) rotates with (10, A) instead of (20, Z)?
I am very confused about this description, because then the zig-zig rotation is described as "rotate node with its grand-parent, then rotate node with its parent". However, everytime I have seen an example of zig-zig rotation, there is always only one rotation with the grand-parent of the node and that's it. I have never seen 2 rotations.
Then there is this other example:
In that example, the splay operation involves a zig-zig. As you can see, there is only 1 rotation. Then there is a second splay because the node being accessed is still not in a root position.
What I would have expected here is that:
Either the description of zig-zig and zig-zag provided are correct, and therefore there would have been 2 rotations in the second example: one around the grandparent of the node, then one around its parent.
Or, the description provided are wrong, so then the second example is correct, but the first is not as we rotated the node with its parent and not its grandparent whilst it was in a zig-zig position.
Can you let me know if any of these 2 examples is wrong, and which one? If none of them is wrong, where am I wrong?
Thank you for your help.
PS: As it is obvious that I am a student, I would just like to let you know that I do not ask this question in the context of an assignment, and therefore I am not cheating. I have, however, an exam this coming Monday and I hope to have any misunderstanding clarified before I sit the exam.
Splaying of a node x is sequence of splay steps until x becomes root of the tree. These steps are zig, zag and zig-zag.
For a node x, its parent p and p's parent g, zig-zag operation on node x is defined for a case where p is not the root and x is the right child of p and p is the left child of g: first rotate left x, then rotate right x. Your first example shows exactly that case: zig rotates left x = (8, X) and its parent p = (7, T), then zag rotates right (8, X) and g = (10, A). So, this example shows zig-zag on x, but the splaying itself is not done because x is not root yet (meaning more splaying steps is necessary).
zig-zig is defined for the case when both x and p are right children of their respective parents: first rotate left p, then rotate left x. The second picture of your second example shows the result of zig-zig on x = (40, X), p = (37, P), g = (35, R): first p is rotated left, then x is rotated left. Third picture of the second example shows additional zig operation on x (rotating left x since its parent p is the root) moving to the root and finishing splaying of x.
(All zig, zig-zig and zig-zag operations have their symmetrical counterparts).
Is there a way to do this? In other words, is there a way to know which tree has the smaller/larger height without knowing the height themselves? That way we would know which tree we need to pick the room from. Note that all the elements in the first tree are smaller than all the elements in the second tree.
h1 and h2 are the heights of the trees
the height of the merged new tree needs to be max(h1 , h2) + 1
Remove the least element from T2 and make it the root of a tree with T1 as the left subtree and the rest of T2 as the right. max(h1, h2) is just a convenient bound on the length of T2's left spine. We can actually get an algorithm that runs in time O(min(h1, h2)) by traversing T1's right spine and T2's left spine in a dovetailed fashion and then executing the appropriate mirror image of this algorithm when the greatest of T1/least of T2 is discovered.
I think you have to explicitly find the heights of the trees. Without more specific info, there just isn't anything else you can do. You can find the height of a tree recursively in O(n) time where n is the number of nodes in the tree.
I am reading about splay trees by Robert Sedgwick. Following is text snippet from section
In the root-insertion method, we accomplished our primary objective of bringing the newly inserted node to the root of the tree by using left and right rotations. In this section, we examine how we can modify root insertion such that the rotations balance the tree in a certain sense, as well.
Rather than considering (recursively) the single rotation that brings the newly inserted node to the top of the tree, we consider the two rotations that bring the node from a position as one of the grandchildren of the root up to the top of the tree. First, we perform one rotation to bring the node to be a child of the root. Then, we perform another rotation to bring it to the root. There are two essentially different cases, depending on whether or not the two links from the root to the node being inserted are oriented in the same way. . Splay BSTs are based on the observation that there is an alternative way to proceed when the links from the root to the node being inserted are oriented in the same way: Simply perform two rotations at the root.
Additional information is present at slide 4 at following link
http://www.cs.princeton.edu/courses/archive/spr04/cos226/lectures/balanced.4up.pdf
My question is
What does author mean by orientation here?
Request to given an example for orienations differ and orientation match.
Thanks
Orientation simply means left or right child.
A
/ \
B C
/ \
D E
E is an example of the same orientation case - C is the right child of A and E is again the right child of C. D is an example of differing orientations because D is the left child of C but C is the right child of A.
So you can describe the orientation of all the nodes with respect to the root node A as follows.
B left
C right
D right left
E right right
Suppose you are accessing node n, whose parent is p, and whose grandparent is g. Then the orientation is the same if both n and p are the left or right child of its parent.
So, for example, if p is the right child of g and n is the left child of p, the orientation is not the same. If p is the right child of g and n is the right child of p, the orientation is the same.
I am reading about splay trees in Data structures and algorithms by Mark Allen Wesis
The splaying strategy is similar to the rotation idea, except that we
are a little more selective about how rotations are performed. We will
still rotate bottom up along the access path. Let x be a (nonroot)
node on the access path at which we are rotating. If the parent of x
is the root of the tree, we merely rotate x and the root. This is the
last rotation along the access path. Otherwise, x has both a parent
(p) and a grandparent (g), and there are two cases, plus symmetries,
to consider. The first case is the zig-zag case, Here x is a right
child and p is a left child (or vice versa). If this is the case, we
perform a double rotation, exactly like an AVL double rotation.
Otherwise, we have a zig-zig case: x and p are either both left
children or both right children.
In above text what does author mean by following statement "There are two cases plus symmetries"? two cases are given but what are symmetires here?
Thanks!
I think it's just pretty basic axial symetry:
example for a zig zag case, here are 2 symetric trees:
g
/ \
p d
/\
c x
/ \
a b
g
/ \
d p
/\
x c
/ \
a b
For example, say a case is "When the node in questions is the right child of its parent and the parent is the left child of the grandparent" In this case you do a left rotation and then a right rotation. So the node will come up to the grand parent.
The symmetric part of this case is "When the node in questions is the left child of its parent and the parent is the right child of the grandparent" In this case you do a right rotation and then a left rotation. So the node will come up to the grand parent.
replace left with right and right with left you get the symmetric case.
There are only 3 cases for rotation in a splay tree. Listed it here
You can see the runtime difference in searching with and without splaying.