Presentation is loading. Please wait.

Presentation is loading. Please wait.

TCSS 342 AVL Trees v1.01 AVL Trees Motivation: we want to guarantee O(log n) running time on the find/insert/remove operations. Idea: keep the tree balanced.

Similar presentations


Presentation on theme: "TCSS 342 AVL Trees v1.01 AVL Trees Motivation: we want to guarantee O(log n) running time on the find/insert/remove operations. Idea: keep the tree balanced."— Presentation transcript:

1 TCSS 342 AVL Trees v1.01 AVL Trees Motivation: we want to guarantee O(log n) running time on the find/insert/remove operations. Idea: keep the tree balanced after each operation. Solution: AVL (Adelson- Velskii and Landis) trees. AVL tree property: for every node in the tree, the height of the left and right subtrees differs by at most 1.

2 TCSS 342 AVL Trees v1.02 3 117 1 84 6 3 7 1 84 6 2 AVL tree not an AVL tree 5 5

3 TCSS 342 AVL Trees v1.03 AVL trees: find, insert AVL tree find is the same as BST find. AVL insert: same as BST insert, except that we might have to “fix” the AVL tree after an insert. These operations will take time O(d), where d is the depth of the node being found/inserted. What is the maximum height of an n-node AVL tree?

4 TCSS 342 AVL Trees v1.04 AVL tree insert Let x be the deepest node where an imbalance occurs. Four cases to consider. The insertion is in the 1.left subtree of the left child of x. 2.right subtree of the left child of x. 3.left subtree of the right child of x. 4.right subtree of the right child of x. Idea: Cases 1 & 4 are solved by a single rotation. Cases 2 & 3 are solved by a double rotation.

5 TCSS 342 AVL Trees v1.05 Single rotation example 21103 205 15 1 24 17 21 10 320 5 15 1 2 4 17

6 TCSS 342 AVL Trees v1.06 Single rotation in general a Z Y b X h+1 h h h  -1 height h+2 before insert a Z Y b X h+1 hh height h+2 after insert X < b < Y < a < Z

7 TCSS 342 AVL Trees v1.07 Cases 2 & 3 a Z Y b X h h+1 h h  -1 height h+2 before insert single rotation fails a Z Y b X h h+1 h

8 TCSS 342 AVL Trees v1.08 Double rotation, step 1 104 178 15 3 6 16 5 10 6 178 15 4 3 16 5

9 TCSS 342 AVL Trees v1.09 Double rotation, step 2 10 6 178 15 4 3 16 5 10 6 17 8 15 4 3 16 5

10 TCSS 342 AVL Trees v1.010 Double rotation in general a Z b W c X Y h-1 h h h height h+2 before insert a Z b W c X Y h-1 hhh height h+2 after insert h  0 W < b <X < c < Y < a < Z

11 TCSS 342 AVL Trees v1.011 Depth of an AVL tree Theorem: Any AVL tree with n nodes has height less than 1.441 log n. Proof: Given an n-node AVL tree, we want to find an upper bound on the height of the tree. Fix h. What is the smallest n such that there is an AVL tree of height h with n nodes? Let S h be the set of all AVL trees of height h that have as few nodes as possible.

12 TCSS 342 AVL Trees v1.012 Let w h be the number of nodes in any one of these trees. w 0 = 1, w 1 = 2 Suppose T  S h, where h  2. Let T L and T R be T’s left and right subtrees. Since T has height h, either T L or T R has height h-1. Suppose it’s T R. By definition, both T L and T R are AVL trees. In fact, T R  S h-1 or else it could be replaced by a smaller AVL tree of height h-1 to give an AVL tree of height h that is smaller than T.

13 TCSS 342 AVL Trees v1.013 Similarly, T L  S h-2. Therefore, w h = 1 + w h-2 + w h-1. Claim: For h  0, w h   h, where  = (1 +  5) / 2  1.6. Proof: The proof is by induction on h. Basis step: h=0. w 0 = 1 =  0. h=1. w 1 = 2 >  1. Induction step: Suppose the claim is true for 0  m  h, where h  1.

14 TCSS 342 AVL Trees v1.014 Then w h+1 = 1 + w h-1 + w h  1 +  h-1 +  h (by the i.h.) = 1 +  h-1 (1 +  ) = 1 +  h+1 (1+  =  2 ) >  h+1 Thus, the claim is true. From the claim, in an n-node AVL tree of height h, n  w h   h (from the Claim) h  log  n = (log n) / (log  ) < 1.441 log n

15 TCSS 342 AVL Trees v1.015 AVL tree: Running times find takes O(log n) time, because height of the tree is always O(log n). insert: O(log n) time because we do a find (O(log n) time), and then we may have to visit every node on the path back to the root, performing up to 2 single rotations (O(1) time each) to fix the tree. remove: O(log n) time. Left as an exercise.


Download ppt "TCSS 342 AVL Trees v1.01 AVL Trees Motivation: we want to guarantee O(log n) running time on the find/insert/remove operations. Idea: keep the tree balanced."

Similar presentations


Ads by Google