Package com.groiss.ds

Class Tree<D>

java.lang.Object
com.groiss.ds.Tree<D>
Type Parameters:
D - the type of the trees data elements
All Implemented Interfaces:
Serializable

public class Tree<D> extends Object implements Serializable
Simple N-ary Tree structure. Generic but not polymorph.
See Also:
  • Field Details

    • data

      public D data
    • children

      public List<Tree<D>> children
  • Constructor Details

    • Tree

      public Tree()
      Construct an empty Tree.
    • Tree

      public Tree(D data)
      Construct a Tree with a single data element
      Parameters:
      data - the data
    • Tree

      public Tree(D data, List<Tree<D>> children)
      Construct a Tree with a data element and a list of children.
      Parameters:
      data - the data
      children - the children (a List of Tree elements)
  • Method Details

    • getChildren

      public List<Tree<D>> getChildren()
      Get the trees children
      Returns:
      the trees children (may be null).
    • hasChildren

      public boolean hasChildren()
      Determine if a tree has children.
      Returns:
      true, if the tree has children
    • setChildren

      public void setChildren(List<Tree<D>> children)
      Set the trees children. Old children are lost.
      Parameters:
      children - the new children of the tree.
    • addTree

      public void addTree(Tree<D> child)
      Add a tree as last child of the tree.
      Parameters:
      child - the new last child of the tree.
    • add

      public void add(D childData)
      Add a new tree with the data element as (last) child of the tree.
      Parameters:
      childData - the data of the new last child of the tree.
    • removeChildren

      public void removeChildren()
      Prune a tree. Remove all children of the tree. Subsequent call of method getChildren() would return null.
    • getData

      public D getData()
      Get the data element of the tree.
      Returns:
      the trees data element
    • setData

      public void setData(D data)
      Set the trees data element.
      Parameters:
      data - the new data element of the tree.
    • toString

      public String toString()
      Overrides:
      toString in class Object