Upload
others
View
3
Download
0
Embed Size (px)
Citation preview
Compsci 201Binary Trees
Dna Linked Assignment
Owen Astrachan [email protected] 19, 2018
10/17/2018 Compsci 201, Fall 2018, Recursion + Binary Trees 1
N is for …
• new• Allocating memory from the heap
• null• Value when nothing has been allocated
• next• Iterating if hasNext
10/18/17 Compsci 201, Fall 2017, Linked 2
Plan for Today
• Binary Trees, Binary Search Trees• Best of arrays and linked lists?• O(1) insert, O(log n) search
• Recursion will be indispensable
• DNA Linked Assignment• Conceptual overview
• Guest Speaker: Ge Wang
10/17/2018 Compsci 201, Fall 2018, Recursion + Binary Trees 3
Best of Both Worlds
• With arrays we can use binary search• This is O(log N), that’s really, really fast• Remember that 210 = 1024 so …
• With linked lists we can add/remove quickly• Cannot search, cannot index, can relink
• Can we get fast search and fast add/remove?
10/17/2018 Compsci 201, Fall 2018, Recursion + Binary Trees 4
Binary Search Trees
• Nodes have left/right references: similar prev/next• At each node: <= goes left, > goes right
• How do we search?• How do we insert?
• Insert: “koala”
�llama�
�tiger�
�monkey��jaguar��elephant�
�giraffe�
�pig��hippo� �leopard�
�koala�
�koala�
�koala�
�koala�
�koala�
10/17/2018 Compsci 201, Fall 2018, Recursion + Binary Trees 5
Tree Terminology
• Root: "top node", has no parent
• "macaque". Subtrees also have a root
• Leaf: bottom nodes, have no children
• "baboon", "lemur", "organutan"
• Path: sequence of parent-child nodes
• "macaque", "chimp", "lemur"
10/17/2018Compsci 201, Fall 2018, Recursion +
Binary Trees6
A TreeNode by any other name…• What does this look like? Doubly linked list?
public class TreeNode{
TreeNode left;TreeNode right;String info;TreeNode(String s,
TreeNode llink, TreeNode rlink){info = s;left = llink;right = rlink;
}}
�llama�
�tiger��giraffe�
10/17/2018Compsci 201, Fall 2018, Recursion +
Binary Trees7
Trees: Concepts and Code
• In a search tree: property holds at every node
• Nodes in left subtree are < (or <=)
• Nodes in right subtree are >
• To search or add: if not found?
• Look left if <=
• Look right if >
• Iterative or recursive
10/17/2018Compsci 201, Fall 2018, Recursion +
Binary Trees8
all values < 7
all values > 7
7
Tree Performance
• Seasrch for any value. Compare to root and …• Similar to binary search. O(log N) if tree "good"
• Trees are generally well-behaved, but !!!• Guarantee? Balanced tree: AVL or Red-Black
• We get O(log N) search and …• No shifting to add, find leaf
10/17/2018 Compsci 201, Fall 2018, Recursion + Binary Trees 9
all values < 7
all values > 7
7
Tree Algorithms/Methods
• Think of search trees as recursive/hierarchical
• Node with two subtrees
• What do we know about subtrees? Also tree!
10/17/2018Compsci 201, Fall 2018, Recursion +
Binary Trees10
Height of a tree• Longest root-to-leaf path (# nodes): 5,4,3 below
10/17/2018 Compsci 201, Fall 2018, Recursion + Binary Trees 11
Tree function: Tree height
• Compute tree height (longest root-to-leaf path)
int height(Tree root) {if (root == null) return 0;else {return 1 + Math.max(height(root.left),
height(root.right));}
}
• Find height of left subtree, height of right subtree
• Use results to determine height of tree
10/17/2018Compsci 201, Fall 2018, Recursion +
Binary Trees12
Interfaces and Inheritance
• EfficientMarkov extends BaseMarkov• Inherits method getRandomText()• This works the same in both classes!
• Method .getRandomText() uses myRandom• Must be accessible in BaseMarkov!
• Don't shadow state/instance variables
10/17/2018Compsci 201, Fall 2018, Recursion +
Binary Trees13
EfficientMarkov em = new EfficientMarkov();em.setTraining("hello new world");String st = em.getRandomText();// what getFollows() is called
Interfaces
• Specify method headers/signatures
• See MarkovInterface for examples
• BaseMarkov implements MarkovInterface
• Can also inherit default implementations
• Cannot have state in an interface
• myRandom and myText in Markov Assignment
• Thus use BaseMarkov.getRandomText()
10/17/2018Compsci 201, Fall 2018, Recursion +
Binary Trees14
From Links to …
11/1/17 Compsci 201, Fall 2017, Tree, Tries, Tradoffs 15
• What is the DNA/LinkStrand assignment about?• Why do we study linked lists• How do you work in a group?
From PCR to linked lists
• Polymerase Chain Reaction
• Make copies of a specific DNA segment
• Recombinant DNA
• Insert DNA using restriction enzymes
• Loosely forms basis for DNA/Linked assignment
• Big gains in efficiency using Linked Lists
• Compare to array of chars, e.g. Strings
10/17/2018Compsci 201, Fall 2018, Recursion +
Binary Trees16
Basics of cutAndSplice
• Find enzyme like ‘gat’
• Replace with splicee like ‘gggtttaaa’
• Strings and StringBuilder for creating new strings
• Complexity of “hello” + “world”, or A+B
• String: A + B, StringBuilder: B
11/1/17Compsci 201, Fall 2017, Tree, Tries,
Tradoffs17
What do linked lists get us?
• Faster run-time, much better use of memory
• We splice in constant time? Re-use strings
• Same as previous slide: sequential char view
11/1/17Compsci 201, Fall 2017, Tree, Tries,
Tradoffs18
String Concatenation Examined
• See StringPlay.java• https://github.com/astrachano/classcode201fall18
• Runtime of stringConcat(“hello”,N)• Depends on size of ret, 5, 10, 15, … 5*N
• 5(1 + 2 + … + N) which is O(N2)
11/1/17Compsci 201, Fall 2017, Tree, Tries,
Tradoffs19
public String stringConcat(String s, int reps) {String ret = "";for(int k=0; k < reps; k++) {
ret += s;}return ret;
}
StringBuilder Examined
• Just say no to quadratic, use StringBuilder
• String is immutable, StringBuilder is not
• Runtime of builderConcat(“hello”,N)• 5 + 5 + 5 + … + 5 a total of N times: O(N)
11/1/17Compsci 201, Fall 2017, Tree, Tries,
Tradoffs20
public String builderConcat(String s, int reps) {StringBuilder ret = new StringBuilder();for(int k=0; k < reps; k++) {
ret.append(s);}return ret.toString();
}
linked list improvement: memory
• Suppose we have B "gat" (blue), in strand size N
• Inserting size S "gggtttaaa" (green) splicees
• For String/StringBuilder: memory: B*S (+ N)
• For LinkedList: memory: S (re-use green!) (+ N)
10/17/2018Compsci 201, Fall 2018, Recursion +
Binary Trees21
linked list improvement: time
• Suppose we have B "gat" (blue), in strand size N
• Inserting size S "gggtttaaa" (green) splicees
• For String: time: B2S + N, builder: BS + N
• For LinkedList: B + N
10/17/2018Compsci 201, Fall 2018, Recursion +
Binary Trees22
Theory and Practice
• The JVM can sometimes optimize your code• Don’t optimize what you don’t have to …• http://www.pellegrino.link/2015/08/22/string-
concatenation-with-java-8.html
• Timings with System.nanoTime() are suspect• Other things going on in computer• JVM can go into garbage-collection mode• Other considerations
11/1/17 Compsci 201, Fall 2017, Tree, Tries, Tradoffs 23
WOTO
http://bit.ly/201fall18-oct19-1
11/1/17 Compsci 201, Fall 2017, Tree, Tries, Tradoffs 24
Guest Speaker
10/17/2018Compsci 201, Fall 2018, Recursion +
Binary Trees25
https://en.wikipedia.org/wiki/Ge_Wang