public abstract class AbstractTree<E> extends Object implements Tree<E>
Constructor and Description |
---|
AbstractTree() |
Modifier and Type | Method and Description |
---|---|
Iterable<Position<E>> |
breadthfirst()
Returns an iterable collection of positions of the tree in breadth-first order.
|
int |
depth(Position<E> p)
Returns the number of levels separating Position p from the root.
|
int |
height(Position<E> p)
Returns the height of the subtree rooted at Position p.
|
boolean |
isEmpty()
Tests whether the tree is empty.
|
boolean |
isExternal(Position<E> p)
Returns true if Position p does not have any children.
|
boolean |
isInternal(Position<E> p)
Returns true if Position p has one or more children.
|
boolean |
isRoot(Position<E> p)
Returns true if Position p represents the root of the tree.
|
Iterator<E> |
iterator()
Returns an iterator of the elements stored in the tree.
|
int |
numChildren(Position<E> p)
Returns the number of children of Position p.
|
Iterable<Position<E>> |
positions()
Returns an iterable collection of the positions of the tree.
|
Iterable<Position<E>> |
postorder()
Returns an iterable collection of positions of the tree, reported in postorder.
|
Iterable<Position<E>> |
preorder()
Returns an iterable collection of positions of the tree, reported in preorder.
|
int |
size()
Returns the number of nodes in the tree.
|
public boolean isInternal(Position<E> p)
isInternal
in interface Tree<E>
p
- A valid Position within the treeIllegalArgumentException
- if p is not a valid Position for this tree.public boolean isExternal(Position<E> p)
isExternal
in interface Tree<E>
p
- A valid Position within the treeIllegalArgumentException
- if p is not a valid Position for this tree.public boolean isRoot(Position<E> p)
public int numChildren(Position<E> p)
numChildren
in interface Tree<E>
p
- A valid Position within the treeIllegalArgumentException
- if p is not a valid Position for this tree.public int size()
public boolean isEmpty()
public int depth(Position<E> p) throws IllegalArgumentException
p
- A valid Position within the treeIllegalArgumentException
- if p is not a valid Position for this tree.public int height(Position<E> p) throws IllegalArgumentException
p
- A valid Position within the treeIllegalArgumentException
- if p is not a valid Position for this tree.public Iterable<Position<E>> positions()
public Iterable<Position<E>> preorder()
public Iterable<Position<E>> postorder()