BinaryNode<String> node3 = new BinaryNode<String>("C",node1,node2); node3 data left right C node1 data left right null null B data left right null null A
storing records, each of which has a unique search key • The records are inserted into the Binary Search Tree based on the search key, which uses the method compareTo() to compare the search key of two objects
class Person’s case, the SSN is the search key • The Binary Search Tree formed is based on the SSN • Use method compareTo() to compare person1’s SSN to person2’s SSN • Use subtraction to compare numbers
and methods that are overloaded • Four (4) Person() constructors • Two (2) setTax() methods • Overloading makes the methods more convenient to use • We can use either a String, int, or double parameter with these methods
• Can add, edit, remove, display a list of People objects and how much tax that they owe • Input is from file presidents.csv, which is a list of Social Security numbers (SSN), tax owed, and names of U.S. Presidents and First Ladies
presidents.csv are in alphabetical order by first name, the records are added to the BinarySearchTree object according to the ssn data field stored in each Person object
file to tree tree data left right null ssn tax name 5000 918.15 "Abigail Powers Fillmore" ssn tax name 1200 -181.29 "Abigail Smith Adams" data left right null null root
in Memory 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." root
variable of class Person called “searchKey” • The searchKey object stores the ssn (social security number) in a data field in order to locate a Person object in the tree which has the same ssn as the data field
Person object with ssn = 8200 • We first examine the root, and find a Person object with ssn = 5000 • So we move to the right node • The right node has a Person object with ssn = 8200, so we now have the address of the node and Person object to edit, remove, or get information
with searchKey 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." root
an exception (record not found) 2. If search key is equal to the root’s search key, return a reference to the corresponding record 3. If search key is less than the root’s search key, search the left subtree 4. If search key is greater than the root’s search key, search the right subtree
we use 2 overloaded get() methods 1. public T get(T searchKey1) { return this.get(root, searchKey1); } 2. private T get(BinaryNode<T> node, T searchKey2) { //Recursive method that returns //the address of the //object that matches //the the search key. }
to get the address of object in tree Person searchKey = new Person(8200); Person person1 = tree.get(searchKey); searchKey ssn tax name 8200 0.0 "SEARCH KEY"
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."
greater than node’s ssn (5000), the 2nd get() method executes the else statement at the bottom of the method else{ return this.get(node.getRightChild(), searchKey2); }
method call to the same get() method for the right child, this 2nd method call to 2nd get() creates a 2nd node parameter that points to the right child • return this.get(node.getRightChild(), searchKey2); • private T get(BinaryNode<T> node, T searchKey2) {
right 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."
searchKey2 variable for this 2nd method call private T get(BinaryNode<T> node, T searchKey2) { searchKey searchKey1 searchKey2 searchKey2 ssn tax name 8200 0.0 "SEARCH KEY"
to the 2nd get() method, this if statement is true, because searchKey’s ssn (8200) is equal to node’s data’s Person object’s ssn (8200) • This returns the address of Abraham Lincoln’s record (Person object) if(searchKey2.compareTo(node.getData( )) == 0){return node.getData();}
left right 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."
2nd get() method returns the address of Abraham Lincoln’s record . . . if(searchKey2.compareTo(node.getData( )) == 0){return node.getData();} • . . . to the 1st method call of the 2nd get() method public T get(T searchKey1) { return this.get(root, searchKey1); }
to 2nd get() method is finished, parameters node and searchKey2 are popped off the runtime stack searchKey searchKey1 searchKey2 ssn tax name 8200 0.0 "SEARCH KEY"
data left right 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."
the 2nd get() method returns the address of Abraham Lincoln’s record . . . return this.get(node.getRightChild(), searchKey2); • . . . to the 1st get() method public T get(T searchKey1) { return this.get(root, searchKey1); }
to the 2nd get() method is finished, parameters node and searchKey2 are popped off the runtime stack searchKey searchKey1 ssn tax name 8200 0.0 "SEARCH KEY"
left right 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."
the address of Abraham Lincoln’s record… public T get(T searchKey1) { return this.get(root, searchKey1); } • …to the variable person1 Person person1 = tree.get(searchKey);
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."
to the 1st get() method is finished, parameter searchKey1 is popped off the runtime stack Person searchKey = new Person(8200); Person person1 = tree.get(searchKey); searchKey ssn tax name 8200 0.0 "SEARCH KEY"
used to edit an object in some way • In other words, we change the value of the data field variables of the object • For example, we can now adjust Abraham Lincoln’s tax person1.setTax(777.77);
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 777.77 "A.L."
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 next lecture