public class Tree
extends java.lang.Object
You need to complete this class by adding methods to insert values, compute the minimum and maximum, search for a value, and compute a value's depth in the tree and number of descendants. You will also need to complete several constructors used during tree creation and value insertion.
Our binary tree is an example of a recursive data structure, since each tree instance refers to several other tree instances to complete the entire structure. As you might expect, recursive algorithms are particularly useful on recursive data structures.
Constructor | Description |
---|---|
Tree(int setValue) |
Initialize a new binary tree.
|
Tree(int setValue,
Tree setParent) |
Initialize a new binary tree node for an existing tree.
|
Modifier and Type | Method | Description |
---|---|---|
int |
depth() |
Return the depth of this node from the root of the tree.
|
int |
descendants() |
Return the number of nodes below this node in the tree.
|
boolean |
insert(int newValue) |
Insert a new value into the binary tree.
|
int |
maximum() |
Return the maximum value stored in this binary tree.
|
int |
minimum() |
Return the minimum value stored in this binary tree.
|
Tree |
search(int searchValue) |
Search the binary tree for a specific value.
|
public Tree(int setValue)
setValue
- value for the root of the treepublic Tree(int setValue, Tree setParent)
setValue
- value for the new nodesetParent
- the parent of this nodepublic boolean insert(int newValue)
Insertion should follow the algorithm described in the MP5 writeup. Do not rebalance the tree or otherwise modify it between insertions. Otherwise the depth and descendants tests will fail. Insertion should fail and return false if the value already exists in the tree.
newValue
- the new value to insertpublic int minimum()
This function can assume it is called on the root node.
public int maximum()
This function can assume it is called on the root node.
public Tree search(int searchValue)
searchValue
- the value to search forpublic int depth()
public int descendants()