compsci201 binary trees dnalinked assignment · binary search trees ... •similar to binary...
TRANSCRIPT
![Page 1: Compsci201 Binary Trees DnaLinked Assignment · Binary Search Trees ... •Similar to binary search. O(log N) if tree "good" •Trees are generally well-behaved, but !!! •Guarantee?](https://reader036.vdocuments.mx/reader036/viewer/2022063016/5fd5b17e44f13e6c110a4c5a/html5/thumbnails/1.jpg)
Compsci 201Binary Trees
Dna Linked Assignment
Owen Astrachan [email protected] 19, 2018
10/17/2018 Compsci 201, Fall 2018, Recursion + Binary Trees 1
![Page 2: Compsci201 Binary Trees DnaLinked Assignment · Binary Search Trees ... •Similar to binary search. O(log N) if tree "good" •Trees are generally well-behaved, but !!! •Guarantee?](https://reader036.vdocuments.mx/reader036/viewer/2022063016/5fd5b17e44f13e6c110a4c5a/html5/thumbnails/2.jpg)
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
![Page 3: Compsci201 Binary Trees DnaLinked Assignment · Binary Search Trees ... •Similar to binary search. O(log N) if tree "good" •Trees are generally well-behaved, but !!! •Guarantee?](https://reader036.vdocuments.mx/reader036/viewer/2022063016/5fd5b17e44f13e6c110a4c5a/html5/thumbnails/3.jpg)
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
![Page 4: Compsci201 Binary Trees DnaLinked Assignment · Binary Search Trees ... •Similar to binary search. O(log N) if tree "good" •Trees are generally well-behaved, but !!! •Guarantee?](https://reader036.vdocuments.mx/reader036/viewer/2022063016/5fd5b17e44f13e6c110a4c5a/html5/thumbnails/4.jpg)
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
![Page 5: Compsci201 Binary Trees DnaLinked Assignment · Binary Search Trees ... •Similar to binary search. O(log N) if tree "good" •Trees are generally well-behaved, but !!! •Guarantee?](https://reader036.vdocuments.mx/reader036/viewer/2022063016/5fd5b17e44f13e6c110a4c5a/html5/thumbnails/5.jpg)
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
![Page 6: Compsci201 Binary Trees DnaLinked Assignment · Binary Search Trees ... •Similar to binary search. O(log N) if tree "good" •Trees are generally well-behaved, but !!! •Guarantee?](https://reader036.vdocuments.mx/reader036/viewer/2022063016/5fd5b17e44f13e6c110a4c5a/html5/thumbnails/6.jpg)
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
![Page 7: Compsci201 Binary Trees DnaLinked Assignment · Binary Search Trees ... •Similar to binary search. O(log N) if tree "good" •Trees are generally well-behaved, but !!! •Guarantee?](https://reader036.vdocuments.mx/reader036/viewer/2022063016/5fd5b17e44f13e6c110a4c5a/html5/thumbnails/7.jpg)
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
![Page 8: Compsci201 Binary Trees DnaLinked Assignment · Binary Search Trees ... •Similar to binary search. O(log N) if tree "good" •Trees are generally well-behaved, but !!! •Guarantee?](https://reader036.vdocuments.mx/reader036/viewer/2022063016/5fd5b17e44f13e6c110a4c5a/html5/thumbnails/8.jpg)
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
![Page 9: Compsci201 Binary Trees DnaLinked Assignment · Binary Search Trees ... •Similar to binary search. O(log N) if tree "good" •Trees are generally well-behaved, but !!! •Guarantee?](https://reader036.vdocuments.mx/reader036/viewer/2022063016/5fd5b17e44f13e6c110a4c5a/html5/thumbnails/9.jpg)
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
![Page 10: Compsci201 Binary Trees DnaLinked Assignment · Binary Search Trees ... •Similar to binary search. O(log N) if tree "good" •Trees are generally well-behaved, but !!! •Guarantee?](https://reader036.vdocuments.mx/reader036/viewer/2022063016/5fd5b17e44f13e6c110a4c5a/html5/thumbnails/10.jpg)
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
![Page 11: Compsci201 Binary Trees DnaLinked Assignment · Binary Search Trees ... •Similar to binary search. O(log N) if tree "good" •Trees are generally well-behaved, but !!! •Guarantee?](https://reader036.vdocuments.mx/reader036/viewer/2022063016/5fd5b17e44f13e6c110a4c5a/html5/thumbnails/11.jpg)
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
![Page 12: Compsci201 Binary Trees DnaLinked Assignment · Binary Search Trees ... •Similar to binary search. O(log N) if tree "good" •Trees are generally well-behaved, but !!! •Guarantee?](https://reader036.vdocuments.mx/reader036/viewer/2022063016/5fd5b17e44f13e6c110a4c5a/html5/thumbnails/12.jpg)
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
![Page 13: Compsci201 Binary Trees DnaLinked Assignment · Binary Search Trees ... •Similar to binary search. O(log N) if tree "good" •Trees are generally well-behaved, but !!! •Guarantee?](https://reader036.vdocuments.mx/reader036/viewer/2022063016/5fd5b17e44f13e6c110a4c5a/html5/thumbnails/13.jpg)
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
![Page 14: Compsci201 Binary Trees DnaLinked Assignment · Binary Search Trees ... •Similar to binary search. O(log N) if tree "good" •Trees are generally well-behaved, but !!! •Guarantee?](https://reader036.vdocuments.mx/reader036/viewer/2022063016/5fd5b17e44f13e6c110a4c5a/html5/thumbnails/14.jpg)
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
![Page 15: Compsci201 Binary Trees DnaLinked Assignment · Binary Search Trees ... •Similar to binary search. O(log N) if tree "good" •Trees are generally well-behaved, but !!! •Guarantee?](https://reader036.vdocuments.mx/reader036/viewer/2022063016/5fd5b17e44f13e6c110a4c5a/html5/thumbnails/15.jpg)
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?
![Page 16: Compsci201 Binary Trees DnaLinked Assignment · Binary Search Trees ... •Similar to binary search. O(log N) if tree "good" •Trees are generally well-behaved, but !!! •Guarantee?](https://reader036.vdocuments.mx/reader036/viewer/2022063016/5fd5b17e44f13e6c110a4c5a/html5/thumbnails/16.jpg)
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
![Page 17: Compsci201 Binary Trees DnaLinked Assignment · Binary Search Trees ... •Similar to binary search. O(log N) if tree "good" •Trees are generally well-behaved, but !!! •Guarantee?](https://reader036.vdocuments.mx/reader036/viewer/2022063016/5fd5b17e44f13e6c110a4c5a/html5/thumbnails/17.jpg)
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
![Page 18: Compsci201 Binary Trees DnaLinked Assignment · Binary Search Trees ... •Similar to binary search. O(log N) if tree "good" •Trees are generally well-behaved, but !!! •Guarantee?](https://reader036.vdocuments.mx/reader036/viewer/2022063016/5fd5b17e44f13e6c110a4c5a/html5/thumbnails/18.jpg)
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
![Page 19: Compsci201 Binary Trees DnaLinked Assignment · Binary Search Trees ... •Similar to binary search. O(log N) if tree "good" •Trees are generally well-behaved, but !!! •Guarantee?](https://reader036.vdocuments.mx/reader036/viewer/2022063016/5fd5b17e44f13e6c110a4c5a/html5/thumbnails/19.jpg)
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;
}
![Page 20: Compsci201 Binary Trees DnaLinked Assignment · Binary Search Trees ... •Similar to binary search. O(log N) if tree "good" •Trees are generally well-behaved, but !!! •Guarantee?](https://reader036.vdocuments.mx/reader036/viewer/2022063016/5fd5b17e44f13e6c110a4c5a/html5/thumbnails/20.jpg)
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();
}
![Page 21: Compsci201 Binary Trees DnaLinked Assignment · Binary Search Trees ... •Similar to binary search. O(log N) if tree "good" •Trees are generally well-behaved, but !!! •Guarantee?](https://reader036.vdocuments.mx/reader036/viewer/2022063016/5fd5b17e44f13e6c110a4c5a/html5/thumbnails/21.jpg)
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
![Page 22: Compsci201 Binary Trees DnaLinked Assignment · Binary Search Trees ... •Similar to binary search. O(log N) if tree "good" •Trees are generally well-behaved, but !!! •Guarantee?](https://reader036.vdocuments.mx/reader036/viewer/2022063016/5fd5b17e44f13e6c110a4c5a/html5/thumbnails/22.jpg)
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
![Page 23: Compsci201 Binary Trees DnaLinked Assignment · Binary Search Trees ... •Similar to binary search. O(log N) if tree "good" •Trees are generally well-behaved, but !!! •Guarantee?](https://reader036.vdocuments.mx/reader036/viewer/2022063016/5fd5b17e44f13e6c110a4c5a/html5/thumbnails/23.jpg)
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
![Page 24: Compsci201 Binary Trees DnaLinked Assignment · Binary Search Trees ... •Similar to binary search. O(log N) if tree "good" •Trees are generally well-behaved, but !!! •Guarantee?](https://reader036.vdocuments.mx/reader036/viewer/2022063016/5fd5b17e44f13e6c110a4c5a/html5/thumbnails/24.jpg)
WOTO
http://bit.ly/201fall18-oct19-1
11/1/17 Compsci 201, Fall 2017, Tree, Tries, Tradoffs 24
![Page 25: Compsci201 Binary Trees DnaLinked Assignment · Binary Search Trees ... •Similar to binary search. O(log N) if tree "good" •Trees are generally well-behaved, but !!! •Guarantee?](https://reader036.vdocuments.mx/reader036/viewer/2022063016/5fd5b17e44f13e6c110a4c5a/html5/thumbnails/25.jpg)
Guest Speaker
10/17/2018Compsci 201, Fall 2018, Recursion +
Binary Trees25
https://en.wikipedia.org/wiki/Ge_Wang