Share
Embed
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
55 slides
-
Memory Upload • Class BinaryNode • Class BinarySearchTree • Search
keys • Class Person • Class Menu • Method get() -
Binary Node • A binary node has three parts 1.
An address to the item being stored 2. An address to the left child node 3. An address to the right child node -
Binary Node Code • See BinaryNode.java for code • Data
fields • data, left, right • Methods • constructor, toString, mutators, and accessors, • Driver (client, test) program • See the main method -
Binary Node in Memory • Instantiate a BinaryNode object BinaryNode<String>
node1 = new BinaryNode<String>("A",null,null); node1 data left right null null A -
Binary Node in Memory • Instantiate a 2nd BinaryNode object
BinaryNode<String> node2 = new BinaryNode<String>("B",null,null); node2 node1 data left right null null B data left right null null A -
Binary Node in Memory • Instantiate a 3rd BinaryNode object
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 -
BinaryNode<String> nodeX = node3.getLeftChild(); nodeX Binary Node in Memory •
Get pointer to root’s (node3) left child data left right C node1 data left right null null B data left right null null A -
• Change left node’s data to "X" nodeX.setData("X"); Binary Node
in Memory data left right C node1 data left right null null B data left right null null X nodeX -
Binary Search Tree Code • See BinarySearchTree.java • Data fields
• root • Public methods • constructor, add(), toString(), preOrder(), postOrder(), get(), remove() -
Binary Search Tree Code • See BinarySearchTree.java • Driver (client,
test) program • See the main() method • Exception class • See class TreeException at the bottom of the file -
Search Key • A Binary Search Tree is useful in
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 -
Search Key • Using a search key to organize items
into a Binary Search Tree provides a quick and easy way to locate an item -
Search Key • A single record consists of several fields
• Fields are related items • For example, the name, the amount of tax owed, and the social security number (SSN) are different fields for a Person -
Search Key • One of the fields in the record
is the designated search key • In this example, SSN is the search key • IMPORTANT: The search key should be unique and should NOT be changed -
Class Person • See file Person.java for code • In
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 -
Class Person and Overloading • Class Person has many constructors
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 -
Person Object in Memory • An object of class Person
in memory Person person1 = new Person(5000, 123.45, "Nami"); person1 ssn tax name 5000 123.45 Nami -
IRS Menu • See file Menu.java for a “mini-IRS” program
• 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 -
Adding to the Tree • Although the records in file
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 -
Menu Object in Memory • The constructor instantiates a Menu
object with a BinarySearchTree object, which has no binary nodes Menu menu = new Menu(); menu "presidents.csv" tree database root null -
Menu Object in Memory • Add 1st record in the
file to tree menu "presidents.csv" Tree database root data left right null null ssn tax name 5000 918.15 "Abigail Powers Fillmore" -
Menu Object in Memory • Add 2nd record in the
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 -
• Add 3rd record tree data left right Menu Object
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 -
Person searchKey • Methods get(), remove(), and nodeInformation() have a
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 searchKey in Memory • An example searchKey object Person
searchKey = new Person(8200); searchKey ssn tax name 8200 0.0 "SEARCH KEY" -
Searching with searchKey • Since we have implemented the compareTo()
method for class Person, we can search through the tree for the matching Person object using the compareTo() method and the searchKey variable -
Searching with searchKey • For example, let’s search for the
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 -
• Tree of 3 nodes tree data left right Searching
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 -
Method Get() Algorithm 1. If root is null, then throw
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 -
Method Get() Code • To access private data field root,
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. } -
Method Get() in Memory • Send searchKey1 to method get()
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" -
Method Get() in Memory • Jump to 1st get() method
definition public T get(T searchKey1) { return this.get(root, searchKey1); } searchKey searchKey1 ssn tax name 8200 0.0 "SEARCH KEY" -
Method Get() in Memory tree root 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." -
Method Get() in Memory • Jump to 2nd get() method
private T get(BinaryNode<T> node, T searchKey2) { searchKey searchKey1 searchKey2 ssn tax name 8200 0.0 "SEARCH KEY" -
Method Get() in Memory tree node root 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." -
Method Get() in Memory • Because searchKey’s ssn (8200) is
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 Get() in Memory • Since we have a recursive
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) { -
Method Get() in Memory tree node node root 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." -
Method Get() in Memory • Likewise, we have a 2nd
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" -
Method Get() in Memory • In the 2nd method call
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();} -
Method Get() in Memory tree node node node.getData() root 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." -
Method Get() in Memory • The 2nd method call to
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); } -
Method Get() in Memory • Because the 2nd method call
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" -
Method Get() in Memory tree node this.get(. . .) root
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." -
Method Get() in Memory • The 1st method call to
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); } -
Method Get() in Memory • Since the 1st method call
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" -
Method Get() in Memory tree this.get(. . .) root 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." -
Method Get() in Memory • The 1st get() method returns
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); -
Method Get() in Memory tree person1 root 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." -
Method Get() in Memory • Furthermore, since the method call
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" -
Method Get() in Memory • The get() method is typically
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); -
Method Get() in Memory tree person1 root 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 777.77 "A.L." -
Memory Defragmenter • Class BinaryNode • Class BinarySearchTree • Search
keys • Class Person • Class Menu • Method get() -
Task Manager • Before the next class, you need to:
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