













Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!














(i) Assuming we want to create a Binary Search Tree (BST) from three nodes A, B and C, this will give 3! ( 6 ) possible trees as shown on the next slide. (ii) Four of the trees have a height of three and two have 2. The higher the height of a tree the higher the number of comparisons needed to locate a node in the worse case (or when the search node is not on the tree) (iii) For the four trees that have a height of 3 (a-d), a single rotation is required or a double rotation (on e and f) can reduce the height of the trees to 2. (iv) Rotating a node in a tree that results in balancing the height of the tree gives what is known as a height balanced tree. (v) The main advantage of an AVL over BST is the fact that in most cases the maximum number of comparisons required in locating a node in AVL is lower than BST. (v) The main disadvantage of AVL over BST is the fact that an additional field is required for each node to store the node’s balance factor, hence more memory.
(a)CBA (b) ABC (c) CAB (d) ACB (e) BAC (f) BCA C B A B C A B C C B A B C A It can be seen from above that the trees (a) to (d) have a height of 3 hence the maximum number of comparisons (height) to locate a node is 3. Trees (e) and (f) have a height of 2 and hence require a maximum of 2 comparisons to locate any node. Since the maximum number of comparisons in (e) and (f) are lower than in (a) to (d), the trees (e) and (f) are said to be a better representation of the nodes for a Binary Search Tree. If we rotate (a) at node C to the right and (b) at node A to the left we obtain the same tree as in (e) or (f) thus reducing the height to 2. For (c), if we apply double rotation, first at A to the left and then at C to the right we obtain the same tree as in (e). Similarly, for (d), if we apply double rotation, first at node B to the right and then to the left at node A we obtain the same tree as (e). B C A (^) A C B
AVL
LL insertion requires single right rotation C B A B A C
LR insertions require double (LR)rotation C B A Left Rotation at A C B A Right Rotation B A C
RL insertions require double (RL)rotation A B C Right Rotation C A B C Left Rotation B A C
Case 2 is as shown below where the tree is unbalanced at the right sub- tree A D B C F E B D F F B D D C E F A B From the above, it can be seen that the tree is unbalanced at RIGHT subtree of node B (B has a balance factor of - 2), hence we need to balance the left subtree of node B, that is nodes B, D and F. This is because the last node to be inserted is E. Since ECA is RR, we rotate to the left. After rotation, B becomes the left child of D. The left child of C before the rotation is C, C then becomes the right child of B after rotation Case 2
Step 1: insert A BF of A is 0 Step 2: insert B BF of A is - 1 and that of B is 0 Step 3 : Insert C BF of A = - 2 B = - 1 C = 0 A A B A B C
Step 6: Insert E BF of A = 0 B = - 2 C = - 2 D = - 1 E = 0 As can be seen from above, the tree is not balanced and hence the need for rotation. Both B and C have BF of - 2, however, in such situations, we rotate the one at the higher level, in this case node C. We have a situation of RR hence the need for left rotation. This therefore gives the tree below A B C D E
AVL BF of A = 0 B = - 1 C = 0 D = 0 E = 0 Thus we have successfully created an AVF tree for ABCDE. Note that the height of the tree is 3. If we had create an ordinary Binary Search Tree the height would have been 5 A B D C E
Searching and Insertion Searching an AVL is not different from searching a Binary Tree Insertion into an AVL is also not different from Binary Search tree, however, once an insertion occurs, you must ensure that the resulting tree is also an AVL
Deletion from an AVL The steps involved are as follows: