Compsci 201 P is for … Search Trees and Recursion • Patterns • Object-oriented design: from decorator to … • Password • From changeme to oh-oh • Phishing • From changeme to bitcoin! Susan Rodger March 6, 2020 3/6/2020 Compsci 201, Spring 2020 1 3/6/2020 Compsci 201, Spring 2020 2 Announcements Plan for DBSB • Binary Trees • Assignment P4 DNA-Link • Search and more: best of array and linked lists • Part 1 due yesterday! – Analysis, Partner form • O(1) insert and O(log n) search • Part 2 due March 19 – Code and more Analysis • Understanding structure and recursion • There is Discussion for Monday, March 16 • List has one node and another list • There is no Pre-Discussion before • Tree has one node/root and two subtrees • Comparing two objects • APT-5 out after break 3/6/2020 Compsci 201, Spring 2020 3 3/6/2020 Compsci 201, Spring 2020 4
Binary Search Trees Binary Search Trees • Nodes have left/right references: similar prev/next • Nodes have left/right references: similar prev/next • At each node: <= goes left, > goes right • At each node: <= goes left, > goes right � koala � • How do we search? • How do we search? � koala � � llama � � llama � � koala � • How do we insert? • How do we insert? � giraffe � � giraffe � � tiger � � tiger � � koala � � jaguar � � � monkey � � jaguar � � monkey � � elephant � � elephant � • Insert: “koala” • Insert: “koala” � koala � � pig � � pig � � hippo � � leopard � � hippo � � leopard � � koala � 3/6/2020 Compsci 201, Spring 2020 5 3/6/2020 Compsci 201, Spring 2020 6 Tree Terminology A TreeNode by any other name… • What does this look like? Doubly linked list? • Root: "top node", has no parent • "macaque". Subtrees also have a root public class TreeNode • Leaf: bottom nodes, have no children { � llama � TreeNode left; • "baboon", "lemur", "organutan" � giraffe � � tiger � TreeNode right; • Path: sequence of parent-child nodes String info; TreeNode(String s, • "macaque", "chimp", "lemur" TreeNode llink, TreeNode rlink){ info = s; left = llink; right = rlink; } } 3/6/2020 Compsci 201, Spring 2020 7 3/6/2020 Compsci 201, Spring 2020 8
Trees: Concepts and Code Tree Performance • In a search tree: property holds at every node • Search for any value. Compare to root and … • Similar to binary search. O(log N) if tree "good" • Nodes in left subtree are < (or <=) • Nodes in right subtree are > • Trees are generally well-behaved, but !!! • Guarantee? Balanced tree: AVL or Red-Black • To search or add: if not found? nd? • Look left if <= • We get O(log N) search and … … • Look right if > • No shifting to add, find leaf af • Iterative or recursive 3/6/2020 Compsci 201, Spring 2020 9 3/6/2020 Compsci 201, Spring 2020 10 Printing a Search Tree Good Search Trees and Bad Trees • Think of search trees as recursive/hierarchical • Empty OR Root/Node with two subtrees • What do we know about subtrees? Also tree! http://www.9wy.net/onlinebook/CPrimerPlus5/ch17lev1sec7.html 3/6/2020 Compsci 201, Spring 2020 11 3/6/2020 Compsci 201, Spring 2020 12
Print the Print the tree - tree - 1 Recursion Recursion Believe in recursion 1 OUTPUT: baboon chimp lemur 3/6/2020 Compsci 201, Spring 2020 13 3/6/2020 Compsci 201, Spring 2020 14 Print the Print the tree - tree - 2 Recursion Recursion 3 Believe in recursion 2 3 OUTPUT: OUTPUT: baboon chimp lemur macaque baboon chimp lemur macaque monkey orangutan tamarin 3/6/2020 Compsci 201, Spring 2020 15 3/6/2020 Compsci 201, Spring 2020 16
Print the If you don’t believe in recursion yet, tree - 1 let’s see all the steps. 2 Recursion 3 2 3 1 A green check mark will mean we have finished all the recursion for a node. 3/6/2020 Compsci 201, Spring 2020 17 3/6/2020 Compsci 201, Spring 2020 18 Print the Print the tree - tree - Recursion Recursion OUTPUT: OUTPUT: baboon chimp lemur macaque monkey orangutan tamarin 3/6/2020 Compsci 201, Spring 2020 19 3/6/2020 Compsci 201, Spring 2020 45
Print the Constructing and Printing Tree tree - • Code and visualize: constructor has 3 parameters 1 2 • Info, Left Subtree, Right Subtree Recursion 3 2 3 1 3/6/2020 Compsci 201, Spring 2020 46 3/6/2020 Compsci 201, Spring 2020 47 Just larger – PrintTree.java Visualize https://coursework.cs.duke.edu/201spring20/classcode • A different tree: • Left subtree? • Right subtree? ? 3/6/2020 Compsci 201, Spring 2020 48 3/6/2020 Compsci 201, Spring 2020 49
Three recursive calls Standard Tree Traversals • Pre-, In-, and Post- order • "apple" node • When is root visited? Before, in-between, after • null,null • Analogy: traveling the border: down, under, up • Up to "durian" • https://coursework.cs.duke.edu/201spring20/classcode • See TreeDemo.java, alphabetical for search tree 3/6/2020 Compsci 201, Spring 2020 50 3/6/2020 Compsci 201, Spring 2020 51 Preorder traversal Inorder traversal – the p print we just did • Analogy: traveling the border: on the way down • Analogy: traveling the border: under a node • Useful to read and write trees: Huffman • Useful to print the elements in order macaque, chimp, baboon, lemur, monkey, tamarin, orangutan baboon, chimp, lemur, macaque, monkey, orangutan, tamarin 3/6/2020 Compsci 201, Spring 2020 52 3/6/2020 Compsci 201, Spring 2020 54
Postorder traversal Motivation for Trees • Analogy: traveling the border: on the way up • Useful to destroy/delete trees • HashSet and HashMap are O(1) average • Astonishing! Search, insert, delete • No order for keys, sometimes order matters • Worst-case ? Everything in same locker/bucket • Just in case? Use a tree in that locker/bucket • Search Trees: TreeSet and TreeMap • O(log N) no matter what, average and worst • "Alphabetical" order and range queries • Find all keys in range [low,high] efficiently baboon, lemur, chimp, orangutan, tamarin, monkey, macaque 3/6/2020 Compsci 201, Spring 2020 56 3/6/2020 Compsci 201, Spring 2020 58 Why Trees are O(log N) WOTO • With each query: eliminate half of tree http://bit.ly/201spring20-0306-1 • 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1 • Can ensure trees are balanced: TreeSet/TreeMap • Re-balance on add or delete 3/6/2020 Compsci 201, Spring 2020 59 3/6/2020 Compsci 201, Spring 2020 60
Richard Stallman Not Everything is Comparable • Created "free" software foundation • Speech not beer • Wrote Gnu C compiler • No Linux without gcc • MacArthur award, Hopper award • Maybe world's best programmer? You and I we exist for ourselves, fundamentally. We should care about others but each human being is a source of value, each human being deserves things. And so if you lose control over your computing, that's bad for you, directly bad for you. So my first reaction is to say: Oh, what a shame; I hope you recover the control over your computing and the way you do that is to stop using the non-free software. 3/6/2020 Compsci 201, Spring 2020 61 3/6/2020 Compsci 201, Spring 2020 62 Java-isms for comparing Strings are Comparable • Compare strings lexicographically, natural ordering, • We can compare int, double, char dictionary order • Using ==, and !=, and <, <=, >, >= • “zebra” > “aardvark” but “Zebra” < “aardvark” • Primitives use conventional symbols • Conceptual, cannot use < or > or == • We had to use s.equals(t) for strings/objects • Cannot write "apple" < "zebra" • "yak".compareTo(s) returns < 0, == 0, > 0 • Must compare objects using specific method • s is “zebra”, “yak”, and “toad”, respectively • Objects must be comparable , that is they must implement the Comparable interface • The int convention also used in C++, C, others 3/6/2020 Compsci 201, Spring 2020 63 3/6/2020 Compsci 201, Spring 2020 64
Comparable in Java? Don't do this at home: (x,y) < (z,w) • String implements Comparable<String> • Can we compare Point objects? "hello".compareTo("goodbye") • http://stackoverflow.com/questions/5178092/so rting-a-list-of-points-with-java • Integer implements Comparable<Integer> • Let's look at the Java code that makes a Point new Integer(5).compareTo(new Integer(6)) comparable to another Point • Point implements Comparable<Point> • Cannot compare ArrayLists or arrays • public int compareTo(Point other) • Note: .equals works for ArrayList, not arrays 3/6/2020 Compsci 201, Spring 2020 65 3/6/2020 Compsci 201, Spring 2020 67 Build on What You Know Extend what you know • How does .equals work? • This is method in Point class • Make sure you have the correct type Point implements Comparable<Point> Note: parameter is Point and not Object • Cast, compare public int compareTo(Point p) { public boolean equals(Object o) { if (this.x < p.x) return -1; if (o == null || ! (o instanceof Point)) { if (this.x > p.x) return 1; return false; // what must be true here? } if (this.y < p.y) return -1; Point p = (Point) o; if (this.y > p.y) return 1 return p.x == x && p.y == y; return 0; } } 3/6/2020 Compsci 201, Spring 2020 68 3/6/2020 Compsci 201, Spring 2020 69
Recommend
More recommend