Download presentation
Presentation is loading. Please wait.
1
Compsci 201 Trees and Tradeoffs
Owen Astrachan Jeff Forbes November 3, 2017 11/3/17 Compsci 201, Fall 2017, Tree and Tradoffs
2
Compsci 201, Fall 2017, Tree and Tradoffs
S is for … Stack Last in, First Out, source of overflow! Software Joys and sorrows, eating the world SQL Structured Query Language System The .in’s and .out’s of Java 11/3/17 Compsci 201, Fall 2017, Tree and Tradoffs
3
Compsci 201, Fall 2017, Tree and Tradoffs
Plan for the Day Trees: Tradeoffs, In-Practice, In-Theory Along the way we’ll revisit Stacks and Queues Thinking Recursively Developing recursive algorithms Reasoning about recursive algorithms Midterm and APT Quiz information 11/3/17 Compsci 201, Fall 2017, Tree and Tradoffs
4
Why do we use search trees?
Fast insertion, removal, search: O(log n) What is n? When do trees “go bad” and what do we do? Range queries are also important All m values between low..high: O(m log n) Can’t do that with a HashSet/HashMap 11/3/17 Compsci 201, Fall 2017, Tree and Tradoffs
5
Compsci 201, Fall 2017, Tree and Tradoffs
Balanced Trees In the 1960’s, Adelson-Velsky and Landis: AVL O(log n) worst-case, because tree is rebalanced as needed In 1978, Guibas and Sedgewick: Red-Black tree Sedgewick continues to improve this! 11/3/17 Compsci 201, Fall 2017, Tree and Tradoffs
6
What does insertion look like?
Simple recursive insertion into tree (accessed by root) root = insert("foo", root); TreeNode insert(TreeNode t, String s) { if (t == null) t = new Tree(s,null,null); else if (s.compareTo(t.info) <= 0) t.left = insert(t.left,s); else t.right = insert(t.right,s); return t; } 11/3/17 Compsci 201, Fall 2017, Tree and Tradoffs
7
Notes on tree insert and search
In each recursive insert call Tree parameter in call is either the left or right field of some node in the original tree Will be assignment to a .left or .right field! t.left = treeMethod(t.left,…) Common idiom: modify and assign on return 11/3/17 Compsci 201, Fall 2017, Tree and Tradoffs
8
Compsci 201, Fall 2017, Tree and Tradoffs
Insert and Removal For insertion we can use iteration (see BSTSet) Traverse left or right and “look ahead” to add Removal has details, depends on # of children Straightforward when zero or one child Complicated when two children, find successor See set code for complete cases If right child, straightforward Otherwise find node that’s left child of its parent (why?) 11/3/17 Compsci 201, Fall 2017, Tree and Tradoffs
9
Compsci 201, Fall 2017, Tree and Tradoffs
Inorder w/o Recursion public void inOrderStack(TreeNode root) { Stack<TreeNode> stack = new Stack<>(); TreeNode current = root; while (!stack.empty() || current != null) { if (current != null) { stack.push(current); current = current.left; } else { current = stack.pop(); System.out.println(current.info); current = current.right; 11/3/17 Compsci 201, Fall 2017, Tree and Tradoffs
10
Aside: Wordladder Story
Ladder from ‘white’ to ‘house’ white, while, whale, shale, … I can do that… optimally My brother was an English major My ladder is 16, his is 15, how? There's a ladder that's 14 words! The key is ‘sough’ Guarantee optimality! QUEUE I heard the puzzle on the way into Duke in the mid 90's. I called my brother and said I'd solve the problem. He said he would too. He used his brain, I wrote a program. Called him an hour alter and said "got it". He said "got it". I said my code was provably optimal and had a 16-word ladder. He said he had 15. WHAT? I added "sough" to dictionary and got a 14-word ladder 11/3/17 Compsci 201, Fall 2017, Tree and Tradoffs
11
Queue for shortest path
public boolean findLadder(String[] words, String first, String last){ Queue<String> qu = new LinkedList<>(); Set<String> set = new HashSet<>(); qu.add(first); while (qu.size() > 0){ String current = qu.remove(); if (oneAway(current,last)) return true; for(String s : words){ if (! set.contains(s) && oneAway(from,s)){ qu.add(s); set.(s); } } return false; 11/3/17 Compsci 201, Fall 2017, Tree and Tradoffs
12
Shortest Path reprised
How does Queue ensure we find shortest path? Where are words one away from first? Where are words two away from first? We use a set to avoid visiting the same word: hot-dot-dog-dot-hot-hog-dog-dot … What's path from white to house? We know there is one. All words one-away from first are on Q and are added when first is dequeued/removed first time through loop. Each of these 1-away-from-start words comes off the queue and words that are one-away from these, or 2-away from start, are put on the Q. BUT it's a queue so all 2-away words go on after the 1-away. In general, we remove N-away words before (N+1)-away words because of FIFO structure 11/3/17 Compsci 201, Fall 2017, Tree and Tradoffs
13
Compsci 201, Fall 2017, Tree and Tradoffs
Shortest path proof Just as we visited root before any trees in the LevelOrder Traversal We visit all words one-away from start before all words two-away We can combine queue with map to reconstruct the ladder (Key,Value): value is word, key is what caused it to be put onto the queue Don't dwell on this, but walk through it as a semi-formal argument, akin to a hand-wavy proof 11/3/17 Compsci 201, Fall 2017, Tree and Tradoffs
14
Keeping track of ladder
Find w, a one-away word from current Enqueue w if not seen Call map.put(w,current) Remember keys are unique! Put word on queue once! map.put("lot", "hot") map.put("dot", "hot") map.put("hat", "hot") Remind students that we need to keep track of how words are connected to reconstruct the ladder. Keys in a map are unique, but we only put a word on the queue once! Why? Using a set. We can make this one time the time we make a word the key in a map. The value is what caused the word to be put onto the queue, see examples 11/3/17 Compsci 201, Fall 2017, Tree and Tradoffs
15
Reconstructing Word Ladder
Run WordLaddersFull See map and call to map.put(word,current) What about when returning the ladder, why is the returned ladder in reverse order? What do we know about code when statement adding (key,value) to map runs? Run program with "white" "house" and with "voter" "crazy" and with "voter" "smart" 11/3/17 Compsci 201, Fall 2017, Tree and Tradoffs
16
Compsci 201, Fall 2017, Tree and Tradoffs
BlobModel Revisited Counting “blobs” using techniques from depth-first search and percolation Rather than using recursion to visit all neighbors and find size of blob, use Queue of cells to manage the neighbors Recursion is a stack-based model This is a queue-based model 11/3/17 Compsci 201, Fall 2017, Tree and Tradoffs
17
Mind your Stacks and Queues
11/3/17 Compsci 201, Fall 2017, Tree and Tradoffs
18
Compsci 201, Fall 2017, Tree and Tradoffs
Jan Cuny Program officer at National Science Foundation (NSF) Leading #CSforAll initiatives ABI Woman of Vision Award for Social Impact, 2016 Distinguished Educator Award “All of toady’s kids will need – along with reading, writing, and arithmetic – a basic understanding of computation and the role it plays across a wide range of disciplines.” 11/3/17 Compsci 201, Fall 2017, Tree and Tradoffs
19
Compsci 201, Fall 2017, Tree and Tradoffs
Midterm and APT Quiz Practice midterm available Focus of discussion section Practice APT quiz available Linked List question Tree Question Other question (linked list likely) 11/3/17 Compsci 201, Fall 2017, Tree and Tradoffs
20
Compsci 201, Fall 2017, Tree and Tradoffs
Thinking about Trees LeafSum HeightLabel LevelLabel What to do in base case: null (and leaf node) Combining results of recursive calls 11/3/17 Compsci 201, Fall 2017, Tree and Tradoffs
21
Developing Recursive Ideas
When possible have the recursive calls mirror the structure of the problem: trees/lists are easy! Store the result(s) of all call(s), use the results to return a value: call, store, compute, return Sometimes a helper function with extra parameter is a good idea: height-label and level-label 11/3/17 Compsci 201, Fall 2017, Tree and Tradoffs
22
Is Recursion Important?
Solving problems is important Scaling solutions is important Knowledge of tools that can be applied is important The tool itself? …. 11/3/17 Compsci 201, Fall 2017, Tree and Tradoffs
23
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 problem-solving approach. It is too important and too valuable to be belittled by showing a recursive factorial function in CS1, which conveys almost nothing of its power Self Reference is an Illustrative Essential 11/3/17 Compsci 201, Fall 2017, Tree and Tradoffs
24
Compsci 201, Fall 2017, Tree and Tradoffs
TIL 11/3/17 Compsci 201, Fall 2017, Tree and Tradoffs
25
Leafsum Sum all the values in leaves of tree Base cases?
Recursive calls? What should big-Oh be? N-node tree? Visit each node …. Balanced or stringy … Similar to tree height
26
Compsci 201, Fall 2017, Tree and Tradoffs
LeafSum correct? What do we do with null tree? Why? What value will always be returned? Why? From base-case to combining recursive calls Recurrence expression? public class LeafSum { public int sum(TreeNode t) { if (t == null) return 0; // something is missing here! return sum(t.left) + sum(t.right); } 11/3/17 Compsci 201, Fall 2017, Tree and Tradoffs
27
Compsci 201, Fall 2017, Tree and Tradoffs
LeafSum Now Correct! What do we do with a leaf, why? Why else isn’t needed, but ok Why this is still O(n) for ALL TREES! public class LeafSum { public int sum(TreeNode t) { if (t == null) return 0; if (t.left == null && t.right == null) return 1; return sum(t.left) + sum(t.right); } 11/3/17 Compsci 201, Fall 2017, Tree and Tradoffs
28
Height Label: Call height
What is/are base cases? Do we know how to determine height? Create new nodes: info, left, right What will recurrence be, similar to … 11/3/17 Compsci 201, Fall 2017, Tree and Tradoffs
29
Compsci 201, Fall 2017, Tree and Tradoffs
Height and IsBalanced int height(Tree root) { if (root == null) return 0; else { return 1 + Math.max(height(root.left), height(root.right)); } T(n) = 2T(n/2) + O(1) Visit both children Each visits both children, … 11/3/17 Compsci 201, Fall 2017, Tree and Tradoffs
30
Compsci 201, Fall 2017, Tree and Tradoffs
Toward IsBalanced boolean isBalanced(Tree root){ if (root == null) return true; return isBalanced(root.left) && isBalanced(root.right) && Math.abs(height(root.left)–height(root.right)) <= 1; } T(n) = 2T(n/2) + O(n) Visit both children Each determines height! 3 gets height:4,1,7 then children! 4 gets height, 1,7 then children! 11/3/17 Compsci 201, Fall 2017, Tree and Tradoffs
31
Compsci 201, Fall 2017, Tree and Tradoffs
Isomorphic trees Two parameters, what are base cases? If both s == null && t == null means …. If one, not both that means … sameShape(t.left,s.left) && … 11/3/17 Compsci 201, Fall 2017, Tree and Tradoffs
32
Compsci 201, Fall 2017, Tree and Tradoffs
Height Label all Green public TreeNode rewire(TreeNode t) { if (t == null) return null; return new TreeNode(height(t), rewire(t.left), rewire(t.right)); } private int height(TreeNode t) { if (t == null) return 0; return 1 + Math.max(height(t.left), height(t.right)); T(N) = 2T(N/2) + O(N) or .. T(N) = T(N-1) + O(N) 11/3/17 Compsci 201, Fall 2017, Tree and Tradoffs
33
Compsci 201, Fall 2017, Tree and Tradoffs
YATP Do two trees have the same shape? If they are both empty/null they do If left subtrees same? If right subtrees same? 11/3/17 Compsci 201, Fall 2017, Tree and Tradoffs
34
Compsci 201, Fall 2017, Tree and Tradoffs
Quasi-Isomorphic Base cases are similar: both null means … If left same as right and right same as left? If left same as left and right same as right? How many recursive calls 11/3/17 Compsci 201, Fall 2017, Tree and Tradoffs
35
What are all the calls for?
public String sameShape(TreeNode s, TreeNode t) { if (s == null && t == null) return "same"; if (s == null || t == null) return "different"; if ("same".equals(sameShape(s.left, t.left)) && "same".equals(sameShape(s.right,t.right))) return "same"; if ("same".equals(sameShape(s.left, t.right)) && "same".equals(sameShape(s.right, t.left))) return "different"; } 11/3/17 Compsci 201, Fall 2017, Tree and Tradoffs
36
Compsci 201, Fall 2017, Tree and Tradoffs
WOTO 11/3/17 Compsci 201, Fall 2017, Tree and Tradoffs
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.