AVL Tree Deletion Rules - data-structures

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.

Related

Splay trees: how do the zig-zig and zig-zag rotations work, in details?

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).

AVL Tree height as a function of nodes

I am trying to find the way to find out the height of the AVL tree as a function of his nodes.
I want to know if it is possible to make an AVL Tree at the height of 4 with exactly 11 nodes. I know that the upper bound of height of an AVL tree which is approximately 1.44*logn. So if I have 11 nodes it is actually 4.32. And yet, I am trying to built one with height 4 for at least 2 hours and fail to do so every time.
Build full binary tree of height 4 with 15 nodes.
Remove ANY four nodes from the last level. Now it is valid AVL tree (the heights of the two child subtrees of any node differ by at most one). Note that it's impossible to remove a node from 3rd level (together with childs, of course) and preserve AVL balance criteria.
One variant (from wiki):

priority generation in treap data structure

I'm studying the treap data structure. When inserting node, treap radomly generate node's priority. But what if 69 node's generated priority is 13 in the picture above?
Parent's priority must higher than child's priority. Do treap's binary tree attribute collide with heap attribute?
I want to know. Thanks.
Assuming you have treap from your picture without 69 node and want to add (69, 13) node:
1. Split existing treap to 2 treaps L and R by key 69 (here all old treap will be L)
2. Create treap M with single node (69, 13)
3. Merge M with L, then result with R
For this case node (69, 13) becomes a new root, and old treap will be it's left child.
If node 69 had priority 13, then it would be the root of the tree and node 31 would be its left child. The descendants of node 31 would be as in your diagram, except of course for node 69.
It's always possible to arrange a treap so that it respects both the heap and the binary search properties. In fact, in the absence of equal values or equal priorities, there is only one possible arrangement.
The random assignment of priorities makes it likely that the treap will be reasonably balanced. It might not be perfectly balanced, but on the positive side building the treap is fast and uncomplicated.

Regarding splay trees

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.

Regarding AVL rotations

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.

Resources