Interface Tree<E>

Type Parameters:
E - Element to be stored at each position of the tree.
All Superinterfaces:
Iterable<E>
All Known Subinterfaces:
BinaryTree<E>
All Known Implementing Classes:
AbstractBinaryTree, AbstractTree, LinkedBinaryTree, TreeMap.BalanceableBinaryTree

public interface Tree<E> extends Iterable<E>
An interface for a tree where nodes can have an arbitrary number of children.
  • Method Details

    • root

      Position<E> root()
      Returns the root Position of the tree (or null if tree is empty).
      Returns:
      root Position of the tree (or null if tree is empty)
    • parent

      Returns the Position of p's parent (or null if p is root).
      Parameters:
      p - A valid Position within the tree
      Returns:
      Position of p's parent (or null if p is root)
      Throws:
      IllegalArgumentException - if p is not a valid Position for this tree.
    • children

      Returns an iterable collection of the Positions representing p's children.
      Parameters:
      p - A valid Position within the tree
      Returns:
      iterable collection of the Positions of p's children
      Throws:
      IllegalArgumentException - if p is not a valid Position for this tree.
    • numChildren

      int numChildren(Position<E> p) throws IllegalArgumentException
      Returns the number of children of Position p.
      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.
    • isInternal

      boolean isInternal(Position<E> p) throws IllegalArgumentException
      Returns true if Position p has one or more children.
      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

      boolean isExternal(Position<E> p) throws IllegalArgumentException
      Returns true if Position p does not have any children.
      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

      boolean isRoot(Position<E> p) throws IllegalArgumentException
      Returns true if Position p represents the root of the tree.
      Parameters:
      p - A valid Position within the tree
      Returns:
      true if p is the root of the tree, false otherwise
      Throws:
      IllegalArgumentException - if p is not a valid Position for this tree.
    • size

      int size()
      Returns the number of nodes in the tree.
      Returns:
      number of nodes in the tree
    • isEmpty

      boolean isEmpty()
      Tests whether the tree is empty.
      Returns:
      true if the tree is empty, false otherwise
    • iterator

      Iterator<E> iterator()
      Returns an iterator of the elements stored in the tree.
      Specified by:
      iterator in interface Iterable<E>
      Returns:
      iterator of the tree's elements
    • positions

      Iterable<Position<E>> positions()
      Returns an iterable collection of the positions of the tree.
      Returns:
      iterable collection of the tree's positions