Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 326: Data Structures AVL Trees

Similar presentations


Presentation on theme: "CSE 326: Data Structures AVL Trees"— Presentation transcript:

1 CSE 326: Data Structures AVL Trees
Ben Lerner Summer 2007

2 The AVL Balance Condition
Adelson-Velskii and Landis The AVL Balance Condition Left and right subtrees of every node have equal heights differing by at most 1 Define: balance(x) = height(x.left) – height(x.right) AVL property: –1  balance(x)  1, for every node x Ensures small depth Will prove this by showing that an AVL tree of height h must have a lot of (i.e. O(2h)) nodes Easy to maintain Using single and double rotations

3 The AVL Tree Data Structure
Structural properties Binary tree property Balance property: balance of every node is between -1 and 1 Result: Worst case depth is O(log n) Ordering property Same as for BST 8 5 11 2 6 10 12 So, AVL trees will be Binary Search Trees with one extra feature: They balance themselves! The result is that all AVL trees at any point will have a logarithmic asymptotic bound on their depths 4 7 9 13 14 15

4 Is this an AVL tree? We need to be able to: 1. Track the balance
2. Detect imbalance 3. Restore balance 10 5 15 2 9 20 We need to know a few things now: How do we track the balance? How do we detect imbalance? How do we restore balance? Let’s start with this tree and see if it’s balanced. By the way, is it leftist? No! because of 15 Height (level order): There’s that darn 15 again! It’s not balanced at 15. 7 17 30 (NULLs have height -1)

5 How about these? 6 4 8 1 7 11 12 10 3 11 7 1 8 4 6 2 5

6 Height of an AVL Tree M(h) = minimum number of nodes in an AVL tree of height h. Basis M(0) = 1, M(1) = 2 Induction M(h) = M(h-1) + M(h-2) + 1 Solution M(h) > h ( = (1+5)/2  1.62) h h-2 h-1

7 Proof that M(h) > h Basis: M(0) = 1 > 0 -1, M(1) = 2 > 1-1
Induction step. M(h) = M(h-1) + M(h-2) > (h-1 - 1) + (h-2 - 1) = h-1 + h = h-2 ( +1) = h (Uses fact that 2 =  +1)

8 Height of an AVL Tree M(h) > h (  1.62)
Suppose we have n nodes in an AVL tree of height h. N > M(h) N > h - 1 log(N+1) > h (relatively well balanced tree!!)

9 Node Heights 6 6 4 9 4 9 1 5 1 5 8 2 2 1 1 1 height of node = h
1 1 4 9 4 9 1 5 1 5 8 height of node = h balance factor = hleft-hright empty height = -1

10 Node Heights after Insert 7
balance factor 1-(-1) = 2 2 3 6 6 1 1 1 2 4 9 4 9 -1 1 1 5 7 1 5 8 7 height of node = h balance factor = hleft-hright empty height = -1

11 Insert and Rotation in AVL Trees
Insert operation may cause balance factor to become 2 or –2 for some node only nodes on the path from insertion point to root node have possibly changed in height So after the Insert, go back up to the root node by node, updating heights If a new balance factor (i.e. the difference hleft-hright) is 2 or –2, adjust tree by rotation around the node

12 Single Rotation in an AVL Tree
2 2 6 6 1 2 1 1 4 9 4 8 1 1 5 8 1 5 7 9 7

13 Insertions in AVL Trees
Let the node that needs rebalancing be . There are 4 cases: Outside Cases (require single rotation) : 1. Insertion into left subtree of left child of . 2. Insertion into right subtree of right child of . Inside Cases (require double rotation) : 3. Insertion into right subtree of left child of . 4. Insertion into left subtree of right child of . The rebalancing is performed through four separate rotation algorithms.

14 Where is AVL property violated?
Bad Case #1 Insert(6) Insert(3) Insert(1) But, let’s start over… Insert [SMALL] Now, [MIDDLE]. Now, [TALL]. Is this tree balanced? NO! Who do we need at the root? [MIDDLE!] Alright, let’s pull er up. Where is AVL property violated?

15 Fix: Apply Single Rotation
AVL Property violated at this node (x) 2 1 6 3 1 3 1 6 This is the basic operation we’ll use in AVL trees. Since this is a left child, it could legally have the parent as its right child. When we finish the rotation, we have a balanced tree! 1 Single Rotation: 1. Rotate between x and child

16 AVL Insertion: Outside Case
j Consider a valid AVL subtree k h Z h h X Y

17 AVL Insertion: Outside Case
j Inserting into X destroys the AVL property at node j k h Z h+1 h Y X

18 AVL Insertion: Outside Case
j Do a “rotation from left” k h Z h+1 h Y X

19 Single rotation from left
j k h Z h+1 h Y X

20 Outside Case Completed
k “rotation from left” done! (“rotation from right” is mirror symmetric) j h+1 h h X Z Y AVL property has been restored!

21 Single rotation example
15 5 20 3 10 17 21 2 4 1 21 10 3 20 5 15 1 2 4 17

22 Bad Case #2 Insert(1) Insert(6) Insert(3)
There’s another bad case, though. What if we insert: [SMALL] [TALL] [MIDDLE] Now, is the tree imbalanced? Will a single rotation fix it? (Try it by bringing up tall; doesn’t work!) Will single rotation fix? (No) (try bringing up Large; doesn’t work)

23 Fix: Apply Double Rotation
Intuition: 3 must become root AVL Property violated at this node (x) 2 2 1 1 1 3 1 1 6 3 1 6 3 6 Let’s try two single rotations, starting a bit lower down. First, we rotate up middle. Then, we rotate up middle again! Is the new tree balanced? Balanced? Double Rotation 1. Rotate between x’s child and grandchild 2. Rotate between x and x’s new child

