Package me.legrange.tree
Class BinaryTree<T>
- java.lang.Object
-
- me.legrange.tree.BinaryTree<T>
-
-
Constructor Summary
Constructors Constructor Description BinaryTree(T rootData)Create a new binary ree with the given data at the root.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddLeft(T child)Add a left child to the root of the treevoidaddLeft(T parent, T child)Add a left child to a specific parent.voidaddRight(T child)Add a right child to the root of the treevoidaddRight(T parent, T child)Add a right 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.intgetDepth()Return the depth of the tree.Optional<T>getLeft(T parent)Get the left child data for specific parent data.Optional<T>getParent(T child)Get the parent data for child dataOptional<T>getRight(T parent)Get the right child data for specific parent data.TgetRoot()Return the data at the root of the treeintgetWidth()Return the width of the tree
-
-
-
Field Detail
-
index
protected final Map<T,me.legrange.tree.BinaryNode<T>> index
-
root
protected final me.legrange.tree.BinaryNode<T> root
-
-
Constructor Detail
-
BinaryTree
public BinaryTree(T rootData)
Create a new binary ree with the given data at the root.- Parameters:
rootData- The data for the root
-
-
Method Detail
-
addLeft
public void addLeft(T child)
Add a left child to the root of the tree- Parameters:
child- The child data
-
addRight
public void addRight(T child)
Add a right child to the root of the tree- Parameters:
child- The child data
-
addLeft
public void addLeft(T parent, T child)
Add a left child to a specific parent.- Parameters:
parent- The parent datachild- The child data
-
addRight
public void addRight(T parent, T child)
Add a right child to a specific parent.- Parameters:
parent- The parent datachild- The child data
-
contains
public final boolean contains(T object)
Description copied from interface:TreeCheck if the tree contains the given data somewhere.
-
depthStream
public final 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 final 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 final T getRoot()
Description copied from interface:TreeReturn the data at the root of the tree
-
getParent
public final Optional<T> getParent(T child)
Description copied from interface:TreeGet the parent data for child data
-
getDepth
public final int getDepth()
Description copied from interface:TreeReturn the depth of the tree.
-
getWidth
public final int getWidth()
Description copied from interface:TreeReturn the width of the tree
-
getLeft
public final Optional<T> getLeft(T parent)
Get the left child data for specific parent data.- Parameters:
parent- The parent data- Returns:
- The child data
-
getRight
public final Optional<T> getRight(T parent)
Get the right child data for specific parent data.- Parameters:
parent- The parent data- Returns:
- The child data
-
-