Compsci 201 Binary Trees StacksQueues Owen Astrachan olacs
Compsci 201 Binary Trees Stacks+Queues Owen Astrachan ola@cs. duke. edu October 26, 2018 10/26/2018 Compsci 201, Fall 2018, Trees + Stacks + Queues 1
P is for … • P 2 P or Peer-2 -Peer • From Bittorrent to the Internet to … • Password • From changeme to oh-oh • Phishing • From changeme to bitcoin! 10/26/2018 Compsci 201, Fall 2018, Trees + Stacks + Queues 2
Plan for Today • Practicing with Recursive code and Recurrences • Developing an understanding • Binary Trees in Tree. Set, Tree. Map, and more • DIYAD sets • Why java. util. Tree. Set uses a red-black tree • Stacks and Queues • Simple linear structures with many applications Compsci 201, Fall 2018, Trees + Stacks 10/26/2018 + Queues 3
Recurrence Relations No need to derive, remember or look up Recurrence Algorithm Solution T(n) = T(n/2) + O(1) binary search O(log n) T(n) = T(n-1) + O(1) sequential search O(n) T(n) = 2 T(n/2) + O(1) tree traversal T(n) = T(n/2) + O(n) quicksort partition T(n) = 2 T(n/2) + O(n) mergesort, quicksort T(n) = T(n-1) 10/26/2018 + O(n) selection or bubble sort Compsci 201, Fall 2018, Trees + Stacks + Queues O(n) O(n log n) O(n 2) 4
Standard Tree Traversals • Pre-, In-, and Post- order • When is root visited? Before, in-between, after • Analogy: traveling the border: down, under, up • https: //github. com/astrachano/classcode 201 fall 18 See Tree. Demo. java 10/26/2018 Compsci 201, Fall 2018, Trees + Stacks + Queues 5
Why different traversals? • Pre-order: reading and writing trees, e. g. , APTs • Read root value: recursively read subtrees • In-order: visit nodes "in order", search tree • All nodes less than root, all nodes greater • Post-order: destroy/free tree • Delete subtrees then delete root • Tough to delete root first … 10/26/2018 Compsci 201, Fall 2018, Trees + Stacks + Queues 6
Test and APT Practice • Create a new tree from a tree parameter • Same shape, but nodes labeled with tree-size • Must call new Tree. Node. With what values … 10/26/2018 Compsci 201, Fall 2018, Trees + Stacks + Queues 7
Standard Tree Recursion • Create labeled subtrees … via recursion • Label root/self, and return result • What is recurrence? 10/26/2018 Compsci 201, Fall 2018, Trees + Stacks + Queues 8
Developing Recurrence • T(n): runtime of count. Label on n-node tree • Lines 83 and 84: each are T(n/2) • Line 86: O(n)Recurrence: 2 T(n/2) + O(n) 10/26/2018 Compsci 201, Fall 2018, Trees + Stacks + Queues 9
O(n log n) intuition • Each node visits subtree, each node calls count • Deposit penny at each node when visited, e. g. , when count is called • Leaves get lots of $$, root has one cent, leaves get O(log n) cents • How many leaves? • Intuition + Recurrence 10/26/2018 Compsci 201, Fall 2018, Trees + Stacks + Queues 10
Bottom-up rather than top-down? • Recurse and use results stored in children • Leaf base-case not needed? 2 T(n/2) + O(1) 10/26/2018 Compsci 201, Fall 2018, Trees + Stacks + Queues 11
Adding into a Tree. Set • Traverse from root-to-leaf: < go left, > go right • But using. compare. To(. . ) for < and > “koala” “llama” • See http: //bit. ly/jdk-treemap 566 -575 “koala” “giraffe” • https: //github. com/astrachano/diyad-treestuff “tiger” • While loop on 68 -89“elephant” “koala” “jaguar” “monkey” “koala” “hippo” “leopard” “pig” “koala” 10/26/2018 Compsci 201, Fall 2018, Trees + Stacks + Queues 12
Using Tree. Node internally • Linked. List class used doubly-linked list • Tree. Set and Tree. Map use balanced tree • https: //github. com/astrachano/diyad-treestuff • Source code: Balanced Red/Black tree where nodes have left, right, parent pointer • http: //hg. openjdk. java. net/jdk 8/jdk/file/687 fd 7 c 7986 d/src/share/classes/java/util/Tree. Ma p. java 10/26/2018 Compsci 201, Fall 2018, Trees + Stacks + Queues 13
Balanced Trees • Current standard is red-black tree • Re-balance after each tree alteration, e. g. , add • Details can be found in books and online • Long ago standard? AVL tree, heightbalanced • https: //github. com/astrachano/diyad-treestuff • See AVLSet. java and BSTSet. java 10/26/2018 Compsci 201, Fall 2018, Trees + Stacks + Queues 14
WOTO http: //bit. ly/201 fall 18 -oct 26 -trees 10/26/2018 Compsci 201, Fall 2018, Trees + Stacks + Queues 15
Richard Stallman • Created "free" software foundation • Speech not beer • No Linux without gcc • Mac. Arthur 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. 10/26/2018 Compsci 201, Fall 2018, Trees + Stacks + Queues 16
Is Recursion Important? • • Solving problems is important Scaling solutions is important Knowledge of tools to solve and scale are … You'll be expected to understand recursion! • Interviews, later courses, fundamental Compsci • You can always solve a problem iteratively • May be less elegant, may be harder to code • But, the tool itself? …. 10/26/2018 Compsci 201, Fall 2018, Trees + Stacks + Queues 17
Time Changes Points of View We argue that the notion of self reference should permeate first courses in computer science. If this is to be the case such courses should take a view far broader than “Wow, I can average 10 numbers with the skills I learned in my first programming course!” Recursion is fundamental in computer science, whether understood as a mathematical concept, a programming technique, a way of expressing an algorithm, or a problemsolving approach. It is too important and too valuable to be belittled by showing a recursive factorial function in CS 1, which conveys almost nothing of its power https: //scholars. duke. edu/display/pub 758401 Self Reference is an Illustrative Essential 10/26/2018 Compsci 201, Fall 2018, Trees + Stacks + Queues 18
Linear Data Structures • Back in the day … weeks on these. Now? • Stack: LIFO, Queue: FIFO • We can use standard java. util classes 10/26/2018 Compsci 201, Fall 2018, Trees + Stacks + Queues 19
https: //www. google. com/search? q=stack&source=lnms&tbm=isch 10/26/2018 Compsci 201, Fall 2018, Trees + Stacks + Queues 20
https: //www. google. com/search? q=queue&source=lnms&tbm=isch 10/26/2018 Compsci 201, Fall 2018, Trees + Stacks + Queues 21
Tree Traversal • How do we visit nodes in level-order? • See Tree. Demo. java 10/26/2018 Compsci 201, Fall 2018, Trees + Stacks + Queues 22
Queue: FIFO in Java • Scheduling: processes on CPU, events in simulation • O(1) add (to back) and remove (from front) • Queue<> is interface, implement Linked. List<> • Use add(), peek(), remove() 10/26/2018 Compsci 201, Fall 2018, Trees + Stacks + Queues 23
Stack: LIFO in Java • Often used in place of recursion: runtime stack • Each method "stacks" on top of other calls • Postfix notation: 3 + 5 * 7 and 5 7 * 3 + • Sequentially scan expression left to right • Number? Push/add • Operand? Pop/remove, push result • Stack in Java like wrapper over Array. List • Use s. pop() not s. remove(s. size()-1) 10/26/2018 Compsci 201, Fall 2018, Trees + Stacks + Queues 24
Stack Code • See Tree. Iterator in Tree. Demo to find successor • https: //github. com/astrachano/classcode 201 fall 18 • O(1) add at end and remove from end: LIFO • Array. List: push and pop are veneer 10/26/2018 Compsci 201, Fall 2018, Trees + Stacks + Queues 25
The Word. Ladder Story • Listening to radio on way to work: '90 s • Make a ladder from "white" to "house" • white, while, whale, shale, … louse, house! • I think: I can do that optimally! With a Queue! • Guaranteed shortest ladder • Call my brother, the English major • An hour later we call each other back 10/26/2018 Compsci 201, Fall 2018, Trees + Stacks + Queues 26
The Callback • Optimal? What about the word "sough" I have a 15 word ladder 10/26/2018 Compsci 201, Fall 2018, Trees + Stacks + Queues I have a 16 word ladder 27
Computer Science and English • Adding 'sough' to dictionary: 14 word ladder • This becomes the basis for an assignment 10/26/2018 Compsci 201, Fall 2018, Trees + Stacks + Queues 28
Midterm Study and Practice • See practice midterm • Answers at discussion, some map questions • Possible review sessions for those … • Update on schedule • Logistics of taking exam: notes, turn in and more • SDAO and all students, STINF? Just say no? 10/26/2018 Compsci 201, Fall 2018, Trees + Stacks + Queues 29
- Slides: 29