Presentation is loading. Please wait.

Presentation is loading. Please wait.

AVL Search Trees Inserting in an AVL tree Insertion implementation Deleting from an AVL tree.

Similar presentations


Presentation on theme: "AVL Search Trees Inserting in an AVL tree Insertion implementation Deleting from an AVL tree."— Presentation transcript:

1 AVL Search Trees Inserting in an AVL tree Insertion implementation Deleting from an AVL tree

2 Insertion Insert using a BST insertion algorithm. Rebalance the tree if an imbalance occurs. An imbalance occurs if a node's balance factor changes from -1 to -2 or from+1 to +2. Rebalancing is done at the deepest or lowest unbalanced ancestor of the inserted node. There are three insertion cases: 1.Insertion that does not cause an imbalance. 2.Insertion that does not cause an imbalance. Requires a single rotation to rebalance. 3.Opposite side insertion that causes an imbalance. Requires a double rotation to rebalance.

3 Insertion: case 1 Example: An insertion that does not cause an imbalance. Insert 14

4 Insertion: case 2 Case 2a: The lowest node (with a balance factor of -2) had a taller left-subtree and the insertion was on the left-subtree of its left child. Requires single right rotation to rebalance. Insert 3 right rotation, with node 10 as pivot -2

5 Insertion: case 2 (contd.) Case 2b: The lowest node (with a balance factor of +2) had a taller right-subtree and the insertion was on the right-subtree of its right child. Requires single left rotation to rebalance. Insert 45 left rotation, with node 30 as the pivot +2 +1

6 Insertion: case 3 Case 3a: The lowest node (with a balance factor of -2) had a taller left-subtree and the insertion was on the right-subtree of its left child. Requires a double left-right rotation to rebalance. Insert 7 left rotation, with node 5 as the pivot right rotation, with node 10 as the pivot -2 +1

7 Insertion: case 3 (contd.) Case 3b: The lowest node (with a balance factor of +2) had a taller right-subtree and the insertion was on the left-subtree of its right child. Requires a double right-left rotation to rebalance. Insert 15 right rotation, with node 16 as the pivot left rotation, with node 9 as the pivot +2

8 AVL Rotation Summary -2 +1 -2 +1 +2 Single right rotation Double left-right rotation Single left rotation Double right-left rotation

9 Insertion Implementation The insert method of the AVLTree class is: 1 public void insert(MyComparable comparable){ 2 super.insert(comparable); 3 balance(); 4} Recall that the insert method of the BinarySearchTree class is: 1public void insert (MyComparable comparable) 2{ 3 if(isEmpty()) 4 attachKey(comparable); 5 else 6 { 7 MyComparable key = (MyComparable) getKey(); 8 if(comparable.isEQ(key)) 9 throw new IllegalArgumentException("duplicate key" ); 10 else if(comparable.isLT(key)) 11 getLeftBST().insert(comparable); 12 else 13 getRightBST().insert(comparable); 14 } 15 }

10 Insertion Implementation (contd.) The AVLTree class overrides the attachKey method of the BinarySearchTree class: 1 public void attachKey(Object object) 2 { 3 if( ! isEmpty()) 4 throw new InvalidOperationException(); 5 key = object; 6 left = new AVLTree(); 7 right = new AVLTree(); 8 height = 0; 9 }

11 Insertion Implementation (contd.) 1protected void balance() 2{ 3 adjustHeight(); 4 int balanceFactor = getBalanceFactor(); 5 if(balanceFactor == -2) 6 { 7 if(getLeftAVL().getBalanceFactor() < 0) 8 rotateRight(); 9 else 10 rotateLeftRight(); 11 } 12 else if(balanceFactor == 2) 13 { 14 if(getRightAVL().getBalanceFactor() > 0) 15 rotateLeft(); 16 else 17 rotateRightLeft(); 18 } 19 }

12 Deletion Delete by a BST deletion by copying algorithm. Rebalance the tree if an imbalance occurs. There are three deletion cases: 1.Deletion that does not cause an imbalance. 2.Deletion that requires a single rotation to rebalance. 3.Deletion that requires two or more rotations to rebalance. Deletion case 1 example: Delete 14

13 Deletion: case 2 examples Delete 40 right rotation, with node 35 as the pivot

14 Deletion: case 2 examples (contd.) Delete 32 left rotation, with node 44 as the pivot

15 Deletion: case 3 examples Delete 40 0 right rotation, with node 35 as the pivot right rotation, with node 30 as the pivot


Download ppt "AVL Search Trees Inserting in an AVL tree Insertion implementation Deleting from an AVL tree."

Similar presentations


Ads by Google