Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 AVL Trees. 2 Consider a situation when data elements are inserted in a BST in sorted order: 1, 2, 3, … BST becomes a degenerate tree. Search operation.

Similar presentations


Presentation on theme: "1 AVL Trees. 2 Consider a situation when data elements are inserted in a BST in sorted order: 1, 2, 3, … BST becomes a degenerate tree. Search operation."— Presentation transcript:

1 1 AVL Trees

2 2 Consider a situation when data elements are inserted in a BST in sorted order: 1, 2, 3, … BST becomes a degenerate tree. Search operation FindKey takes O(n), which is as inefficient as in a list. 1 2 3 n

3 3 AVL Trees It is possible that after a number of insert and delete operations a binary tree may become imbalanced and increase in height. Can we insert and delete elements from BST so that its height is guaranteed to be O(log n)?  Yes, AVL Tree ensures this. Named after its two inventors: Adelson- Velski and Landis.

4 4 Imbalanced Tree 70 60 90 30 20 110 80 70 60 903010 20 110 An Imbalanced Tree A Balanced Tree

5 5 AVL Tree: Definition Height-balanced tree: A binary tree is a height-balanced-p-tree if for each node in the tree, the difference in height of its two subtrees is at the most p. AVL tree is a BST that is height-balanced- 1-tree.

6 6 AVL Trees: Examples

7 7 AVL Trees 1 2 3 4 5 Inserting 1, 2, 3, 4 and 5 2 3 41 5 BST after insertions AVL Tree after insertions

8 8 ADT AVL Tree Elements: The elements are nodes, each node contains the following data type: Type Structure: Same as for the BST; in addition the height difference of the two subtrees of any node is at the most one. Domain: the number of nodes in a AVL is bounded; type AVLTree

9 9 ADT AVL Tree Operations: 1.Method FindKey (int tkey, boolean found). 2.Method Insert (int k, Type e, boolean inserted). 3.Method Remove_Key (int tkey, boolean deleted) 4.Method Update(Type e)

10 10 ADT AVL Tree 5.Method Traverse (Order ord) 6.Method DeleteSub ( ) 7.Method Retrieve (Type e) 8.Method Empty (boolean empty). 9.Method Full (boolean full)

11 11 ADT AVL Tree Representation: public class AVLNode // AVL Tree Node { private: int key Type data; Balance bal; //Balance is enum +1, 0, -1 AVLNode *left, *right; public: AVLNode(int, Type);// constructors };

12 12 AVL Tree: Insert Step 1: A node is first inserted into the tree as in a BST. There is always a unique path from the root to the new node called the search path. Step 2: Nodes in the search path are examined to see if there is a pivot node. Three cases arise. A pivot node is a node closest to the new node on the search path, whose balance is either –1 or +1.

13 13 AVL Tree: Insert Case 1: There is no pivot node. No adjustment required. Case 2: The pivot node exists and the subtree to which the new node is added has smaller height. No adjustment required. Case 3: The pivot node exists and the subtree to which the new node is added has the larger height. Adjustment required.

14 14 Insert: Case 1 20 3010 0 0 0 Insert 40 +1 20 3010 +1 0 40 0 30 5020 0 60 0 4010 0 0 0 30 5020 +1 60 4010 +1 0 0 55 0 Insert 55 No Pivot node

15 15 Insert: Case 2 +1 20 30 10 +1 0 40 0 0 20 3010 +1 40 0 30 5 0 +1 70 30 5020 +1 60 +1 4010 0 0 0 70 30 5020 0 60 +1 4010 0 +1 45 0 0 +1 Insert 5 Pivot Node Insert 45 New node added to the shorter subtrees of the Pivot.

16 16 Insert: Case 3 5 10 60 8040 +1 100 0 50 20 0 30 00 0 Pivot Node Insert 5 AVL Tree is no more an AVL Tree after insertion.

17 17 Insert: Case 3 When after an insertion or a deletion an AVL tree becomes imbalanced, adjustments must be made to the tree to change it back into an AVL tree. These adjustments are called rotations. Rotations are either single or double rotations. For Case 3 there are 4 sub-cases (2+2)

