Compsci 201 Recurrences Binary Search Trees Owen Astrachan
Compsci 201 Recurrences & Binary Search Trees Owen Astrachan Jeff Forbes October 27, 2017 9/26/2020 Comp. Sci 201, Spring 2018, 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? 9/26/2020 Comp. Sci 201, Spring 2018, BSTs 2
Binary Tree Review • Binary tree is a structure: • empty • root node with left and right subtrees • Binary trees: efficient insert, delete, and search • trees used in many contexts, not just for searching, • Game trees, collisions, … • Cladistics, genomics, quad trees, … • search in O(log n) like sorted array • Average case, worst case can be avoided! • insertion/deletion O(1) like list, once location 9/26/2020 Comp. Sci 201, Spring 2018, BSTs found 3
What does insertion look like? • Simple recursive insertion into tree (accessed by root) root = insert("foo", root); Tree. Node insert(Tree. Node t, String s) { if (t == null) t = new Tree(s, null); else if (s. compare. To(t. info) <= 0) t. left = insert(t. left, s); else t. right = insert(t. right, s); return t; }
Complete Binary Tree • Binary Tree: empty or node with two links • Complete binary tree • every non-leaf node has two children • all the leaves are at the same level. • How many nodes in complete tree with N levels? • Recall 1 + 2 + 4 + … + 2 N • 2 N+1 – 1 = 2 x 2 N – 1 which is O(2 N ) 9/26/2020 Comp. Sci 201, Spring 2018, BSTs 5
How many trees? • How many differently shaped trees are there with n nodes? • 0 nodes: 1 unique tree • 1 node: 1. 2 nodes: 2 • 4 nodes: 14 • 3 nodes? http: //bit. ly/201 -s 18 -0323 -0 • What about BSTs? • Can you make a different number of trees with the elements (1, 1, 1) vs. (1, 2, 3) vs. (-4, 72, 0)? 9/26/2020 Comp. Sci 201, Spring 2018, BSTs 6
BSTs to Lists • Trees • Nodes with two pointers 9/26/2020 Comp. Sci 201, Spring 2018, BSTs 7
BSTs to Lists • Doubly-linked Lists • Also nodes with two pointers head data prev 1 2 3 next 4 5 • How to convert from BST to list? 9/26/2020 Comp. Sci 201, Spring 2018, BSTs 8
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? 9/26/2020 Comp. Sci 201, Spring 2018, BSTs 9
Joining Nodes TLNode join(TLNode a, TLNode b) • How to implement? b a 3 4 http: //bit. ly/201 -s 18 -0323 -1 9/26/2020 Comp. Sci 201, Spring 2018, BSTs 10
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 9/26/2020 Comp. Sci 201, Spring 2018, BSTs 11
Appending Lists TLNode append(TLNode a, TLNode b) { if (a == null) return b; if (b == null) return a; } 9/26/2020 // 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, Spring 2018, BSTs 12
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; } • 9/26/2020 Comp. Sci 201, Spring 2018, BSTs 13
Circular Lists? • How does append change? • What is the overall big-Oh? http: //bit. ly/201 -s 18 -0323 -2 9/26/2020 Comp. Sci 201, Spring 2018, BSTs 14
Autocomplete Overview • In this assignment, you will be completing the following three classes: • Term. java • Binary. Search. Autocomplete. java • Trie. Autocomplete. java • In doing so, you will practice the following skills: • Writing Comparators • Using binary search on sorted lists • Creating and navigating tries • Analyzing tradeoffs between different algorithms 15
Searching public interface Autocompletor { // Returns the top k matching terms in descending order of weight. public Iterable<String> top. Matches(String prefix, int k); // Returns the single top matching term public String top. Match(String prefix); // Return the weight of a given term public double weight. Of(String term); } https: //coursework. cs. duke. edu/201 spring 18/autocomplete-start 9/26/2020 Comp. Sci 201, Trees 16
Brute. Autocomplete • Naïve approach to autocomplete • Store data as a Term array. • Find the top k matches: • iterates through the array, • pushes all terms starting with the prefix onto a max -priority queue sorted by weight. • Return top k terms off that priority queue • Find top match is similar 9/26/2020 Comp. Sci 201, Trees 17
Autocomplete Understanding 1 • Given the following definitions: String[] names = {"ape", "app", "ban", "bat", "bee", "car", "cat"}; double[] weights = {6, 4, 2, 3, 5, 7, 1 }; Autocompletor ac = new Brute. Autocomplete(names, weights); Complete as part of discussion • If Brute. Auto. Complete and Term was correct, what is? • ac. top. Match("") warmup • ac. top. Matches("", 6) • ac. top. Matches("b", 2) • ac. top. Matches("ba", 2) • ac. top. Matches("d", 10) 9/26/2020 Comp. Sci 201, Spring 2018, BSTs 18
The Term class • The Term class encapsulates a Comparable wordweight pair. • Includes completed compare. To method, which sorts lexicographically. • You are responsible for implementing three Term Comparators: • Weight. Order, which sorts in ascending weight order • Reverse. Weight. Order, which sorts in descending weight order • Prefix. Order, which sorts by the first r characters 9/26/2020 Comp. Sci 201, Trees 19
Autocomplete Understanding 2 • Currently, when you run Brute. Autocomplete with Autocomplete. Main, results are not displayed in order of weights. • Why not? • What file and method do you need to change in order to produce the correct behavior? • Answer for Discussion Warmup 9/26/2020 Comp. Sci 201, Spring 2018, BSTs 20
Prefix. Order • The goal of Prefix. Order is to sort lexicographically, but only considering the first r characters. • e. g. normally we would put “energy” before “entropy” lexicographically. However, Prefix. Order with r = 2 considers them equal (Prefix. Order with r = 3 would still put “energy” before “entropy”, however). • If one or both of the words is shorter than r characters, we just use normal lexicographic sorting. • For full credit, Prefix. Order’s compare method should take O(r). 9/26/2020 Comp. Sci 201, Trees 21
Binary. Search. Autocomplete • For Autocomplete: find the range of all terms comparator considers equal to key • e. g. , all terms with a that match prefix auto • Binary. Search. Autocomplete is the 2 nd class you should implement. • Binary. Search. Autocomplete implements Autocompletor plus: • public static int first. Index. Of(Term[] a, Term key, Comparator<Term> comp) • public static int last. Index. Of(Term[] a, Term key, Comparator<Term> comp) • Use binary search to quickly return the first and last index respectively of an element in the input array which the comparator considers equal to key. • We specify first and last index because there could be multiple Terms in a which the comparator considers equal to key. 9/26/2020 Comp. Sci 201, Trees 22
Autocomplete Understanding 3 • What happens if you use Collections. binary. Search to implement Binary. Search. first. Index. Of? • How? Find the index and then find first/earliest matching element • What is the worst case big-Oh? • Answer for Discussion Warmup 9/26/2020 Comp. Sci 201, Spring 2018, BSTs 23
Using tries for autocomplete • To completely eliminate our dependence on the terms which don’t start with our prefix, let’s use a trie instead of an array to store terms • Why is this an improvement? Consider trying to find the top k matches starting with some string str. • The very first thing we should do is navigate to the node representing str. The trie rooted at this node only contains nodes starting with str. • No matter how many words are in 9/26/2020 our trie, navigating to this node Comp. Sci 201, Trees 24
Tries • How to search for Terms? • Consider a trie (also called prefix tree), utilized in Trie. java: • This trie represents: • Dog • Doting • Drastic • Top • Torn • Trap 9/26/2020 Comp. Sci 201, Trees 25
Tries • This trie supports queries (add, contains, delete) in O(w) time for words of length w. • Each node in a trie has one subtrie for every next valid letter that can follow • • How would you add “duke” to the trie? How many new nodes created? (warmup) • Red dots indicate nodes holding the final 9/26/2020 Comp. Sci 201, Trees letter of a word 26
Discussion Warmup • Read Autocomplete assignment • Answer questions 9/26/2020 Comp. Sci 201, Spring 2018, BSTs 27
- Slides: 27