BinaryTree
code, and a separate class where you'll do most of your work. But first, let's take a tour of the BinaryTree
class...
BinaryTree
class defines an inner class that is uses as a building block when constructing trees, just like SinglyLinkedList
did. Go find the inner class. What is it called? What fields does it store? What methods does it support? How is this different from the Node
class in SinglyLinkedList
?
BinaryTree
class itself. What fields does it use? What methods does it support? Are they all public, or are there some private or protected "helpers" like getNode
in the list class?
BinaryTree
create a new, empty tree? How would they add a new value to the tree? How would the user access a data value in a tree? (In other words, what's the equivalent of .get()
on trees?)
createTree
method in the TreeCode
class. It creates and returns a new binary tree containing five strings. Your task is to create a diagram showing the structure of that tree. Start by right-clicking on the TreeCode
class and selecting the createTree
method. You should get a dialog box like the one shown below:
root
field shown as an arrow. Double click that to see where it leads. Each time you discover a new object (a new red window pops up), double-click on all of its object references, and repeat the process until there are no more arrows to follow. Drag the red windows around to approximate the structure of the tree they're representing. (For example, if one object points to others, arrange the "children" below their "parent".) The goal here is to gain confidence in your understanding of how trees are represented in terms of objects and object references, and make sure you have the ability to interactively explore any tree structures you come across in the future.
createTree
method and see if you can figure out how the code there built the tree you just explored. What are each of those constructor calls doing? To demonstrate your mastery of tree code, rearrange the code in createTree
(or create a new version of it) such that it creates the tree shown below. Verify that it really did build a tree with that structure by using the exploratory approach you used in the previous step.
TreeCode
class, finish the definition of rightmost
. It should take a BinaryTree of integers, and return the value from the rightmost node in the tree. (That is, keep going right until there's no right subtree, and return the value from last node you hit.) Note that the rightmost node might not be a leaf node. You can write this one with a loop if you prefer, but it's better practice to write it recursively. Below, the interactions show the method being called on trees created by the smallTree()
and bigTree()
methods.
> TreeCode.rightmost(TreeCode.smallTree()) 70 (int) > TreeCode.rightmost(TreeCode.bigTree()) 130 (int)
countNodes
, which takes a binary tree and counts the number of nodes it contains. This will require exploring all paths through the tree, and should be written recursively. Keep in mind that you can assume that countNodes
works properly as you go about implementing it. How could you make use of one or more recursive calls to countNodes
to help process the subtrees? How would you come up with the complete total after making those recursive calls?
> TreeCode.countNodes(TreeCode.smallTree()) 7 (int) > TreeCode.countNodes(TreeCode.bigTree()) 13 (int)
printTree
. It should print the contents of the tree out on a single line, using parenthesis to help illustrate the structure of the tree. These interactions show sample outputs:
> TreeCode.printTree(TreeCode.smallTree()); ( ( (10) 20 (30) ) 40 ( (50) 60 (70) ) ) > TreeCode.printTree(TreeCode.bigTree()); ( ( ( (10) 20 (30) ) 40 ( (50) 60) ) 70 ( ( (80) 90 (100) ) 110 ( (120) 130) ) )
contains
. It looks through the nodes in a binary tree to see if the tree contains a specific item, but does not assume that the items in the tree are in any particular order. (In other words, you potentially need to check each and every node's value for a match.) Recursion will be your friend once again here.
TreeCode.contains(TreeCode.bigTree(), 30) true (boolean) TreeCode.contains(TreeCode.bigTree(), 110) true (boolean) TreeCode.contains(TreeCode.bigTree(), 70) true (boolean) TreeCode.contains(TreeCode.bigTree(), 55) false (boolean)
true
if a tree is full. That is, each node either has two children, or is a leaf.