Consider the rotations in the insertion and the removal of AVL trees. Let w be t
ID: 3774361 • Letter: C
Question
Consider the rotations in the insertion and the removal of AVL trees. Let w be the position to insert or to remove one node. Let z be the first unbalanced node along the path from w to the root. Let y be z’s child with larger height and x be y’s child with larger height. Consider the case where the inorder order of x, y, z is y, x, z (the double-rotation case). Let h0, h1 be the height function of nodes before and after the update respectively. Prove the following:a.) Insertion: prove that x, y, z are balanced after the update. Moreover, prove that h1(x) = h0(z).
b.) Removal: prove that x, y, z are balanced after the update. HINT: please review insertion and removal operations of AVL trees and the note on the correctness of the single-rotation case first. Note the difference between insertion and removal and carefully argue about the properties of h0, h1 for each case.
/*IF YOU WRITE CODE, PLEASE PROVIDE COMMENTS*/ I need to understand Consider the rotations in the insertion and the removal of AVL trees. Let w be the position to insert or to remove one node. Let z be the first unbalanced node along the path from w to the root. Let y be z’s child with larger height and x be y’s child with larger height. Consider the case where the inorder order of x, y, z is y, x, z (the double-rotation case). Let h0, h1 be the height function of nodes before and after the update respectively. Prove the following:
a.) Insertion: prove that x, y, z are balanced after the update. Moreover, prove that h1(x) = h0(z).
b.) Removal: prove that x, y, z are balanced after the update. HINT: please review insertion and removal operations of AVL trees and the note on the correctness of the single-rotation case first. Note the difference between insertion and removal and carefully argue about the properties of h0, h1 for each case.
/*IF YOU WRITE CODE, PLEASE PROVIDE COMMENTS*/ I need to understand Consider the rotations in the insertion and the removal of AVL trees. Let w be the position to insert or to remove one node. Let z be the first unbalanced node along the path from w to the root. Let y be z’s child with larger height and x be y’s child with larger height. Consider the case where the inorder order of x, y, z is y, x, z (the double-rotation case). Let h0, h1 be the height function of nodes before and after the update respectively. Prove the following:
a.) Insertion: prove that x, y, z are balanced after the update. Moreover, prove that h1(x) = h0(z).
b.) Removal: prove that x, y, z are balanced after the update. HINT: please review insertion and removal operations of AVL trees and the note on the correctness of the single-rotation case first. Note the difference between insertion and removal and carefully argue about the properties of h0, h1 for each case.
/*IF YOU WRITE CODE, PLEASE PROVIDE COMMENTS*/ I need to understand
Explanation / Answer
Let the newly inserted node be w.
1) Perform stand BST insert for w.
2) Strating from w, travel up % find the 1st unbalanced node. let z be the 1st unbalanced node, y be the child of z that comes on the path from w to z and x be the grandchild of z that comes on the path from w to z.
3) Re-balance the tree by performing appropriaye rotations on the subtree rooted with z. there can be four possible cases that needs to be handled as x,y, and z can be arranged in 4 ways.
a) y is the left child of z and x is the left child of y (Left Left Case)
let T1,T2,T3 & T4 are subtrees.
z y
/ right rotate (z) /
y T4 x z
/ / /
T1 T2 T1 T2 T3 T4
b) y is left child of z and x is right child of y (Left Right Case) :
z z x
/ / /
y T4 Left Rotate (y) x T4 Right Rotate (z) y z
/ / / /
T1 x y T3 T1 T2 T3 T4
/ /
T2 T3 T1 T2
c) y is right child of z and x is right child of y (Right Right Case) :
z y
/ /
T1 y Left Rotate (z) z x
/ / /
T2 x T1 T2 T3 T4
/
T3 T4
d) y is right child of z and z is the left child of y (Right Left Case) .
z z x
/ / /
T1 y T1 x z y
/ Right Rotate (y) / Left Rotate (z) / /
x T4 T2 y T1 T2 T3 T4
/ /
T2 T3 T3 T4