Package com.zybooks.dsaj.tree
Class AbstractTree<E>
java.lang.Object
com.zybooks.dsaj.tree.AbstractTree<E>
- Type Parameters:
E
- Element to be stored at each position of the tree.
- Direct Known Subclasses:
AbstractBinaryTree
An abstract base class providing some functionality of the Tree interface.
The following three methods remain abstract, and must be
implemented by a concrete subclass: root, parent, children. Other
methods implemented in this class may be overridden to provide a
more direct and efficient implementation.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionReturns an iterable collection of positions of the tree in breadth-first order.int
Returns the number of levels separating Position p from the root.int
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
Returns true if Position p represents the root of the tree.iterator()
Returns an iterator of the elements stored in the tree.int
numChildren
(Position<E> p) Returns the number of children of Position p.Returns an iterable collection of the positions of the tree.Returns an iterable collection of positions of the tree, reported in postorder.preorder()
Returns an iterable collection of positions of the tree, reported in preorder.int
size()
Returns the number of nodes in the tree.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
AbstractTree
public AbstractTree()
-
-
Method Details
-
isInternal
Returns true if Position p has one or more children.- Specified by:
isInternal
in interfaceTree<E>
- Parameters:
p
- A valid Position within the tree- Returns:
- true if p has at least one child, false otherwise
- Throws:
IllegalArgumentException
- if p is not a valid Position for this tree.
-
isExternal
Returns true if Position p does not have any children.- Specified by:
isExternal
in interfaceTree<E>
- Parameters:
p
- A valid Position within the tree- Returns:
- true if p has zero children, false otherwise
- Throws:
IllegalArgumentException
- if p is not a valid Position for this tree.
-
isRoot
Returns true if Position p represents the root of the tree. -
numChildren
Returns the number of children of Position p.- Specified by:
numChildren
in interfaceTree<E>
- Parameters:
p
- A valid Position within the tree- Returns:
- number of children of Position p
- Throws:
IllegalArgumentException
- if p is not a valid Position for this tree.
-
size
public int size()Returns the number of nodes in the tree. -
isEmpty
public boolean isEmpty()Tests whether the tree is empty. -
depth
Returns the number of levels separating Position p from the root.- Parameters:
p
- A valid Position within the tree- Returns:
- the depth of position p
- Throws:
IllegalArgumentException
- if p is not a valid Position for this tree.
-
height
Returns the height of the subtree rooted at Position p.- Parameters:
p
- A valid Position within the tree- Returns:
- the height of the subtree rooted at p
- Throws:
IllegalArgumentException
- if p is not a valid Position for this tree.
-
iterator
Returns an iterator of the elements stored in the tree. -
positions
Returns an iterable collection of the positions of the tree. -
preorder
Returns an iterable collection of positions of the tree, reported in preorder.- Returns:
- iterable collection of the tree's positions in preorder
-
postorder
Returns an iterable collection of positions of the tree, reported in postorder.- Returns:
- iterable collection of the tree's positions in postorder
-
breadthfirst
Returns an iterable collection of positions of the tree in breadth-first order.- Returns:
- iterable collection of the tree's positions in breadth-first order
-