empty or at end of a leaf a. Return the new node 2. Base case 2: if the new item is already in the tree a. Throw an exception, because no duplicate values are allowed
item belongs somewhere in the left subtree a. Add it to the left subtree 4. Recursive case 2: if the new item belongs somewhere in the right subtree a. Add it to the right subtree
Search Tree by using any of the 3 recursive traversal algorithms • InOrder traversal is done by the toString() method, which calls the private recursive method inOrder()
methods preOrder(), inOrder(), and postOrder() are the order of the recursive method calls in each method definition • Check out the code of these 3 methods to see the very small difference between them
by private recursive methods, because we need access to the root, which is private and should not be accessed outside the class • In order to use the private root as a parameter, a public method must call a private method with the root as a parameter
that has two (2) or more methods with the same name, but with a different number of parameters, and/or different types of parameters • Usually, one method is called by the other overloaded methods
more classes related by inheritance that each have one (1) method with the same name, same number of parameters, same type of parameters, and same return type, but the methods have different code in the method body
ClassB and ClassC public class ClassB{ public void methodY(String s){ System.out.println(s + s); } } public class ClassC extends ClassB{ public void methodY(String s){ System.out.println(s.length()); } }
of superclass LinkedList and subclass OrderedLinkedList • The code for add() of superclass LinkedList adds to the END of list • The code for add() of subclass OrderedLinkedList adds to list in ALPHABETICAL (ascending) order
exception 2. If search key is less than root’s search key, delete from left subtree 3. If search key is greater than root’s search key, delete from right subtree 4. If search key is equal to root’s search key, then delete the record
leaf, return null (delete it) b. If node has a one (1) child node, return a reference to the child node (replace it with the child node) c. If node has two (2) child nodes, get the item in the rightmost node in the left subtree, replace the node’s item with this item, and delete the rightmost node in the left subtree
private T getLargestItem( BinaryNode<T> node) { //Returns the address of the item //with the largest search key } 5. private BinaryNode<T> deleteNodeWithLargestItem(BinaryNode<T> node) { //Removes the node with the //largest search key }
of the item with the largest search key in the left subtree private BinaryNode<T> remove(BinaryNode<T> node){ . . . T largestItemInLeftSubtree = this.getItemWithLargestSearchKey( node.getLeftChild()); . . . }
ssn tax name 5000 918.15 "A.P.F." ssn tax name 1200 -181.29 "A.S.A." data left right null null data left right null null ssn tax name 8200 -367.36 "A.L."
node's data with an address to item with the largest search key in the left subtree private BinaryNode<T> remove(BinaryNode<T> node){ . . . node.setData(largestItemInLeftSubtree); . . . }
rightmost node in the left subtree by replacing the rightmost node with its left child private BinaryNode<T> remove(BinaryNode<T> node){ . . . node.setLeftChild(this.removeNodeWithLargest SearchKey(node.getLeftChild())); . . .}
address stored in node to the 2nd remove(), which returns the address to the 1st remove(), which assigns the root to node private BinaryNode<T> remove(BinaryNode<T> node){ . . . return node; }
height • Height = log2 n (where n = number of items in the tree) • The Big-O for add, get, and remove methods is O(log2 n) • For example, only 20 comparisons are needed for a balanced binary search tree of 1 million items
maximum height • Height = n (where n = number of items in the tree) • The Big-O for add, get, and remove methods is O(n) • If the height is n, 1 million comparisons are needed for 1 million items
O(n), because we have to visit every node in the tree • Displays methods: preOrder(), inOrder(), and postOrder() • Whether the tree is balanced or unbalanced does NOT change the Big-O for the display methods
file presidents.csv and stores the records as Person objects in the tree, which is a BinarySearchTree • The issue is to keep the tree balanced • The original online file has the SSN stored at random, so the tree is only slightly unbalanced
data in the tree back to file presidents.csv, the preOrder() method is used • This will preserve the structure of the tree when the program is run again • You can also put the records into alphabetical order based on the names • But, if you order the records based on SSN, the tree will become a linked list
1.Do the assignment corresponding to this lecture 2.Email me any questions you may have about the material 3.Turn in the assignment before the due date