Data Structures Binary Search Trees • Dictionary ADT / Search ADT • Quick Tree Review • Binary Search Trees
Total Page:16
File Type:pdf, Size:1020Kb
Today’s Outline CSE 326: Data Structures Binary Search Trees • Dictionary ADT / Search ADT • Quick Tree Review • Binary Search Trees 5/30/2008 1 5/30/2008 2 The Dictionary ADT ADTs Seen So Far •jfogarty • Priority Queue • Data: James •Stack Fogarty –Push –Insert – a set of insert(jfogarty, ….) CSE 666 –Pop – DeleteMin (key, value) • phenry pairs Peter • Queue Henry CSE 002 – Enqueue find(boqin) – Dequeue • Operations: • boqin Then there is decreaseKey… • boqin – Insert (key, Bo, Qin, … Bo Qin value) CSE 002 – Find (key) Need pointer! Why? The Dictionary ADT is also Because find not efficient. – Remove (key)called the “Map ADT” 5/30/2008 3 5/30/2008 4 A Modest Few Uses Implementations insert find delete Θ(1) Θ(n) Θ(n) •Sets • Unsorted Linked-list • Dictionaries • Networks : Router tables Θ(1) Θ(n) Θ(n) • Unsorted array • Operating systems : Page tables • Compilers : Symbol tables log n + n Θ(log n) log n + n Probably the most widely used ADT! • Sorted array SO CLOSE! What limits the performance? 5/30/2008 5 5/30/2008 Time to move elements, can we mimic BinSearch with BST?6 Tree Calculations Tree Calculations Example Recall: height is max t How high is this tree? A number of edges from root to a leaf B C D E F G Find the height of the tree... H I height(t) = 1 + max {height(t.left), height(t.right)} height(B) = 1 runtime: height(C) = 4 J K L L Θ(N) (constant time for each node; so height(A) = 5 each node visited twice) M N 5/30/2008 7 5/30/2008 8 More Recursive Tree Calculations: Inorder Traversal Tree Traversals void traverse(BNode t){ A traversal is an order for visiting all the nodes of a tree + if (t != NULL) traverse (t.left); * 5 Three types: process t.element; • Pre-order: Root, left subtree, right subtree 2 4 traverse (t.right); • In-order: Left subtree, root, right subtree } (an expression tree) } • Post-order: Left subtree, right subtree, root 5/30/2008 9 5/30/2008 10 Binary Trees Binary Tree: Representation • Binary tree is A – a root left right A pointerpointer A – left subtree (maybe empty) B C B C B C – right subtree (maybe left right left right empty) pointerpointer pointerpointer D E F D E F Data • Representation:left right G H D E F pointer pointer left right left right left right pointerpointer pointerpointer pointerpointer I J 5/30/2008 11 5/30/2008 12 Binary Tree: Special Cases Binary Tree: Some Numbers! For binary tree of height h: – max # of leaves: 2h, for perfect tree A A A – max # of nodes: 2h+1 – 1, for perfect tree B C B C B C – min # of leaves: 1, for “list” tree D E F D E F G D E F G – min # of nodes: h+1, for “list” tree Complete Tree Perfect Tree H I Full Tree Average Depth for N nodes? 5/30/2008 13 5/30/2008 14 Binary Search Tree Data Structure Example and Counter-Example • Structural property – each node has ≤ 2children – result: • storage is small 8 5 8 • operations are simple • average depth is small 5 11 4 8 5 11 • Order property – all keys in left subtree smaller than root’s key 2 6 10 12 1 7 11 2 7 6 10 18 – all keys in right subtree larger than root’s key – result: easy to find any given key 4 7 9 14 3 4 All children must 15 20 obey order • What must I know about what I store? 13 BINARY SEARCH TREES? 21 Comparison, equality testing 5/30/2008 15 5/30/2008 16 Find in BST, Recursive Find in BST, Iterative Node Find(Object key, Node Find(Object key, 10 Node root) { Node root) { if (root == NULL) 10 return NULL; 5 15 while (root != NULL && if (key < root.key) root.key != key) { if (key < root.key) 5 15 2 9 20 return Find(key, root.left); root = root.left; else if (key > root.key) else 2 9 20 7 17 30 return Find(key, root = root.right; root.right); } else 7 17 30 return root; return root; Runtime: } } Runtime: 5/30/2008 17 5/30/2008 18 Insert in BST BuildTree for BST 10 • Suppose keys 1, 2, 3, 4, 5, 6, 7, 8, 9 are Insert(13) inserted into an initially empty BST. 5 15 Insert(8) Insert(31) Runtime depends on the order! – in given order 2 9 20 Θ(n2) 7 17 30 – in reverse order Insertions happen only Θ(n2) at the leaves – easy! Runtime: – median first, then left median, right median, etc. 5, 3, 7, 2, 1, 6, 8, 9 better: n log n 5/30/2008 19 5/30/2008 20 Bonus: FindMin/FindMax Deletion in BST 10 • Find minimum 10 5 15 5 15 2 9 20 • Find maximum 2 9 20 7 17 30 7 17 30 Why might deletion be harder than insertion? 5/30/2008 21 5/30/2008 22 Non-lazy Deletion Lazy Deletion • Removing an item disrupts the tree Instead of physically deleting nodes, just mark them as structure. deleted • Basic idea: find the node that is to be + simpler 10 removed. Then “fix” the tree so that it is + physical deletions done in batches still a binary search tree. + some adds just flip deleted 5 15 flag • Three cases: 2 9 20 – node has no children (leaf node) – extra memory for “deleted” flag – many lazy deletions = slow – node has one child finds 7 17 30 – some operations may have to – node has two children be modified (e.g., min and max) 5/30/2008 23 5/30/2008 24 Non-lazy Deletion – The Leaf Case Deletion – The One Child Case Delete(17) 10 Delete(15) 10 5 15 5 15 2 9 20 2 9 20 7 17 30 7 30 5/30/2008 25 5/30/2008 26 Deletion – The Two Child Case Deletion – The Two Child Case 10 Idea: Replace the deleted node with a value Delete(5) guaranteed to be between the two child subtrees 5 20 Options: 2 9 30 • succ from right subtree: findMin(t.right) • pred from left subtree : findMax(t.left) 7 A value guaranteed to be between the two subtrees! - succ from right subtree Now delete the original node containing succ or pred What can we replace 5 with? -predfrom left subtree • Leaf or one child case – easy! 5/30/2008 27 5/30/2008 28 Finally… Balanced BST Observation 10 • BST: the shallower the better! • For a BST with n nodes 7 replaces 5 7 20 – Average height is O(log n) – Worst case height is O(n) • Simple cases such as insert(1, 2, 3, ..., n) 2 9 30 lead to the worst case scenario Original node containing Solution: Require a Balance Condition that 7 gets deleted 1. ensures depth is O(log n) – strong enough! 2. is easy to maintain – not too strong! 5/30/2008 29 5/30/2008 30 Potential Balance Conditions Potential Balance Conditions 1. Left and right subtrees of the root 3. Left and right subtrees of every node have equal number of nodes have equal number of nodes Too weak! Too strong! Do height mismatch example Only perfect trees 2. Left and right subtrees of the root 4. Left and right subtrees of every node have equal height have equal height Too strong! Too weak! Only perfect trees Do example where there’s a left chain and a right chain, no other nodes 5/30/2008 31 5/30/2008 32 Balanced BST Observation CSE 326: Data Structures • BST: the shallower the better! • For a BST with n nodes AVL Trees – Average height is O(log n) – Worst case height is O(n) • Simple cases such as insert(1, 2, 3, ..., n) lead to the worst case scenario Solution: Require a Balance Condition that 1. ensures depth is O(log n) – strong enough! 2. is easy to maintain – not too strong! 33 34 Potential Balance Conditions The AVL BalanceAdelson-Velskii Condition and Landis 1. Left and right subtrees of the root AVL balance property: have equal number of nodes 2. Left and right subtrees of the root Left and right subtrees of every node have equal height have heights differing by at most 1 3. Left and right subtrees of every node have equal number of nodes • Ensures small depth 4. Left and right subtrees of every node – Will prove this by showing that an AVL tree of height h must have a lot of (i.e. O(2h)) nodes have equal height • Easy to maintain – Using single and double rotations 35 36 AVL trees or not? The AVL Tree Data Structure 6 4 8 Structural properties This is an 1. Binary tree property AVL tree 1 7 11 (0,1, or 2 children) 8 10 12 2. Heights of left and right subtrees of every node 5 11 6 differ by at most 1 4 8 Result: 2 6 10 12 Worst case depth of any 1 5 7 11 node is: O(log n) 4 7 9 13 14 3 Ordering property 15 2 – Same as for BST 37 38 Proving Shallowness Bound Testing the Balance Property Let S(h) be the min # of nodes in an AVL tree of height h=4 AVL tree of height h with the min # of nodes (12) 10 We need to be able to: Trees of height h = 1, 2, 3 …. 8 1. Track Balance 5 15 Claim: S(h) = S(h-1) + S(h-2) + 1 2.