AVL Tree PDF
AVL Tree PDF
An AVL Tree is a Height-Balanced Tree operations are directly proportional to height, we want to
Since height in balanced tree is closed to log n, it is c log n,
Binary Search Tree. (bBST) reduce height as far as possible for sake of efficiency
therefore all operations in a balanced BST, that had O(h) can
What is an AVL Tree? now be O( log n)
1. int height (i.e. number of edges from this vertex to deepest When there is a node that has a |bf| > 1, it needs to be rotated 10/20/30 becomes 10/20\30. (10 -> 20 -> 30)
leaf) // Why? To calculate balance factor for each vertex after becomes (10 -> 20 <- 30)
a Dynamic Operation) Implementation:
Height is -1 if tree is empty (i.e. null Vertex)
AVL Tree How to visualize? Put a nail below root's node (in this case 30)
height of current vertex x = max(x.left.height, x.right.height) + and imagine you are pulling the entire string down the right
The 4 possible cases of Rotation:
1 side of the nail until root of left subtree (20 in this case) goes
to top of the nail.
Right Left Rotation:
2. int size (i.e. total number of vertices in the subtree of this Left Right Rotation:
when bfactor(x) = - 2 and bfactor(x.right) = 1
vertex + 1) // Why? for Select Operation (when bfactor(x) = +2 and bfactor(x.left) = -1
Need to invoke rightRotate(x.right)
x.size = 0 (if x is an empty tree, i.e. x is null) next invoke leftRotate(x); Need to invoke leftRotate(x.left);
x.size = x.left.size + x.right.size + 1 next invoke rightRotate(x);
30 right child of 10, 20 left child of 30.
3. int key 10 left child of 30. 20 right child of 10.
Two-Step Rotation:
4. AVLTreeVertex parent, left, right Two-Step Rotation:
1. Fix nail at right child of root node and rotate such that left child
1. insert(int v) 1. First, fix nail at left child of root node and rotate such that right
of right subtree now becomes right child of root. (Now you
have a right-right situation) child of left subtree now becomes the left child of root. (Now
// need to update height and size 2. Then, do the same rotation as done in RR Rotation. you have a Left-Left Situation)
refer to attributes for formula to calc new height and size Note that this is still considered as one rotation, although it 2. Then, do the same rotation as done in LL Rotation.
has 2 steps. Note that this is still considered as one rotation, although it
//After updating height, calculate new balance factor of each has 2 steps.
vertex up to root. Once we have a vertex with a new balance Methods/ Operations:
factor of + 2 or - 2, we need to rotate immediately. Cannot defer. (Everything O(log n) except ListSorted ( -> O(n)) -> Left Rotation about P -> Right Right Rotation:
// Parent of P now becomes Parent of Q (when bfactor(x) = -2 and bfactor(x.right) = -1 or 0)
// triggers one of the four possible rotations AT MOST ONCE Only need to invoke leftRotate(x);
(from insertion point to root) Q
P
2. delete(int v) <- Right Rotation about Q <- 10\20\30 becomes 10/20\30
// Parent of Q now become Parent of P
// need to update height and size How to visualize? Put a nail below root's node (10 in this case)
refer to attributes for formula to calc new height and size and imagine you are pulling the entire string down the left side
of the nail until root of rightsubtree (20 in this case) goes to top
//After updating height, calculate new balance factor of each of the nail.
vertex up to root. Once we have a vertex with a new balance P Right Subtree of Q
factor of + 2 or - 2, we need to rotate immediately. Cannot defer.
Left Subtree of P Q
Pseudocode
a. AVLTreeVertex w = T.right;
b. w.parent = T.parent;
c. T.parent = w;
d. T.right = w.left;
5. Query Operations inherited from BST Class
e. if(w.left != null) w.left.parent = T;
f. w.left = T;
g. //Update height and size of T and w
search(v)
h. return w;
findMax()
findMin()
4. AVLTreeVertex rotateRight (AVLTreeVertex T) ListSorted()
Pseudocode Successor(v)
a. AVLTreeVertex w = T.left Predecessor(v)
b. w.parent = T.parent
c. T.parent = w
d. T.left = w.right 6. Rank (node, v)
e. (if w.right != null) w.right.parent = T //returns the number of items smaller than v + 1
f. w.right = T // rank operation involves the size operation
g. //Update height and size of T and w Pseudocode
h. return w
if(node.key == v ) return node.left.size+ 1
else if(node.key > v) return rank(node.left, v)
else return 1 + node.left.size + rank(node.right, v) // need to add
node.left.size + 1 instead of node.size to avoid counting right
child
Psuedocode:
int q = node.left.size;
if( q + 1 == k) return node.key; // this node has rank k
else if (q + 1 > k) return select(node.left, k) // this node has rank
> k)
else return select(node.right, k - q - 1) // this node has rank < k,
// so we are going to the right subtree but need to reduce the
rank in the tally since rank of something in right subtree = (that
rank + size of left subtree + 1))