compsci 201 201 sear earch t tree ees an and recursio sion
play

Compsci 201 201 Sear earch T Tree ees an and Recursio sion - PowerPoint PPT Presentation

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


  1. 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

  2. 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

  3. 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

  4. 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

  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 • 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. Good Search Trees and Bad Trees http://www.9wy.net/onlinebook/CPrimerPlus5/ch17lev1sec7.html 3/6/2020 Compsci 201, Spring 2020 11

  12. 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

  13. Print the tree - Recursion 3/6/2020 Compsci 201, Spring 2020 13

  14. Print the tree - 1 Recursion Believe in recursion 1 OUTPUT: baboon chimp lemur 3/6/2020 Compsci 201, Spring 2020 14

  15. Print the tree - 2 Recursion 2 OUTPUT: baboon chimp lemur macaque 3/6/2020 Compsci 201, Spring 2020 15

  16. 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

  17. Print the tree - 1 2 Recursion 3 2 3 1 3/6/2020 Compsci 201, Spring 2020 17

  18. 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

  19. Print the tree - Recursion OUTPUT: 3/6/2020 Compsci 201, Spring 2020 19

  20. Print the tree - Recursion OUTPUT: baboon chimp lemur macaque monkey orangutan tamarin 3/6/2020 Compsci 201, Spring 2020 45

  21. Print the tree - 1 2 Recursion 3 2 3 1 3/6/2020 Compsci 201, Spring 2020 46

  22. 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

  23. Just larger – PrintTree.java https://coursework.cs.duke.edu/201spring20/classcode 3/6/2020 Compsci 201, Spring 2020 48

  24. Visualize • A differ erent ent t tree: • Left subtree? • Right subtree? 3/6/2020 Compsci 201, Spring 2020 49

  25. Three recursive calls • "appl pple" n node de • null,null • Up to " "durian" ian" 3/6/2020 Compsci 201, Spring 2020 50

  26. 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

  27. 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

  28. 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

  29. 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

  30. 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

  31. 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

  32. WOTO http://b ://bit.l .ly/20 /201s 1spring2 g20-030 306-1 3/6/2020 Compsci 201, Spring 2020 60

  33. 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

  34. Not Everything is Comparable 3/6/2020 Compsci 201, Spring 2020 62

  35. 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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend