You are on page 1of 15

AVL TREE An empty binary tree is an AVL tree.

If T is a non-empty binary tree with TL and TR as its left and right subtrees, then T is an AVL tree iff 1TL and TR are AVL trees 2|HL -HR| <= 1 , where HL is the height of TL and HR is the height of th e right subtree. AVL SEARCH TREE: Is a binary search tree that is also an AVL tree.

a) c)

b)

VALID AVL TREE(S): (a) and (b) VALID AVL SEARCH TREE(S): only (b) a) is not an AVL Search Tree, since it is not a BST. INDEXED AVL SEARCH TREE: Is an indexed BST that is also an AVL tree. Simply counts how many elements exis ts to the left of each node and add 1 to the count Example:

REPRESENTATION of an AVL Tree: Balance factor or Balance Info of a node is defined to be: bf(X) = height of left subtree of X - height of right subtree of X Note: In some text books, this is defined as: bf(X) = height of right subtree of X - height of left subtree of X Given the definition of balance info, here are some examples:

Tree A

Tree B

Hence, bf is either1, 0, or -1, in an AVL Search Tree. INSERTION INTO AVL SEARCH TREE Use the strategy of BST insertion. Find the location and insert. Example: After inserting the node with value 32 to Tree B, it looks like

Observation: 1An AVL Search Tree becomes unbalanced if a bf(x) values is -2 or 2. 2a) A node with bf(x) = 2 was having a bf(x) = 1 before insertion. b) A node with bf(x) = -2 was having a bf value -1 before insertion 3Result of insertion affects the bf of the nodes in the path, where node is inserted. 4Let X be the nearest ancestor of the newly inserted node whose balance f actor is either 2 or -2. The bf value of all nodes on the path from X to the new ly inserted node was 0 prior to the insertion.

Note, if we continue to inserting (to the example given earlier above which is l abeled as Tree A) the values 22 or 28, Node X is the node which has the value 35 .

XL XR h-1 h-2

XR h-2

XL h-1

XR` h-1

XL` h

