ASSIGNMENTS h0 available and due before 10pm on Monday 1/28 h1 available and due before 10pm on Monday 2/4 p1 available and due before 10pm on Thursday 2/7 Week 2 TA Lab Consulting - See schedule (cs400 home pages) Peer Mentoring available - Friday 8am-12pm, 12:15-1:30pm in 1289CS TAs and Peer Mentors will focus on helping students get p1 JUnit tests running in Lab. We can not guarantee that we can get your personal computers configured. Module: Week 2 (start on week 3 before next week) THIS WEEK Ready Set Program 1! o Read Assignment - there are getting started instructions there o Create and configure project for JUnit5, compile and run TestDS_My o Testing: JUnit5 Java: inner classes Determining Height of a Tree (Recursion Review) Binary Search Trees (BST) (Review?) o operations o implementing o complexities Classifying Binary Trees Balanced Search Trees George Adelson-Velsky and Evgenii Landis NEXT WEEK X-team Exercise x1 (in-class exercise with your assigned teams) Watch for instructions to find your team number and how to meet Copyright 2018 Deb Deppeler CS 400 (S19): W02-1
Writing JUnit5 Tests What is JUnit? a framework that allows the programmer to just write the tests the JUnit test runner will run and show results of your tests How do I use it? 1. Add JUnit 5 to the build path 2. Add a JUnit Test Case (class) 3. Add test methods with the @Test annotation 4. Run the Test Case class using JUnit5 test runner What does a @test method look like? contains code to do something and some "test" of the state of a variable, object, or other Details: 1. Import JUnit classes import static org.junit.jupiter.api.assertions.*; import org.junit.jupiter.api.afterall; import org.junit.jupiter.api.aftereach; import org.junit.jupiter.api.beforeall; import org.junit.jupiter.api.beforeeach; import org.junit.jupiter.api.test; public class TestClass { } @Test void test123_try_someting_check_results() { // try something // if (cond_expression) fail("descriptive failure message") // assertequals( expr1, expr2 ) // fail if something unexpected occurs or if something expected does not occur. } Note: the order the tests are run is not guaranteed Therefore, th test methods must work independent of the results of other tests Copyright 2018 Deb Deppeler CS 400 (S19): W02-2
Java's Inner Classes https://docs.oracle.com/javase/tutorial/java/javaoo/innerclasses.html What is an inner class? a class type that is defined within another class in Java. make it private (ensures used only within the encompassing class) allows the programmer the freedom to define and refactor the inner class as needed without worry that other code is dependent upon inner class Define a generic Tree cass using an inner class for the individual node type of the tree. public class Tree<K extends Comparable<K>> { } Copyright 2018 Deb Deppeler CS 400 (S19): W02-3
Determining Height of a Tree (or sub-tree) CS 400 Programming III Doing height here partially as a review of recursion and private helper methods height of a tree We define height of a tree as the # nodes on path from the root to the deepest leaf. Write a recursive definition for the height of a general tree. height(t) 0 if t is null 1 if t is leaf 1 + max ( height of children ) What are the base cases? recursive cases? Complete the recursive height method based on the recursive definition. Assume the method is added to a Tree class having a root instance variable. public int height() { } private int height(treenode<t> t) { DO ON OWN! Copyright 2018 Deb Deppeler CS 400 (S19): W02-4
Binary Search Tree (BST) Review Image created at: https://www.cs.usfca.edu/~galles/visualization/bst.html Insert 5, 27, 90, 73, 57 into the above BST tree (recall binary search algorithm) Insert the values 1,2,3,4,5 into an empty BST What can you conclude about the shape of a BST when the values are inserted in sorted order? Will you only get this shape if inserted in sorted order? Copyright 2018 Deb Deppeler CS 400 (S19): W02-5
Practice Deleting from a BST Delete 90 from this tree. Delete 40 and then 65 from this tree. Delete 10 and then 70 from this tree. How do you delete 30 or 50 from this tree? Recall: in-order predecessor and in-order successor Copyright 2018 Deb Deppeler CS 400 (S19): W02-6
Delete 30 from this tree using the in-order predecessor replace 30 with 20 and delete 20 from left sub-tree Delete 50 from this tree using the in-order successor replace 50 with 55 and delete 55 from right sub-treee How do we find in-order predecessor or in-order successor? Copyright 2018 Deb Deppeler CS 400 (S19): W02-7
Implementing BST Operations boolean lookup(bstnode<k> n, K key) { if n is null, return false if n's key = key, return true if key < n's key, return lookup(left,key) return lookup(right,key) public insert(k key) { insert(root,key); } /** pre-cond: key is NOT null */ private BSTnode<K> insert(bstnode<k> n, K key) throws DuplicateKeyException { if n is null, return new node with this key if key equals n's key, throw DuplicateKeyException if key < n's key, assign insert(left,key) to n's left subtree else assign insert(right,key) to n's right subtree return current node Copyright 2018 Deb Deppeler CS 400 (S19): W02-8
Implementing BST Operations delete /** pre-cond: key is not null */ private BSTnode<K> delete(bstnode<k> n, K key) { if n is null, return n // key not found if key equals n's key // key found, 3 cases to consider CASE #1: n has no children // "unlink N" by returning null // to appropriate child of parent CASE #2: n has one child // return n's other child to parent if (n.left==null) return n.right if (n.right==null) return n.left CASE #3: n has two children // find replacement value (v) for n // delete v from appropriate child of n 1. find replacement value largest value in left (in-order predecessor) smallest value in right (in-order successor) 2. copy v into n's key 3. recursively delete v from approp sub-tree KEY CONCEPT: AVOID LOTS OF RELINKING, FIND REPLACEMENT VALUE INSTEAD Copyright 2018 Deb Deppeler CS 400 (S19): W02-9
Complexities of BST Operations Problem size: N = print: O(N) review traversals here lookup: O(H) must compute height as before insert: O(H) must compute height delete: O(H) must compute height k-ary Tree Classification Terms Perfect: (referred to as full in some readings) all internal nodes have n-children and all leaves at same level, H = 2^N -1 Complete: all levels filled except possibly last level, leaves at last level filled left to right Height-Balanced: every node is has sub-trees heights that differ by at most one Balanced A tree is balanced if the insert operation is O(log2 N) Copyright 2018 Deb Deppeler CS 400 (S19): W02-10
Balanced Search Trees Goal: keep height at O(log2N) where N is the number of nodes Idea: have insert and delete maintain balance 1. Detect out of balance 2. Fix balance by??? How do we detect and fix? 1. Check height of each sub-tree 2. If height of each sub-tree differs by more than 1, it is not balanced Balance Factor: The height of the left sub-tree minus the height of the right sub-tree. If balance-factor <= 1 for all nodes of the tree, the tree is height-balanced Copyright 2018 Deb Deppeler CS 400 (S19): W02-11
Height-Balanced Practice Which of these trees is not height-balanced? What is B.F. of each node? Copyright 2018 Deb Deppeler CS 400 (S19): W02-12
Balancing I - Practice Draw BST for: 10,20,30 Draw BST for: 30,20,10 Draw BST for 10,30,20 Draw BST for 30,10,20 Copyright 2018 Deb Deppeler CS 400 (S19): W02-13
Balancing II - General Balancing Assume: a triangle represents sub-tree of unknown height, but that trees shown are height-balanced, and show the relative difference (balance factor of -1, 0, 1) CS 400 Programming III Copyright 2018 Deb Deppeler CS 400 (S19): W02-14
Balancing III - More General Balancing Copyright 2018 Deb Deppeler CS 400 (S19): W02-15
Georgy Adelson-Velsky and Evgenii Landis described a tree that maintain its height-balanced uses the "rotations" to maintain balance CS 400 Programming III Complexity of operations O(H) but what is the height of an almost-balanced AVL tree? AV-L proved that in the worst-case AVL tree is ~1.44 log2(n) Implementation Must know balance factor for each node Detect out of balance: when BF > 1 If insert was in left child's left sub-tree, right rotate If insert was in right child's right sub-tree, left rotate If insert was in right child's left sub-tree, right-left rotate If insert was in left child's right sub-tree, left-right rotate How can we know height of each sub-tree? compute height extra processing on each insert delete store height in node must keep current store height difference - -1,0,1 (or it is out of balance) Copyright 2018 Deb Deppeler CS 400 (S19): W02-16