public class Tree extends Object
Modifier and Type | Field and Description |
---|---|
private TreeNode |
root
A tree is simply a pointer to the root
|
Constructor and Description |
---|
Tree()
constructor where the root is not initially known
|
Tree(TreeNode node)
constructor where the root is initially known
|
Modifier and Type | Method and Description |
---|---|
private StringBuilder |
DFS(StringBuilder sb,
TreeNode node,
int depth)
A preorder depth first traversal, printing node as visited
|
Enumeration<TreeNode> |
elements() |
Enumeration<TreeNode> |
getBottomUpEnumerator()
Provides a bottom-up enumeration over all elements in the tree
|
TreeNode |
getRoot()
Gets the root of the tree
|
Enumeration<TreeNode> |
getTopDownEnumerator()
Provides a top-down enumeration over all elements in the tree
|
boolean |
isEmpty()
Is the tree empty?
|
int |
numberOfNodes()
Counts and returns the number of nodes
|
void |
setRoot(TreeNode node)
Sets the root of the tree to be the passed node.
|
String |
toString()
Prints the tree
|
public Tree()
public final boolean isEmpty()
public final void setRoot(TreeNode node)
node
- the new rootpublic final Enumeration<TreeNode> elements()
public final int numberOfNodes()
public final Enumeration<TreeNode> getBottomUpEnumerator()
public final Enumeration<TreeNode> getTopDownEnumerator()
private StringBuilder DFS(StringBuilder sb, TreeNode node, int depth)
sb
- the string buffer to insert the resultsnode
- the node to processdepth
- the current depth (root = 0) in the tree