Compsci 201 201 Sear earch T Tree ees an and Recursio sion Susan Rodger March 6, 2020 3/6/2020 Compsci 201, Spring 2020 1
P is for … • Patterns • Object-oriented design: from decorator to … • Password • From changeme to oh-oh • Phishing • From changeme to bitcoin! 3/6/2020 Compsci 201, Spring 2020 2
Announcements • Assig ignm nment nt P P4 DNA-Link ink • Part 1 due yesterday! – Analysis, Partner form • Part 2 due March 19 – Code and more Analysis • Ther There is is Discus cussio ion f n for M Monday, M , March 1 h 16 • There is no Pre-Discussion before • APT APT-5 o out ut af after b brea eak 3/6/2020 Compsci 201, Spring 2020 3
Plan for DBSB • Bina inary T Tree ees • Search and more: best of array and linked lists • O(1) insert and O(log n) search • Understanding structure and recursion • List has one node and another list • Tree has one node/root and two subtrees • Compar aring ing t two object cts 3/6/2020 Compsci 201, Spring 2020 4
Binary Search Trees • Nodes h have l e left/r /rig ight ht r refer erenc ences: s similar ilar prev rev/ne /next • At each node: <= goes left, > goes right • How d do w we search? h? “ llama ” • How d do w we e ins insert? “ giraffe ” “ tiger ” “ jaguar ” “ monkey ” “ elephant ” • Insert: “ “koala” la” “ pig ” “ hippo ” “ leopard ” 3/6/2020 Compsci 201, Spring 2020 5
Binary Search Trees • Nodes h have l e left/r /rig ight ht r refer erenc ences: s similar ilar prev rev/ne /next • At each node: <= goes left, > goes right “ koala ” “ koala ” • How d do w we search? h? “ llama ” “ koala ” • How d do w we e ins insert? “ giraffe ” “ tiger ” “ koala ” “ jaguar ” “ monkey ” “ elephant ” • Insert: “ “koala” la” “ koala ” “ pig ” “ hippo ” “ leopard ” “ koala ” 3/6/2020 Compsci 201, Spring 2020 6
Tree Terminology • Root: " "top n node", h has s no parent • "macaque". Subtrees also have a root • Leaf af: : bottom n nodes es, h have n no childr dren en • "baboon", "lemur", "organutan" • Pat ath: s : sequen ence ce of p paren rent-ch child n d nodes es • "macaque", "chimp", "lemur" 3/6/2020 Compsci 201, Spring 2020 7
A TreeNode by any other name… • Wha hat does es t thi his lo look lik like? D Doub ubly link linked lis list? public class TreeNode { “ llama ” TreeNode left; “ giraffe ” “ tiger ” TreeNode right; String info; TreeNode(String s, TreeNode llink, TreeNode rlink){ info = s; left = llink; right = rlink; } } 3/6/2020 Compsci 201, Spring 2020 8
Trees: Concepts and Code • In a sear arch t ch tree ee: : proper erty h holds a at e ever ery n node • Nodes in left subtree are < (or <=) • Nodes in right subtree are > • To sea earch o or ad add: if if no not f found und? • Look left if <= • Look right if > • Iterative or recursive 3/6/2020 Compsci 201, Spring 2020 9
Tree Performance • Sea earch f for any any val alue. C Compare t to root and and … • Similar ilar t to binar ary s sear arch. ch. O(log N) if if tree ee "g "good" • Trees are generally well-behaved, but !!! • Guarantee? Balanced tree: AVL or Red-Black • We e get O O(lo log N N) sea earch and and … … • No shifting to add, find leaf 3/6/2020 Compsci 201, Spring 2020 10
Good Search Trees and Bad Trees http://www.9wy.net/onlinebook/CPrimerPlus5/ch17lev1sec7.html 3/6/2020 Compsci 201, Spring 2020 11
Printing a Search Tree • Think nk o of sear arch t ch trees ees as as recur cursiv ive/hier /hierar archical chical • Empty OR Root/Node with two subtrees • What do we know about subtrees? Also tree! 3/6/2020 Compsci 201, Spring 2020 12
Print the tree - Recursion 3/6/2020 Compsci 201, Spring 2020 13
Print the tree - 1 Recursion Believe in recursion 1 OUTPUT: baboon chimp lemur 3/6/2020 Compsci 201, Spring 2020 14
Print the tree - 2 Recursion 2 OUTPUT: baboon chimp lemur macaque 3/6/2020 Compsci 201, Spring 2020 15
Print the tree - Recursion 3 Believe in recursion 3 OUTPUT: baboon chimp lemur macaque monkey orangutan tamarin 3/6/2020 Compsci 201, Spring 2020 16
Print the tree - 1 2 Recursion 3 2 3 1 3/6/2020 Compsci 201, Spring 2020 17
If you don’t believe in recursion yet, let’s see all the steps. A green check mark will mean we have finished all the recursion for a node. 3/6/2020 Compsci 201, Spring 2020 18
Print the tree - Recursion OUTPUT: 3/6/2020 Compsci 201, Spring 2020 19
Print the tree - Recursion OUTPUT: baboon chimp lemur macaque monkey orangutan tamarin 3/6/2020 Compsci 201, Spring 2020 45
Print the tree - 1 2 Recursion 3 2 3 1 3/6/2020 Compsci 201, Spring 2020 46
Constructing and Printing Tree • Code de a and d visuali ualize: constru ructor h r has 3 3 parame meters rs • Info, Left Subtree, Right Subtree 3/6/2020 Compsci 201, Spring 2020 47
Just larger – PrintTree.java https://coursework.cs.duke.edu/201spring20/classcode 3/6/2020 Compsci 201, Spring 2020 48
Visualize • A differ erent ent t tree: • Left subtree? • Right subtree? 3/6/2020 Compsci 201, Spring 2020 49
Three recursive calls • "appl pple" n node de • null,null • Up to " "durian" ian" 3/6/2020 Compsci 201, Spring 2020 50
Standard Tree Traversals • Pre re-, I , In-, and and P Post- ord rder • When is root visited? Before, in-between, after • Analogy: t : traveling eling t the b border er: d down, u under er, u , up • https://coursework.cs.duke.edu/201spring20/classcode • See TreeDemo.java, alphabetical for search tree 3/6/2020 Compsci 201, Spring 2020 51
Inor order der traversal – the print nt we just did • Analogy: t : traveling eling t the b border er: und under a a no node • Useful to print the elements in order baboon, , chimp mp, l lemu mur, , macaqu aque, m , monkey, o , orangutan an, , tamari rin 3/6/2020 Compsci 201, Spring 2020 52
Preor eorder der traversal • Analogy: t : traveling eling t the b border er: o on the w e way d down • Useful to read and write trees: Huffman macaque, c chimp, b baboon, l lemur, monkey, t tamarin, ora rangutan 3/6/2020 Compsci 201, Spring 2020 54
Postorder der traversal • Analogy: t : traveling eling t the b border er: o on the w e way u up • Useful to destroy/delete trees baboon, l lemur, c chimp, ora rangutan, t tamarin, monkey, m macaque 3/6/2020 Compsci 201, Spring 2020 56
Motivation for Trees • Has ashS hSet and H Has ashM hMap ap a are O O(1) a aver erage • 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 • Sear arch ch T Trees ees: Tre reeSet et and nd TreeM eMap ap • O(log N) no matter what, average and worst • "Alphabetical" order and range queries • Find all keys in range [low,high] efficiently 3/6/2020 Compsci 201, Spring 2020 58
Why Trees are O(log N) • Wit ith eac each q quer uery: elim eliminate half half o of tree • 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1 • Can e n ensur ure t e trees es a are b balanc anced ed: : TreeS eSet/TreeM eMap ap • Re-balance on add or delete 3/6/2020 Compsci 201, Spring 2020 59
WOTO http://b ://bit.l .ly/20 /201s 1spring2 g20-030 306-1 3/6/2020 Compsci 201, Spring 2020 60
Richard Stallman • Created " "free" ee" s software f foundation • Speech not beer • Wrote Gnu C compiler • No Linux without gcc • MacArthur award, Hopper award • Maybe w world's b best p pro rogra rammer? mmer? 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
Not Everything is Comparable 3/6/2020 Compsci 201, Spring 2020 62
Java-isms for comparing • We can c n compare e int int, d doub uble le, cha char • Using ==, and !=, and <, <=, >, >= • Primitives use conventional symbols • Canno nnot write " e "apple" le" < < "zeb ebra" a" • Must compare objects using specific method • Objects must be compar arable le , that is they must implement the Comparab able le interface 3/6/2020 Compsci 201, Spring 2020 63
Recommend
More recommend