Docsity

height balance trees(AVL), Lecture notes of Data Structures and Algorithms

Toggle sidebar
bg1
Height Balance Trees (AVL)
(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.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15

Partial preview of the text

Download height balance trees(AVL) and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!

Height Balance Trees (AVL)

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

The six (6) possible Binary search trees from A, B and C

(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

  • For each sub-tree, the difference in height between its left and right subtrees is known as its Height Balance Factor (HBF). An HBF can be either
    • 1, 0 or 1 and is calculated as follows: HBF= Height of left Subtree – Height of right subtree Fig 1. E A B D F (^) G C

AVL

  • A 4 - 2= From fig.1, the height balance factor of the nodes are
  • B 2 - 3=-
  • C 0 - 0=
  • D 0 - 0=
  • E 1 - 1=
  • F 0 - 0=
  • G 0 - 0=

How to perform a rotation

  • Let us look at the 4 basic trees that we may have to rotate
  • The 4 possible trees that require rotations are as follows:
    • LL if the insertion took place along the left-left traversal This requires a single rotation to the right
  • RR if the insertion took place along the right-right traversal This requires a single rotation to the left
  • LR if the insertion is first to the left and then right This requires double rotation, first to the left and then to the right
  • RL if the insertion is first to the right and then right This requires double rotation, first to the right and then to the left
  • The next flow slides shows how each rotation is carried out

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

As an example, let us create or build an AVL for the

nodes ABCDE

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

AVL

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:

  • Search for the node to be deleted
  • Delete the node as you would have done with a Binary tree
  • The deletion can cause the tree to lose its AVL property. If that happens, ensure that the AVL property is maintained. Note that the processes of rebalancing the tree is the same as inserting into an AVL