|CIT 594 Assignment
5: Tree API
CIT 594, Spring
- To give you some experience with general tree
- To give you more experience with writing ADTs
- To give you more practice with recursion
- To create a class you can use in later assignments
Write a general-purpose API for trees.
- public Tree(Object obj)
- Constructs a Tree consisting of a single tree node, with
as its value.
- public Tree (Object obj, Tree child)
- Constructs a Tree consisting of a new tree node with
as its value, and
child as its single child.
- public boolean isBinaryTree( )
true if no node in this tree has more than two children.
- public boolean isLeaf( )
true if this tree node has no children.
- public boolean equals(Object obj)
true if this tree node and the given parameter are
equal (equal values, equal subtrees in same order).
- public int getLevel( )
- Returns the level at which this tree node occurs (root has level 0).
- public int getNumberOfChildren( )
- Returns the number of children of this tree node.
- public Object getValue( )
- Returns the value in this tree node.
- public Tree[ ] getChildren()
- Returns an array of children of this tree node.
- public String toString( )
- Returns a one-line representation of the tree rooted at this tree node.
- public Tree getParent( )
- Returns the parent of this tree node, or
null if this tree
node is the root.
- public Tree getSubtree(Object value)
- Returns the first subtree of this tree node whose value is equal to
- public void setValue(Object value)
- Sets the value of this tree node to
- public void addChild(Tree child)
child as a new (last) child of this tree node.
- public void addChildren(Object[ ] children)
- For each value in the array
children, creates a new tree
node with that value and add it as a new child of this tree node.
- public Tree deleteChild(Tree subtree)
subtree as a child of this tree node, and returns
it (or returns
null if subtree was not a child of this tree
- public void print( )
- Prints the tree rooted at this tree node.
Tuesday, February 22, before midnight. Note that the JUnit tests have been
done for you, so this assignment should be significantly less work than usual.