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
Simple N-ary Tree structure. Generic but not polymorph.
- See Also:
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Add a new tree with the data element as (last) child of the tree.void
Add a tree as last child of the tree.Get the trees childrengetData()
Get the data element of the tree.boolean
Determine if a tree has children.void
Prune a tree.void
setChildren
(List<Tree<D>> children) Set the trees children.void
Set the trees data element.toString()
-
Field Details
-
data
-
children
-
-
Constructor Details
-
Tree
public Tree()Construct an empty Tree. -
Tree
Construct a Tree with a single data element- Parameters:
data
- the data
-
Tree
Construct a Tree with a data element and a list of children.- Parameters:
data
- the datachildren
- the children (a List of Tree elements)
-
-
Method Details
-
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
Set the trees children. Old children are lost.- Parameters:
children
- the new children of the tree.
-
addTree
Add a tree as last child of the tree.- Parameters:
child
- the new last child of the tree.
-
add
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
Get the data element of the tree.- Returns:
- the trees data element
-
setData
Set the trees data element.- Parameters:
data
- the new data element of the tree.
-
toString
-