Presentation is loading. Please wait.

Presentation is loading. Please wait.

AVL Tree. Ordinary Binary Search Tree Searching time is O(height)Searching time is O(height)  log 2 n   height  n – 1  log 2 n   height  n – 1.

Similar presentations


Presentation on theme: "AVL Tree. Ordinary Binary Search Tree Searching time is O(height)Searching time is O(height)  log 2 n   height  n – 1  log 2 n   height  n – 1."— Presentation transcript:

1 AVL Tree

2 Ordinary Binary Search Tree Searching time is O(height)Searching time is O(height)  log 2 n   height  n – 1  log 2 n   height  n – 1 Speed of search depends on luck. 4 4 5 5 2 2 6 6 1 1 3 3 3 3 2 2 5 5 4 4 6 6 1 1 2 2 1 1 6 6 4 4 3 3 5 5 Like a Linked List Better time Best time

3 AVL Tree AVL = Binary Search Tress + Rule on heightAVL = Binary Search Tress + Rule on height hLhL hRhR AVL : Adelson-Velskii and Landis e e For each and every node, e

4 AVL Tree examples Null tree has height = -1 0 0 1021 0 11 1 22 121

5 Are these AVL?????? AVL Non-AVL

6 BST and AVL comparison BST from 1000 random data AVL from 1000 random data

7 Finding the height of AVL tree Let F h be an AVL Tree of height h that has the lowest possible number of nodes.Let F h be an AVL Tree of height h that has the lowest possible number of nodes. F0F0 F1F1 F2F2 F3F3 F4F4 F h – 2 F h – 1 FhFh |F h | = 1 + |F h–1 | + |F h–2 | Fibonacci Tree

8 Height of Fibonacci Tree AVL tree that has n nodes will have its height <= 1.44 log 2 n  log 2 n   h AVL  1.44 log 2 n

9 Maintaining the property of AVL Add and remove just like BST.Add and remove just like BST. But adding or removing can cause the tree to lose its AVL property.But adding or removing can cause the tree to lose its AVL property. –We need to rearrange the tree then. 1 1 2 2 5 5 0 0 3 3 6 6 8 8 9 9 6 6 8 8 9 9 0 0 1 1 2 2 5 5 3 3 6 6 8 8 5 5 6 6 8 8 1 1 3 3 2 2

10 AVL Node class AVLNode{ Comparable data; int height; AVLNode left; AVLNode right; AVLNode AVLNode(Comparable d, AVLNode left, AVLNode right) { data = d; this.left = left; this.right = right; height = 0; } public static int getHeightAVLNode(AVLNode n) { // the height of null is -1 return (n == null ? -1 : n.height); } Each node has its height information

11 AVL Node (cont.) public static void updateHeightAVLNode(AVLNode n) { if (n==null){return;} //do nothing int hL = getHeightAVLNode(n.left); int hR = getHeightAVLNOde(n.right); n.height = 1 + (hL > hR ? hL : hR); } public static int balanceValue(AVLNode n) { if (n==null){return 0;} return getHeightAVLNode(n.right) – getHeightAVLNode(n.left); } 0 +1 -2 +2

12 Rotation Rotation preserves all properties of BST.Rotation preserves all properties of BST. 15 20 99 10 5 5 15 10 5 5 20 99 rotateLeftChild(r) rotateRightChild(r) r r

13 rotateLeftChild( r ) public static AVLNode rotateLeftChild(AVLNode r) { AVLNode newRoot = r.left; r.left = newRoot.right; newRoot.right = r; updateHeightAVLNode(newRoot.right); updateHeightAVLNode(newRoot); return newRoot; } 10 20 5 5 15 99 r newRoot 20 10 5 5 15 99 newRoot  (1)

14 rotateRightChild( r ) public static AVLNode rotateRightChild(AVLNode r) { AVLNode newRoot = r.right; r.right = newRoot.left; newRoot.left = r; updateHeightAVLNode(newRoot.left); updateHeightAVLNode(newRoot); return newRoot; } 20 10 99 15 5 5 r newRoot 10 20 99 15 5 5 newRoot  (1)

15 Add and remove use rebalance class AVLTree{ AVLNode n; public AVLTree(){ n = null; } AVLNode insertAVL(AVLNode r, DType x) { … //same as insert in BST r = rebalance(r); return r; } AVLNode removeAVL(AVLNode r, DType x) {... // same as remove in BST r = rebalance(r); return r; }

16 rebalance : 1 st case 5 5 2 2 6 6 1 1 3 3 5 5 2 2 6 6 1 1 3 3 0 0 x x r r B C A r r x x C B A 5 5 2 2 6 6 1 1 3 3 0 0 rotateLeftChild(r)

17 rebalance : 2 nd case 3 3 5 5 2 2 6 6 4 4 3 3 5 5 2 2 6 6 4 4 9 9 x x r r B C A r r x x C B A 3 3 5 5 2 2 6 6 4 4 9 9 rotateRightChild(r)

18 rebalance : 3 rd case 5 5 2 2 6 6 1 1 3 3 rotateRightChild(r.left) 5 5 2 2 6 6 1 1 3 3 4 4 5 5 2 2 6 6 1 1 3 3 4 4 5 5 2 2 6 6 1 1 3 3 4 4 x x r r D D A A B B C C y y r r D D A A B B C C y y x x r r D D A A B B C C y y x x rotateLeftChild(r)

19 rebalance : 4 th case 2 2 5 5 1 1 6 6 4 4 rotateLeftChild(r.right) 2 2 5 5 1 1 6 6 4 4 3 3 2 2 5 5 1 1 6 6 4 4 3 3 2 2 5 5 1 1 6 6 4 4 3 3 x x r r A A D D C C B B y y r r A A D D C C B B y y x x r r A A D D C C B B y y x x rotateRightChild(r)

20 AVLNode rebalance(AVLNode r) { if (r == NULL) return r; int balance = balanceValue(r); if (balance == -2) { if (balanceValue(r.left) == 1) r.left = rotateRightChild(r.left); r = rotateLeftChild(r); } else if (balance == 2) { if (balanceValue(r.right) == -1) r.right = rotateLeftChild(r.right); r = rotateRightChild(r); } setHeightAVLNode(r); return r; } rebalance x x r r D D A A B B C C y y rotateLeftChild(r) x x r r B C A rotateRightChild(r.left) rotateLeftChild(r) rotateRightChild(r) rotateLeftChild(r.right) rotateRightChild(r) x x r r B C A x x r r A A D D C C B B y y  (1)

21 EXAMPLE 1, 2, 3, 6, 8, 4, 15, 14 1 1 2 1 2 3 2 31 6 2 31 6 8 2 61 8 3 4 3 6 8 4 2 1 15 3 6 8 4 2 1 14 2 31 2 61 8 3 2 31 6 8 4 3 6 8 4 2 1 3 6 8 4 2 1 15 3 6 4 2 1 14 158

22 Conclusion  AVL tree is a Binary Search Tree with height constraint.  It is proven that  log 2 n   h < 1.44 log 2 n  Each node stores its height.  Insert/remove, then rebalance  Rebalance = rotation  Add, remove, search take O(log n)


Download ppt "AVL Tree. Ordinary Binary Search Tree Searching time is O(height)Searching time is O(height)  log 2 n   height  n – 1  log 2 n   height  n – 1."

Similar presentations


Ads by Google