From Doubly-Linked Lists to Binary Trees

From Doubly-Linked Lists to Binary Trees

Binary Trees From doubly-linked lists to binary trees Linked lists: efficient insertion/deletion, inefficient search Instead of using prev and next to point to a linear ArrayList: search can be efficient, insertion/deletion not arrangement, use them to divide the universe in half Similar to binary search, everything less goes left, everything greater goes right Binary trees: efficient insertion, deletion, and search “koala” “llama” trees used in many contexts, not just for searching, e.g., “koala” expression trees How do we search? “giraffe” “tiger” “koala” search in O(log n) like sorted array How do we insert? “elephant” “jaguar” “monkey” insertion/deletion O(1) like list, once location found! “koala” binary trees are inherently recursive, difficult to process “hippo” “leopard” “pig” trees non-recursively, but possible • recursion never required, often makes coding simpler “koala” CPS 100 6.1 CPS 100 6.2 Basic tree definitions A TreeNode by any other name… Binary tree is a structure: What does this look like? empty What does the picture look like? root node with left and right subtrees terminology: parent, children, leaf node, internal node, depth, “llama” height, path public class TreeNode { • link from node N to M then N is parent of M “giraffe” “tiger” TreeNode left; – M is child of N A • leaf node has no children TreeNode right; – internal node has 1 or 2 children B C String info; TreeNode(String s, • path is sequence of nodes, N1, N2, … Nk D E F TreeNode llink, TreeNode rlink){ – Ni is parent of Ni+1 info = s; – sometimes edge instead of node G • depth (level) of node: length of root-to-node path left = llink; – level of root is 1 (measured in nodes) right = rlink; • height of node: length of longest node-to-leaf path } – height of tree is height of root } CPS 100 6.3 CPS 100 6.4 Printing a search tree in order Insertion and Find? Complexity? When is root printed? How do we search for a value in a tree, starting at root? After left subtree, before right subtree. Can do this both iteratively and recursively, contrast to printing which is very difficult to do iteratively void visit(TreeNode t) How is insertion similar to search? { if (t != null) { visit(t.left); What is complexity of print? Of insertion? System.out.println(t.info); visit(t.right); Is there a worst case for trees? } “llama” Do we use best case? Worst case? Average case? } “giraffe” “tiger” How do we define worst and average cases “elephant” “jaguar” “monkey” For trees? For vectors? For linked lists? For arrays of Inorder traversal linked-lists? “hippo” “leopard” “pig” CPS 100 6.5 CPS 100 6.6 See SetTiming code What does contains look like? What about ISimpleSet interface public boolean contains(E element) { How does this compare to java.util? return myList.indexOf(element) >= 0; } Why are we looking at this, what about Java source? public boolean contains(E element){ How would we implement most simply? returns contains(myHead, element); } What are complexity repercussions: add, contains private boolean contains(Node list, E element) { What about iterating? if (list == null) return false; if (list.info.equals(element)) return true; What would linked list get us? Scenarios where better? return contains(list.next,element); Consider N adds and M contains operations } Move to front heuristic? Why is there a private, helper method? What will be different about Tree? CPS 100 6.7 CPS 100 6.8 What does contains look like? What does insertion look like? public boolean contains(E element){ Simple recursive insertion into tree (accessed by root) returns contains(myRoot, element); } root = insert("foo", root); private boolean contains(TreeNode root, E element) { public TreeNode insert(TreeNode t, String s) { if (root == null) return false; if (t == null) t = new Tree(s,null,null); else if (s.compareTo(t.info) <= 0) if (list.info.equals(element)) return true; t.left = insert(t.left,s); if (element.compareTo(root.info) <= 0){ else t.right = insert(t.right,s); return contains(root.left,element); return t; else } return contains(root.right,element); Note: in each recursive call, the parameter t in the called clone is } either the left or right pointer of some node in the original tree What is recurrence? Complexity? Why is this important? When good trees go bad, how can this happen? Why must the idiom t = treeMethod(t,…) be used? CPS 100 6.9 CPS 100 6.10 Removal from tree? Implementing binary trees For insertion we can use iteration (see BSTSet) Trees can have many shapes: short/bushy, long/stringy h Look below, either left or right if height is h, number of nodes is between h and 2 -1 • If null, stop and add single node tree: height = 1, if height = 3 • Otherwise go left when <=, else go right when > Removal is tricky, depends on number of children Straightforward when zero or one child Java implementation, similar to doubly-linked list Complicated when two children, find successor public class Tree • See set code for complete cases { String info; • If right child, straightforward TreeNode left; • Otherwise find node that’s left child of its parent (why?) TreeNode right; TreeNode(String s, TreeNode llink, TreeNode rlink){ info = s; left = llink; right = rlink; } }; CPS 100 6.11 CPS 100 6.12 Tree functions Tree traversals Compute height of a tree, what is complexity? Different traversals useful in different contexts Inorder prints search tree in order int height(Tree root) { • Visit left-subtree, process root, visit right-subtree if (root == null) return 0; else { Preorder useful for reading/writing trees return 1 + Math.max(height(root.left), height(root.right) ); • Process root, visit left-subtree, visit right-subtree } } Postorder useful for destroying trees Modify function to compute number of nodes in a tree, does • Visit left-subtree, visit right-subtree, process root complexity change? “llama” What about computing number of leaf nodes? “giraffe” “tiger” “elephant” “jaguar” “monkey” CPS 100 6.13 CPS 100 6.14 Balanced Trees and Complexity What is complexity? A tree is height-balanced if Assume trees are “balanced” in analyzing complexity Left and right subtrees are height-balanced Roughly half the nodes in each subtree Left and right heights differ by at most one Leads to easier analysis How to develop recurrence relation? What is T(n)? What other work is done? boolean isBalanced(Tree root) { if (root == null) return true; return How to solve recurrence relation isBalanced(root.left) && isBalanced(root.right) && Plug, expand, plug, expand, find pattern Math.abs(height(root.left) – height(root.right)) <= 1; } A real proof requires induction to verify correctness } CPS 100 6.15 CPS 100 6.16 Danny Hillis Searching, Maps,Tries (hashing) The third culture consists of those scientists and Searching is a fundamentally important operation other thinkers in the empirical world who, through their work and expository writing, are We want to search quickly, very very quickly taking the place of the traditional intellectual in Consider searching using Google, ACES, issues? rendering visible the deeper meanings of our lives, redefining who and what we are. In general we want to search in a collection for a key (Wired 1998) And now we are beginning to depend on computers to help us evolve new We've searched using trees and arrays computers that let us produce things of much Tree implementation was quick: O(log n) worst/average? greater complexity. Yet we don't quite understand the process - it's getting ahead of us. Arrays: access is O(1), search is slower We're now using programs to make much faster computers so the process can run much faster. If we compare keys, log n is best for searching n elements That's what's so confusing - technologies are feeding Lower bound is Ω(log n), provable back on themselves; we're taking off. We're at that point analogous to when single-celled organisms were turning Hashing is O(1) on average, not a contradiction, why? into multicelled organisms. We are amoebas and we can't Tries are O(1) worst-case!! (ignoring length of key) figure out what the hell this thing is that we're creating. CPS 100 6.17 CPS 100 6.18 From Google to Maps Interface at work: MapDemo.java If we wanted to write a search engine we’d need to access lots Key is a string, Value is # occurrences of pages and keep lots of data Code below shows how Map interface/classes work Given a word, on what pages does it appear? This is a map of words->web pages while (it.hasNext()) { String s = it.next();. In general a map associates a key with a value Counter c = map.get(s); Look up the key in the map, get the value if (c != null) c.increment(); Google: key is word/words, value is list of web pages else map.put(s, new Counter()); Anagram: key is string, value is words that are anagrams } What clues are there for prototype of map.get and map.put? Interface issues What if a key is not in map, what value returned? Lookup a key, return boolean: in map or value: associated What kind of objects can be put in a map? with the key (what if key not in map?) Insert a key/value pair into the map CPS 100 6.19 CPS 100 6.20 Replacing Counter with Integer Getting keys and values from a map With autoboxing (and unboxing) do we need class Counter? Access every key in the map, then get the corresponding value What if we access a key that’s not there? Get an iterator of the set of keys: keySet().iterator() while (it.hasNext()) { For each key returned by this iterator call map.get(key) String s = it.next();.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    9 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us