Package me.legrange.tree
Class GeneralTree<T>
- java.lang.Object
-
- me.legrange.tree.GeneralTree<T>
-
- Type Parameters:
T- The type of data stored in the tree
- All Implemented Interfaces:
Tree<T>
public final class GeneralTree<T> extends Object implements Tree<T>
A simple tree class that hides the tree implementation. The user works only with the Tree object and with their data. Since the tree relies on hashCode() and equals() internally, for best results it should be used by objects that implement those properly.
-
-
Constructor Summary
Constructors Constructor Description GeneralTree(T rootData)Create a new tree with the given data at the root.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(T child)Add a child to the root of the treevoidadd(T parent, T child)Add a child to a specific parent.Stream<T>breadthStream()Return a stream that does breadth-first traversal of the tree.booleancontains(T object)Check if the tree contains the given data somewhere.Stream<T>depthStream()Return a stream that does depth-first traversal of the tree.List<T>getChildren(T parent)Get the child data for specific parent data.intgetDepth()Return the depth of the tree.Optional<T>getParent(T child)Get the parent data for child dataTgetRoot()Return the data at the root of the treeintgetWidth()Return the width of the tree
-
-
-
Constructor Detail
-
GeneralTree
public GeneralTree(T rootData)
Create a new tree with the given data at the root.- Parameters:
rootData- The data for the root
-
-
Method Detail
-
contains
public boolean contains(T object)
Description copied from interface:TreeCheck if the tree contains the given data somewhere.
-
depthStream
public Stream<T> depthStream()
Description copied from interface:TreeReturn a stream that does depth-first traversal of the tree.- Specified by:
depthStreamin interfaceTree<T>- Returns:
- The stream
-
breadthStream
public Stream<T> breadthStream()
Description copied from interface:TreeReturn a stream that does breadth-first traversal of the tree.- Specified by:
breadthStreamin interfaceTree<T>- Returns:
- The stream
-
getRoot
public T getRoot()
Description copied from interface:TreeReturn the data at the root of the tree
-
getParent
public Optional<T> getParent(T child)
Description copied from interface:TreeGet the parent data for child data
-
getDepth
public int getDepth()
Description copied from interface:TreeReturn the depth of the tree.
-
getWidth
public int getWidth()
Description copied from interface:TreeReturn the width of the tree
-
add
public void add(T child)
Add a child to the root of the tree- Parameters:
child- The child data
-
add
public void add(T parent, T child)
Add a child to a specific parent.- Parameters:
parent- The parent datachild- The child data
-
-