360 likes | 378 Views
Explore the intricacies of binary trees, balanced trees, and the tradeoffs involving insertion and removal. Dive into the powerful world of recursive algorithms, search trees, and shortest paths with queues. Strengthen your understanding of data structures and algorithm design.
E N D
Compsci 201Trees and Tradeoffs Owen Astrachan Jeff Forbes November 3, 2017 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 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 Compsci 201, Fall 2017, Tree and Tradoffs
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 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! https://coursework.cs.duke.edu/201fall17/setstuff Compsci 201, Fall 2017, Tree and Tradoffs
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; } Compsci 201, Fall 2017, Tree and Tradoffs
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 https://coursework.cs.duke.edu/201fall17/d9-linked-trees/blob/master/src/TreePlay.java 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?) Compsci 201, Fall 2017, Tree and Tradoffs
Inorder w/o Recursion publicvoidinOrderStack(TreeNoderoot) { Stack<TreeNode> stack = new Stack<>(); TreeNodecurrent = 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; } } } Compsci 201, Fall 2017, Tree and Tradoffs
Aside: WordladderStory • 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 Compsci 201, Fall 2017, Tree and Tradoffs
Queue for shortest path public booleanfindLadder(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; } Compsci 201, Fall 2017, Tree and Tradoffs
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. 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 Compsci 201, Fall 2017, Tree and Tradoffs
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") Compsci 201, Fall 2017, Tree and Tradoffs
Reconstructing Word Ladder • Run WordLaddersFull • https://coursework.cs.duke.edu/201fall17/wordladders/blob/master/src/WordLaddersFull.java • 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? Compsci 201, Fall 2017, Tree and Tradoffs
BlobModel Revisited • Counting “blobs” using techniques from depth-first search and percolation • https://coursework.cs.duke.edu/201fall17/blobfill • 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 Compsci 201, Fall 2017, Tree and Tradoffs
Mind your Stacks and Queues http://bit.ly/201fal17-nov3-sq Compsci 201, Fall 2017, Tree and Tradoffs
Jan Cuny Program officer at National Science Foundation (NSF) Leading #CSforAll initiatives. 2009 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.” 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) Compsci 201, Fall 2017, Tree and Tradoffs
Thinking about Trees • LeafSum • http://www.cs.duke.edu/csed/newapt/leafsum.html • HeightLabel • http://www.cs.duke.edu/csed/newapt/heightlabel.html • LevelLabel • http://www.cs.duke.edu/csed/newapt/levellabel.html • What to do in base case: null (and leaf node) • Combining results of recursive calls Compsci 201, Fall 2017, Tree and Tradoffs
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 Compsci 201, Fall 2017, Tree and Tradoffs
Is Recursion Important? • Solving problems is important • Scaling solutions is important • Knowledge of tools that can be applied is important • The tool itself? …. Compsci 201, Fall 2017, Tree and Tradoffs
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 https://scholars.duke.edu/display/pub758401 Self Reference is an Illustrative Essential Compsci 201, Fall 2017, Tree and Tradoffs
TIL Compsci 201, Fall 2017, Tree and Tradoffs
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
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? publicclassLeafSum { publicint sum(TreeNodet) { if (t== null) return 0; // something is missing here! return sum(t.left) + sum(t.right); } } 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! publicclassLeafSum { publicint sum(TreeNodet) { if (t== null) return 0; if (t.left== null && t.right == null) return1; returnsum(t.left) + sum(t.right); } } Compsci 201, Fall 2017, Tree and Tradoffs
Height Label: Call height • What is/are base cases? • http://www.cs.duke.edu/csed/newapt/heightlabel.html • Do we know how to determine height? • Create new nodes: info, left, right • What will recurrence be, similar to … 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, … Compsci 201, Fall 2017, Tree and Tradoffs
Toward IsBalanced booleanisBalanced(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! Compsci 201, Fall 2017, Tree and Tradoffs
Isomorphic trees • http://www.cs.duke.edu/csed/newapt/isomorphictrees.html • Two parameters, what are base cases? • If both s == null && t == null means …. • If one, not both that means … • sameShape(t.left,s.left) && … Compsci 201, Fall 2017, Tree and Tradoffs
Height Label all Green publicTreeNode rewire(TreeNodet) { if(t == null) returnnull; returnnewTreeNode(height(t), rewire(t.left), rewire(t.right)); } privateint height(TreeNodet) { if (t== null) return 0; return1 + Math.max(height(t.left), height(t.right)); } T(N) = 2T(N/2) + O(N) or .. T(N) = T(N-1) + O(N) Compsci 201, Fall 2017, Tree and Tradoffs
YATP • http://www.cs.duke.edu/csed/newapt/isomorphictrees.html • Do two trees have the same shape? • If they are both empty/null they do • If left subtrees same? If right subtrees same? Compsci 201, Fall 2017, Tree and Tradoffs
Quasi-Isomorphic • http://www.cs.duke.edu/csed/newapt/quasitrees.html • 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 Compsci 201, Fall 2017, Tree and Tradoffs
What are all the calls for? public String sameShape(TreeNodes, TreeNodet) { 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"same"; return"different"; } Compsci 201, Fall 2017, Tree and Tradoffs
WOTO http://bit.ly/201fall17-nov3-trees Compsci 201, Fall 2017, Tree and Tradoffs