18 18 Insert: Case3 (Sub-Case 1) Remainder of the tree A B T3 T1 New Node T2 Pivot Remainder of the tree B A T3 T1 New Node T2 Single Rotation hh h

19 19 Insert: Case 3 (Sub-Case 2) Remainder of the tree B A T3 T1 New Node T2 Single Rotation h h h Remainder of the tree A B T3 T1 New Node T2 Pivot

20 20 Insert: Case 3 (Sub-Case 3) One of these is a new node Remainder of the tree A B T4 T1 Pivot C T2 T3 h h-1 h h Remainder of the tree A C T4T1 Pivot B T2T3 h-1 h Double Rotation

21 21 Insert: Case 3 (Sub-Case 4) One of these is a new node Remainder of the tree A B T4 T1 Pivot C T2 T3 h h-1 h h Remainder of the tree A C T4T1 Pivot B T2T3 h-1 h Double Rotation

22 22 AVL Tree: Delete Step 1: Delete the node as in BSTs. Leaf or node with one child, will always be deleted. Step 2: For each node on the path from the root to deleted node, check if the node has become imbalanced; if yes perform rotation operations otherwise update balance factors and exit.  Three cases can arise for each node p, in the path.

23 23 AVL Tree: Delete Step 2 (contd.): Case 1: Node p has balance factor 0. No rotation needed. Case 2: Node p has balance factor of +1 or –1 and a node was deleted from the taller sub-trees. No rotation needed. Case 3: Node p has balance factor of +1 or –1 and a node was deleted from the shorter sub-trees. Rotation needed. Eight sub-cases. (4 + 4)

24 24 Delete: Case 1 Remainder of the tree hh-1 Node to be deleted. 0 p Remainder of the tree hh-1 +1 p

25 25 Delete: Case 2 Remainder of the tree hh Node to be deleted. p Remainder of the tree hh 0 p

26 26 Delete: Case 3 (Sub-Case 1) Single Rotation h-1 h Remainder of the tree C B T1 T3 Deleted Node T2 p +1 Remainder of the tree B C T1 T3 T2 0 0 h h-1

27 27 Delete: Case 3 (Sub-Case 2) Single Rotation h-1 hh Remainder of the tree C B T1 T3 Deleted Node T2 p 0 +1 Remainder of the tree B C T1 T3 T2 +1 h h h-1

28 28 Delete: Case 3 (Sub-Case 3) Double Rotation Deleted Node Remainder of the tree A B T1 T4 p C T3 T2 h-1 h-2h-1 +1 h-1 Remainder of the tree A C T1T4 B T3T2 h-2h-1 0 0+1

29 29 Delete: Case 3 (Sub-Case 4) Double Rotation Deleted Node Remainder of the tree A B T1 T4 p C T3 T2 h-1 h-2 h-1 +1 h-1 Remainder of the tree A C T1T4 B T3T2 h-1h-2h-1 0

30 30 Delete: Case 3 (Other Sub-Cases) Sub-Case 5: mirror image of Sub-Case 1. Sub-Case 6: mirror image of Sub-Case 2. Sub-Case 7: mirror image of Sub-Case 3. Sub-Case 8: mirror image of Sub-Case 4.

31 31 Deletion: Example p m njc e s hdb a g f i k u o r tl +1 0 0 0 00 +1 0 0 0 Delete p

32 32 Deletion: Example o m njc e s hdb a g f i k ur tl +1 0 0 0 00 +1 +2 0 +1 0 0 Delete p Sub-Case 1 Single Rotation

33 33 Deletion: Example -2 s m ojc e u hdb a g f i krtn l +1 0 0 0 00 +1 0 0 0 00 Sub Case 8 Double Rotation

34 34 Deletion: Example 0 m j khc e s gdb a f ilou 0 0 0 0 0 +1 0 0 0 0 After Double Rotation rnt 0 0


Download ppt "1 AVL Trees. 2 Consider a situation when data elements are inserted in a BST in sorted order: 1, 2, 3, … BST becomes a degenerate tree. Search operation."

Similar presentations


Ads by Google