Upgrade to Pro — share decks privately, control downloads, hide ads and more …

lecture22.pdf

 lecture22.pdf

Avatar for William Albritton

William Albritton

September 14, 2014
Tweet

More Decks by William Albritton

Other Decks in Programming

Transcript

  1. Memory Upload • Class BinaryNode • Class BinarySearchTree • Search

    keys • Class Person • Class Menu • Method get()
  2. 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
  3. 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
  4. Binary Node in Memory • Instantiate a BinaryNode object BinaryNode<String>

    node1 = new BinaryNode<String>("A",null,null); node1 data left right null null A
  5. 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
  6. 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
  7. 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
  8. • 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
  9. Binary Search Tree Code • See BinarySearchTree.java • Data fields

    • root • Public methods • constructor, add(), toString(), preOrder(), postOrder(), get(), remove()
  10. 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
  11. 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
  12. Search Key • Using a search key to organize items

    into a Binary Search Tree provides a quick and easy way to locate an item
  13. 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
  14. 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
  15. 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
  16. 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
  17. 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
  18. 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
  19. 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
  20. 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
  21. 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"
  22. 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
  23. • 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
  24. 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
  25. Person searchKey in Memory • An example searchKey object Person

    searchKey = new Person(8200); searchKey ssn tax name 8200 0.0 "SEARCH KEY"
  26. 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
  27. 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
  28. • 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
  29. 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
  30. 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. }
  31. 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"
  32. 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"
  33. 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."
  34. 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"
  35. 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."
  36. 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); }
  37. 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) {
  38. 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."
  39. 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"
  40. 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();}
  41. 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."
  42. 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); }
  43. 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"
  44. 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."
  45. 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); }
  46. 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"
  47. 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."
  48. 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);
  49. 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."
  50. 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"
  51. 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);
  52. 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."
  53. Memory Defragmenter • Class BinaryNode • Class BinarySearchTree • Search

    keys • Class Person • Class Menu • Method get()
  54. 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