24 AVL Insertion: Inside Case
j Consider a valid AVL subtree k h Z h h X Y

25 AVL Insertion: Inside Case
j Inserting into Y destroys the AVL property at node j Does “rotation from left” restore balance? k h Z h h+1 X Y

26 AVL Insertion: Inside Case
k “Rotation from left” does not restore balance… now k is out of balance j h X h h+1 Z Y

27 AVL Insertion: Inside Case
j Consider the structure of subtree Y… k h Z h h+1 X Y

28 AVL Insertion: Inside Case
j Y = node i and subtrees V and W k h Z i h h+1 X h or h-1 V W

29 AVL Insertion: Inside Case
j 2 We will do a “double rotation” . . . k 1 Z i X V W

30 Double rotation : first rotation
j i Z k W V X

31 Double rotation : second rotation
j i Z k W V X

32 Double rotation : second rotation
right rotation complete Balance has been restored to the universe i k j h h h or h-1 V Z X W

33 Double rotation, step 1 15 8 17 4 10 16 3 6 5 15 8 17 6 10 16 4 3 5

34 Double rotation, step 2 15 8 17 6 10 16 4 3 5 15 17 6 4 8 16 3 5 10

35 Imbalance at node X Single Rotation Rotate between x and child
Double Rotation Rotate between x’s child and grandchild Rotate between x and x’s new child

36 Insert into an AVL tree: a b e c d

37 Single and Double Rotations:
Inserting what integer values would cause the tree to need a: single rotation? double rotation? no rotation? 1,14+ 9 5 11 2 7 13 -1,4,12 3 6,8,10 Student Activity

38 Insertion into AVL tree
Find spot for new key Hang new node there with this key Search back up the path for imbalance If there is an imbalance: case #1: Perform single rotation and exit case #2: Perform double rotation and exit Zig-zig OK, thank you BST Three! And those two cases (along with their mirror images) are the only four that can happen! So, here’s our insert algorithm. We just hang the node. Search for a spot where there’s imbalance. If there is, fix it (according to the shape of the imbalance). And then we’re done; there can only be one problem! Zig-zag Both rotations keep the subtree height unchanged. Hence only one rotation is sufficient!

39 Easy Insert Insert(3) 10 5 15 2 9 12 20 17 30 Unbalanced? 3 1 2 1 3 No
1 2 9 12 20 Let’s insert 3. This is easy! It just goes under 2 (to the right). Update the balances: any imbalance? NO! 3 17 30 Unbalanced? No

40 Hard Insert (Bad Case #1)
2 3 Insert(33) 10 Imbalance 5 15 2 9 12 20 Now, let’s insert 33. Where does it go? Right of 30. 3 17 30 Unbalanced? Yes, at 15 How to fix? Single rotate Zig-zig

41 Single Rotation 1 2 3 1 2 3 10 10 5 15 5 20 2 9 12 20 2 9 15 30 3 17 30 3 12 17 33 Here’s the tree with the balances updated. Now, node 15 is bad! Since the problem is in the left subtree of the left child, we can fix it with a single rotation. We pull 20 up. Hang 15 to the left. Pass 17 to 15. And, we’re done! Notice that I didn’t update 10’s height until we checked 15. Did it change after all? 33

42 Hard Insert (Bad Case #2)
1 2 3 Insert(18) 10 Imbalance 5 15 2 9 12 20 Now, let’s back up to before 33 and insert 18 instead. Goes right of 17. Again, there’s imbalance. But, this time, it’s a zig-zag! 3 17 30 Unbalanced? Yes, at 15 How to fix? Double rotate Zig-zag

43 Single Rotation (oops!)
1 2 3 1 2 3 10 10 5 15 5 20 2 9 12 20 2 9 15 30 We can try a single rotation, but we end up with another zig-zag! 3 17 30 3 12 17 18 18

44 Double Rotation (Step #1)
2 3 1 2 3 10 10 5 15 5 15 2 9 12 20 2 9 12 17 3 17 30 3 20 So, we’ll double rotate. Start by moving the offending grand-child up. We get an even more imbalanced tree. BUT, it’s imbalanced like a zig-zig tree now! 18 18 30 Still unbalanced. But like zig-zig tree!

45 Double Rotation (Step #2)
1 2 3 1 2 3 10 10 5 15 5 17 2 9 12 17 2 9 15 20 So, let’s pull 17 up again. Now, we get a balanced tree. And, again, 10’s height didn’t need to change. 3 20 3 12 18 30 18 30

46 Implementation balance (1,0,-1) key left right

47 Single Rotation RotateFromRight(n : reference node pointer) {
p : node pointer; p := n.right; n.right := p.left; p.left := n; n := p } n X Y Z RotateFromLeft is symmetric

48 Double Rotation Class participation
Implement Double Rotation in two lines. DoubleRotateFromRight(n : reference node pointer) { ???? } n X Z V W

49 AVL Tree Deletion Similar to insertion
Rotations and double rotations needed to rebalance Imbalance may propagate upward so that many rotations may be needed.

50 Pros and Cons of AVL Trees
Arguments for AVL trees: Search is O(log N) since AVL trees are always well balanced. The height balancing adds no more than a constant factor to the speed of insertion, deletion, and find. Arguments against using AVL trees: Difficult to program & debug; more space for height info. Asymptotically faster but rebalancing costs time. Most large searches are done in database systems on disk and use other structures (e.g. B-trees). May be OK to have O(N) for a single operation if total run time for many consecutive operations is fast (e.g. Splay trees).

51 Double Rotation Solution
DoubleRotateFromRight(n : reference node pointer) { RotateFromLeft(n.right); RotateFromRight(n); } n X Z V W


Download ppt "CSE 326: Data Structures AVL Trees"

Similar presentations


Ads by Google