Compsci 201 Recurrences Binary Search Trees Owen Astrachan
Compsci 201 Recurrences & Binary Search Trees Owen Astrachan Jeff Forbes October 27, 2017 10/27/17 Compsci 201, Fall 2017, BSTs 1
Q is for … • Queue • First-in-first-out • Quadtree • Efficiently store points in a 2 -d space • Query • Databases vs. files • QWERTY • Bad ideas can persist? 10/27/17 Comp. Sci 201, Fall 2017, BSTs 2
Recognizing Recurrences • How to develop recurrence relation? • What is T(n)? Time func executes on input of size What other work? Express recurrence, solve it • How to solve recurrence relation • Plug, expand, plug, expand, find pattern or Lookup! • Proof requires induction to verify correctness T(n) T(n) = = = T(n/2) T(n-1) 2 T(n/2) T(n-1) 2 T(n-1) + + + O(1) O(n) O(1) binary search sequential search tree traversal quicksort selection sort Towers of Hanoi O( O( O( log n n 2 O( 2 n • Remember the algorithm, re-derive complexity ) ) )
Another recurrence int boom(int m, int x) { if (m == 0) return h(x); return boom(m-1, q(x)) + boom(m-1, r(x)); } • Assume h(x), q(x), and r(x) are all O(1) • Express cost of algorithm • How do m and x matter? http: //bit. ly/201 -f 17 -boom
Towards Trees • Trees are a useful data structure • O(log n) search on average • Ordered elements • Binary search trees are used in java. util. Tree. Set • Consider Sorted. Linked. Let and Tree. Set implementation in https: //coursework. cs. duke. edu/201 fall 17/setstuff 10/27/17 Comp. Sci 201, Fall 2017, BSTs 5
Binary Tree Node my. Root = null; public class Node { public int my. Value; public Node my. Left; public Node my. Right; public Node(int val) { my. Value = val; } }
Binary Tree
Binary Tree 0 1 2
Binary Search Tree • Each node has a value • Nodes with values less than their parent are in the left subtree • Nodes with values greater than their parent are in the right subtree • What about equal?
Binary Search Tree A B C http: //bit. ly/201 -f 17 -1027 -1
BSTs to Lists • Trees • Nodes with two pointers
BSTs to Lists • Doubly-linked Lists • Also nodes with two pointers head data prev 1 2 3 next 4 • How to convert from BST to list? 5
BST to List • Take a BST & return sorted circular linked list • Rearrange pointers • How much time is required? public static TLNode tree. To. List(TLNode root) • Base case? • Where should the root lie in the list relative to the lists formed from the left and right subtree?
Joining Nodes TLNode join(TLNode a, TLNode b) • How to implement? b a 3 4 http: //bit. ly/201 -f 17 -1027 -2 10/27/17 Comp. Sci 201, Fall 2017, BSTs 14
Appending Lists TLNode append(TLNode a, TLNode b) • What’s the Big-Oh in terms of # nodes in a & b? a b 1 2 3 4 return 10/27/17 Comp. Sci 201, Fall 2017, BSTs 15
Appending Lists TLNode append(TLNode a, TLNode b) { if (a == null) return b; if (b == null) return a; } 10/27/17 // Go to end of list a TLNode a. Tail = a; while (a. Tail. right != null) a. Tail = a. Tail. right; // What's true about a. Tail here? join(a. Tail, b); return a; Comp. Sci 201, Fall 2017, BSTs 16
Tree to List • How do do we update public static TLNode tree. To. List(TLNode root) { // base case if (root == null) return null; TLNode before. Me = tree. To. List(root. left); TLNode after. Me = tree. To. List(root. right); // TODO What do you need to do here? return root; } • 10/27/17 Comp. Sci 201, Fall 2017, BSTs 17
Circular Lists? • How does append change? • What is the overall big-Oh? http: //bit. ly/201 -f 17 -1027 -3 10/27/17 Comp. Sci 201, Fall 2017, BSTs 18
Balanced Trees and Complexity • A tree is height-balanced if • Left and right subtrees are height-balanced • Left and right heights differ by at most one boolean is. Balanced(Tree root){ if (root == null) return true; return is. Balanced(root. left) && is. Balanced(root. right) && Math. abs(height(root. left) – height(root. right)) <= 1; } } http: //bit. ly/201 -f 17 -1027 -4
Recurrence relation • Let T(n) be time for height to execute (n-node tree) • T(n) = T(n/2) + O(1) • T(n) = 2 T(n/2) + 1 • T(n) = 2 [2(T(n/4) + 1] + 1 • T(n) = 4 T(n/4) + 2 + 1 • T(n) = 8 T(n/8) + 4 + 2 + 1, eureka! • T(n) = 2 k. T(n/2 k) + 2 k-1 why is this true? • T(n) = n. T(1) + O(n) is O(n), if we let n=2 k • Let T(n) be time for is. Balanced on n-node tree • T(n) = 2 T(n/2) + O(n), why? Solution?
Recurrence relation • T(n): time for is. Balanced to execute (n-node tree) • T(n) = T(n/2) + O(n) • T(n) = 2 T(n/2) + n • T(n) = 2 [2(T(n/4) + n/2] + n • T(n) = 4 T(n/4) + n = 4 T(n/4) + 2 n • T(n) = 8 T(n/8) + 3 n, eureka! • T(n) = 2 k. T(n/2 k) + kn why is this true? • T(n) = n. T(1) + n log(n) let n=2 k, so k=log n • So, solution for T(n) = 2 T(n/2) + O(n) is • O(n log n) -- base 2, but base doesn't matter
Printing a search tree in order • When is root printed? • After left subtree, before right subtree. void visit(Tree. Node t) { if (t != null) { visit(t. left); System. out. println(t. info); visit(t. right); } } “llama” “giraffe” “elephant” • Inorder traversal “hippo” “tiger” “jaguar” “monkey” “leopard” “pig”
Tree traversals • Different traversals useful in different contexts • In-order prints search tree in order • Visit left-subtree, process root, visit right-subtree • Pre-order useful for reading/writing trees • Process root, visit left-subtree, visit right-subtree • Post-order useful for destroying trees • Visit left-subtree, visit right-subtree, process root “llama” “giraffe” “elephant” “jaguar” “tiger” “monkey”
Warmup Questions
- Slides: 24