1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003
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 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 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
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 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 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 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 AVL Tree (Adelson-Velskii Landis) 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 AVL Tree Data Structure data height children 30 0
12 Not An AVL Tree data height children
13 Bad Case #1 Insert(small) Insert(middle) Insert(tall) T M S 0 1 2
14 Single Rotation T M S M ST 00 1 Basic operation used in AVL trees: A right child could legally have its parent as its left child.
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 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 Bad Case #2 Insert(small) Insert(tall) Insert(middle) M T S Why won’t a single rotation (bringing T up to the top) fix this?
18 Double Rotation M ST 00 1 M T S T M S 0 1 2
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 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 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 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 Deletion (Really Easy Case) Delete(17)
24 Deletion (Pretty Easy Case) Delete(15)
25 Deletion (Pretty Easy Case cont.) Delete(15)
26 Deletion (Hard Case #1) Delete(12)
27 Single Rotation on Deletion What is different about deletion than insertion?
28 Deletion (Hard Case) Delete(9)
29 Double Rotation on Deletion Not finished!
30 Deletion with Propagation We get to choose whether to single or double rotate! What different about this case?
31 Propagated Single Rotation
32 Propagated Double Rotation
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 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