Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003.

Similar presentations


Presentation on theme: "1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003."— Presentation transcript:

1 1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003

2 2 Outline Finish discussion on random binary search trees (BST) AVL trees Reading assignment for this week: Weiss: 4.3, 4.4, 4.5, and 4.7

3 3 The Average Depth of a BST Insert the elements 1 <2 <... < n in some order, starting with the empty tree For each permutation,  : –T  = the BST after inserting  (1),  (2),...,  (n) The Average Depth:

4 4 The Average Depth of a BST The average depth of a BST is: H(n) =  (log n) For some , height(T  ) = O(log n) For other , height(T  ) = O(n) But the average is O(log n) Please read the proof in the book and/or slides !

5 5

6 6 Random Input vs. Random Trees Inputs 1,2,3 3,2,1 1,3,2 3,1,2 2,1,3 2,3,1 Trees For three items, the shallowest tree is twice as likely as any other – effect grows as n increases. For n=4, probability of getting a shallow tree > 50%

7 7 Average cost The average, amortized cost of n insert/find operations is O(log(n)) But the average, amortized cost of n insert/find/delete operations can be as bad as  (  n) –Deletions make life harder (recall stretchy arrays) Need guaranteed cost O(log n) – next

8 8 Beauty is Only  (log n) Deep Binary Search Trees are fast if they’re shallow e.g.: complete Problems occur when one branch is much longer than the other How to capture the notion of a “sort of” complete tree?

9 9 Balance balance = height(left subtree) - height(right subtree) convention: height of a “null” subtree is -1 zero everywhere  perfectly balanced small everywhere  balanced enough:  (log n) –Precisely: Maximum depth is 1.44 log n t 5 6

10 10 AVL Tree (Adelson-Velskii Landis) 4 121062 115 8 141379 Binary search tree properties Balance of every node is -1  b  1 Tree re-balances itself after every insert or delete 15 What is the balance of each node in this tree?

11 11 AVL Tree Data Structure 15 92 12 5 10 20 17 0 0 100 12 3 10 3 data height children 30 0

12 12 Not An AVL Tree 15 92 12 5 10 20 17 0 1 200 13 4 10 4 data height children 30 0 18 0

13 13 Bad Case #1 Insert(small) Insert(middle) Insert(tall) T M S 0 1 2

14 14 Single Rotation T M S 0 1 2 M ST 00 1 Basic operation used in AVL trees: A right child could legally have its parent as its left child.

15 15 General Case: Insert Unbalances a X Y b Z h h - 1 h + 1 h - 1 h + 2 a X Y b Z h-1 h h + 1 a XY b Z h h - 1 h h + 1

16 16 Properties of General Insert + Single Rotation Restores balance to a lowest point in tree where imbalance occurs After rotation, height of the subtree (in the example, h+1) is the same as it was before the insert that imbalanced it Thus, no further rotations are needed anywhere in the tree!

17 17 Bad Case #2 Insert(small) Insert(tall) Insert(middle) M T S 0 1 2 Why won’t a single rotation (bringing T up to the top) fix this?

18 18 Double Rotation M ST 00 1 M T S 0 1 2 T M S 0 1 2

19 19 General Double Rotation Initially: insert into X unbalances tree (root height goes to h+3) “Zig zag” to pull up c – restores root height to h+2, left subtree height to h a Z b W c Y a Z b W c Y h+1 h h h h + 3 h + 2 hh h+1 h + 2 h+1 h X X

20 20 Another Double Rotation Case Initially: insert into Y unbalances tree (root height goes to h+2) “Zig zag” to pull up c – restores root height to h+1, left subtree height to h a Z b W c Y a Z b W c Y h+1 h h h h + 3 h + 2 h h h+1 h + 2 h+1 h X X

21 21 Insert Algorithm Find spot for value Hang new node Search back up looking for imbalance If there is an imbalance: “outside”: Perform single rotation and exit “inside”: Perform double rotation and exit

22 22 AVL Insert Algorithm Node insert(Comparable x, Node root){ if ( root == NULL ) return new Node(x); if (x == root.key) return root; if (x < root.key){ root.left = insert( x, root.left ); if (root unbalanced) { rotate... } } else { // x > root.key root.right = insert( x, root.right ); if (root unbalanced) { rotate... } } root.height = max(root.left.height, root.right.height)+1; return root; } Node insert(Comparable x, Node root){ if ( root == NULL ) return new Node(x); if (x == root.key) return root; if (x < root.key){ root.left = insert( x, root.left ); if (root unbalanced) { rotate... } } else { // x > root.key root.right = insert( x, root.right ); if (root unbalanced) { rotate... } } root.height = max(root.left.height, root.right.height)+1; return root; }

23 23 Deletion (Really Easy Case) 2092 155 10 30173 12 1 0 100 22 3 0 0 Delete(17)

24 24 Deletion (Pretty Easy Case) 2092 155 10 30173 12 1 0 100 22 3 0 0 Delete(15)

25 25 Deletion (Pretty Easy Case cont.) 2092 175 10 303 12 1 100 22 3 0 0 Delete(15)

26 26 Deletion (Hard Case #1) 2092 175 10 303 12 1 100 22 3 0 0 Delete(12)

27 27 Single Rotation on Deletion 2092 175 10 303 1 10 22 3 0 0 92 205 10 17 3 1 00 21 3 0 0 What is different about deletion than insertion?

28 28 Deletion (Hard Case) Delete(9) 2092 175 10 303 12 1 220 23 4 0 33 15 13 00 1 0 20 30 12 33 15 13 1 00 11 0 18 0

29 29 Double Rotation on Deletion 2 3 0 202 175 10 30 12 1 22 23 4 33 15 13 1 00 1 11 0 18 0 2052 173 10 30 12 0 220 13 4 33 15 13 1 00 1 11 0 18 00 Not finished!

30 30 Deletion with Propagation We get to choose whether to single or double rotate! 2052 173 10 30 12 0 220 13 4 33 15 13 1 00 1 11 0 18 0 What different about this case?

31 31 Propagated Single Rotation 0 30 20 17 33 12 15 13 1 0 52 3 10 4 32 121 000 11 0 2052 173 10 30 12 0 220 13 4 33 15 13 1 0 1 11 0 18 0 0

32 32 Propagated Double Rotation 0 17 12 11 52 3 10 4 23 10 0 0 2052 173 10 30 12 0 220 13 4 33 15 13 1 0 1 11 0 18 0 15 1 0 20 30 33 1 18 0 13 0 2

33 33 AVL Deletion Algorithm Recursive 1.If at node, delete it 2.Otherwise recurse to find it in 3. Correct heights a. If imbalance #1, single rotate b. If imbalance #2 (or don’t care), double rotate Iterative 1. Search downward for node, stacking parent nodes 2. Delete node 3. Unwind stack, correcting heights a. If imbalance #1, single rotate b. If imbalance #2 (or don’t care) double rotate

34 34 Pro: All operations guaranteed O(log N) The height balancing adds no more than a constant factor to the speed of insertion Con: Space consumed by height field in each node Slower than ordinary BST on random data Can we guarantee O(log N) performance with less overhead? Splay trees next time Pros and Cons of AVL Trees


Download ppt "1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003."

Similar presentations


Ads by Google