Let the height of the tree was h before the insertion If bf(X) was 1, then the height of its left subtree XL was h-1 and the height of its right subtree XR was h-2 before the insertion. If after the insertion, bf(X)= 0, insertion must be made in to XR, resulting in XR` (see the middle figure above.) If the insertion would have been made to the left, then the bf(X) = 2 r When such an X node is identified, the imbalance at X can be classified as eithe

o an L (newly inserted node is in the left subtree of X) or R (newly ins erted node is in the right subtree) type imbalance. We can redefine this imbalance classification by determining which grandchild of X is on the path to the newly inserted node. Such a node (grandchild) exists, since the height of the subtree of X that conta ins new node must be at least 2 for the balance factor of X to be 2 or 2. Hence, the imbalance types can be LL: An insertion into the left subtree of the left child of X LR: An insertion into the right subtree of the left child of X RL : An insertion into the left subtree of the right child of X RR: An insertion into the right subtree of the right child of X The following figure represents a LL type imbalance on the left, where A is the node X we have been naming from our earlier discussion, and earlier an insertion is made to the left subtree of B. By a single rotation, such an imbalance can be fixed.

You may view the same figure as

Here is the code for LL Rotation (LL is a single rotation)

void RotateWithLeftChild( BinaryNode<Etype> * &A ) { BinaryNode<Etype> *B = A->Left; A->Left = B->Right; B->Right = A; A=B; } Example:

Example: After inserting 1 to the tree, need LL type rotation, since node with v alue 8 has bf= 2.

LL and RR are symmetric cases. Both require single rotations. Hence, with a single rotation a RR type imbalance can be fixed also. Here is RR

Code for RR Rotation template <class Etype> void RotateWithRightChild( BinaryNode<Etype> * &A ){ BinaryNode<Etype> *B = A->Right; A->Right = B->Left; B->Left = A; A=B; }

LR imbalance (Double Rotation)

Note that, x = bf(B) and y= bf(A) on the right figure If in the middle figure o b=bf(C) =0, then on the right figure x=y= 0 after rotation o b=1 (insertion is done to CL, height of CL is h, and CR is h-1) then x=0 and y =-1 after rotation o b= -1 (insertion is done to CR, height of CL is h-1 and CR is h) then x =1 and y= 0 after rotation

The following figure shows an instance of LR imbalance and transformations need to resolve the problem. Example: After inserting 5 to the tree, results in LR type rotation

The transformation for a LR imbalance can be viewed as a RR rotation followed by a LL rotation, while an RL imbalance can be viewed as an LL rotation followed b y a RR rotation (Prove this). LL and RR are symmetric cases (sometimes they are called single rotations). Code for LR template <class Etype> BinaryNode<Etype> * DoubleRotateWithLeftChild( BinaryNode<Etype> * &A ){ RotateWithRightChild(A->Left); //RR

RotateWithLeftChild(A) ; }

//LL

Code for RL is same, except you first do LL and then RR. Applying the code to the figure given, where A is the node that contains 8,

Example: Constructing AVL tree for values 30, 20,10

The tree is not balanced. Need an LL transformation

Now, insert 11 and 18.

After RR Rotation

Need LR rotation (RR followed by LL) to balance the following tree

After performing

RR

After performing LL rotation

Now, lets insert 12, 15 and 13. After inserting the elements the tree looks like:

RL rotation is needed. First perform an LL (Colored Lines indicate the new conne ctions)

and then RR. The tree is balanced again.

Exercise: Trace the tree by inserting 17 and 40. Summary of the insertion algorithm as follows:

1Use the insertion algorithm developed for BST to insert a node, trace a path from root and insert the node as a leaf. 2During this process, keep track of the most recently seen node with bala nce factor 1 or 1. Let this node be X 3If there is no node X, then make another pass from the root, updating ba lance factors. Terminate after this pass. 4If bf(X) =1 and the new node was inserted to the right subtree of X or i f bf(X) = -1 and the insertion took place in the left subtree, then the new bala nce factor of X is zero. In this case, update the balance factors on the path fr om X to the new node and terminate. 5Classify the imbalance at X and perform the appropriate rotation. Chang e the balance factors as required by the rotation as well as those of nodes on t he path from the new subtree root to the newly inserted node. DELETION FROM AN AVL Search TREE: Deletion or removal is similar to Binary ST deletion (removal). Simply apply the binary search tree deletion algorithm and from the parent of th e deleted node retrace back to the root, by updating the balance factors. If a node is encountered whose balance factor becomes +- 2, a single or double r otation pattern is used to restore the balance. Example: Delete 45. No rotation is needed, update bf of the parent node by retra cing back.

Example: Delete 5. No rotation is needed for rebalancing

Let q be the parent of a node physically deleted. For example, Assume that the node, which contains key value 25, is to be deleted from the tree given in the figure below. o After the node containing 25 is deleted the right child of the parent no de of the deleted node is diverted to the only child of the deleted node which h

as a key value 30. o The parent of the deleted node is the root, so q is the root.

If instead, the node with the data element 15 is to be deleted, the node with the data element 12 uses its spot. o The node previously containing this element, 12, is deleted. That is an exchange happens. Now q is the node that originally contained 15 (i.e., the lef t child of the root). o To update the balance factors of some of the nodes on the path from the root to q, we retrace this path from q towards the root. o Hence, upon deletion, if deletion took place in the left subtree of q, b f(q) decreases by 1 (-1), and if it took place from the right subtree, bf(q) inc reases by 1 (+1). Observations: 1If the new bf(q) = 0, its height has decreased by 1. Hence, we need to c hange the balance factors of the parent and ancestors of q (if any). 2If the new balance factor of q is either 1 or 1, its height is the same a s before the deletion, hence no need to update the balance factors of qs ancestor s. 3If the new balance factor of q is either 2 or 2, the tree is unbalanced a t q. o From Observations 1 and 3, it is clear that, balance factor changes may propagate up to the tree along the path from q to the root. o It is possible for some nodes on this path to have a bf 2 or 2. o Let X be the first such node along such a path. o To restore the balance at the node X, we classify the type of imbalance. o The imbalance is type L if the deletion takes place on Xs left subtree, o therwise it is type R. o If bf(X) = 2 after deletion, it must have been 1 before. o So, X has a left subtree with root Y. o A type R imbalance is subclassified into types o R0, R1 and R-1 depending on the value of bf(Y) (bf(Y) is 0, -1, o 1). o The type R-1 : Refers to the case when deletion took place from right su btree of X and bf(X) = -1. o Type L imbalance is also subclassified as o L0, L1, and L-1. R0 imbalance (bf(Y)= 0):

YL

XR

XR

h h YL h YR h YL YR h h-1 h

h-1

a) Before Deletion ion

h b) After deleting from XR

o Note that the height of subtree was h+2 ( add 1 to bf(XR) because of node containing X, add 1 to bf(YL) and bf(YR) because of Y) before the deletion and is h+2 after deletion. Hence, balance factors of the remaining nodes do not need to be updated after the deletion.

R1 imbalance (bf(Y) =1 ):

YL h h YL h YR h-1 YL YR

a) Before Deletion tation R-1 imbalance(bf(Y)= -1):

b) After deleting from XR

YL h-1 ZL

YL

ZR

XR

h-1 h-1 ZR ZL

XR h

YR

XR

h -1

h h-1

XR

XR

XR

YR

XR

c) After R0 rotat

h-1

h-1 c) After R1 ro

h-1 YL h-1 ZR ZL

a) Before Deletion ation

b) After deleting from XR

c) After R-1 rot

If b= 0 bf(X) = bf(Y) =0 after rotation If b=1 bf(X)= -1 and bf(Y)=0 after rotation If b= -1 bf(X) = 0 and bf(Y) =1 after rotation

The balance factors of X and Y following the rotation depend on the balance fact or b of the right child of Y. This rotation leaves behind a subtree of height h+ 1, while the subtree prior to the deletion was h+2. So we need to trace back on the path to update the balance information. o o Note that: LL and R1, LR and R-1 rotations are identical. LL and R0 rotations differ only in the final balance factors of X and Y.

Example:

Another Example:

Exercise:

1Write the routines that are necessary for R0, R1, R-1, L0, L1, L-1 for d eletion. 2Develop a C++ class AVLtree that includes the binary search tree functio ns Search, Insert, Delete. 3Draw the figures analogous to the figures given for R0, R1, R-1 for L0, L1, and L-1 imbalances. 4In this project, you are to compare the running time for insertion, dele tion and search procedures of unbalanced binary search trees to the of AVL-trees . Implement: 1. A procedure to generate a random permutation of the integers 1,...,n 2. A procedure to select n/2 elements randomly from the generated list. 3. A procedure to insert n values to both trees (keep a separate timestamp for inserting into BST and AVL-trees). 4. A procedure to delete n/2 values from both trees (keep a separate times tamp again for each case). 5. A procedure to draw resulting trees after insertion and for both trees. 6. A main function as follows o Read in n, the number of nodes. If needed, you may assume that n is not too large (say, n<100), but you must che ck the input for this condition. o Read in N, the number of experiments. o Perform N times: Compute a permutation of the numbers 1,...,n. Select n/2 elements from the list given above. These will be used for deletion l ater. Insert the n numbers in a binary search tree in this order. Measure the time use d. Insert the n numbers in an AVL-tree in this order. Measure the time used. Search the n/2 numbers from the BST. Measure the time used. Search the n/2 numbers from the AVL-tree. Measure the time used. Delete the n/2 numbers from the binary search. Measure the time used. Delete the n/2 numbers from an AVL-tree. Measure the time used. o Print out the times you have computed for Insertion, Search and Deletion for each tree. 5In this project, you are to compare the heights of unbalanced binary sea rch trees to the height of AVL-trees and to the lower bound on the height. Implement: 7. Insertion in a binary search tree 8. Insertion in an AVL-tree 9. A procedure to generate a random permutation of the integers 1,...,n 10. A procedure to compute the height of a binary search tree 11. A procedure to draw a binary search tree in inorder with indentation 12. A main function as follows o Read in n, the number of nodes. If needed, you may assume that n is not too large (say, n<100), but you must che ck the input for this condition. o Read in N, the number of experiments. o Perform N times: Compute a permutation of the numbers 1,...,n. Insert the numbers in a binary search tree in this order. Measure the height of the resulting tree. Insert the numbers in an AVL-tree in this order. Measure the height of the resul ting tree. o Print out the average height of the computed binary search trees divided by log(n). o Print out the average height of the computed AVL-trees divided by log(n) . o If N=1, print out the permutation, the binary search tree and the AVL-tr ee.

You might also like