Class BinarySearchTree<T extends Comparable>

  • All Implemented Interfaces:
    Tree<T>

    public final class BinarySearchTree<T extends Comparable>
    extends Object
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected Map<T,​me.legrange.tree.BinaryNode<T>> index  
      protected me.legrange.tree.BinaryNode<T> root  
    • Constructor Summary

      Constructors 
      Constructor Description
      BinarySearchTree​(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
      void add​(T child)
      Add a right child to a specific parent.
      protected void addLeft​(T child)
      Add a left child to the root of the tree
      protected void addLeft​(T parent, T child)
      Add a left child to a specific parent.
      protected void addRight​(T child)
      Add a right child to the root of the tree
      protected void addRight​(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.
      boolean contains​(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.
      T find​(T data)
      Find the data closest to the search term in the tree.
      int getDepth()
      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 data
      Optional<T> getRight​(T parent)
      Get the right child data for specific parent data.
      T getRoot()
      Return the data at the root of the tree
      int getWidth()
      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

      • BinarySearchTree

        public BinarySearchTree​(T rootData)
        Create a new binary ree with the given data at the root.
        Parameters:
        rootData - The data for the root
    • Method Detail

      • add

        public void add​(T child)
        Add a right child to a specific parent.
        Parameters:
        child - The child data
      • find

        public T find​(T data)
        Find the data closest to the search term in the tree.
        Parameters:
        data - The search term
        Returns:
        The closest found data
      • contains

        public final boolean contains​(T object)
        Description copied from interface: Tree
        Check if the tree contains the given data somewhere.
        Specified by:
        contains in interface Tree<T>
        Parameters:
        object - The object
        Returns:
        Is it in the tree?
      • depthStream

        public final Stream<T> depthStream()
        Description copied from interface: Tree
        Return a stream that does depth-first traversal of the tree.
        Specified by:
        depthStream in interface Tree<T>
        Returns:
        The stream
      • breadthStream

        public final Stream<T> breadthStream()
        Description copied from interface: Tree
        Return a stream that does breadth-first traversal of the tree.
        Specified by:
        breadthStream in interface Tree<T>
        Returns:
        The stream
      • getRoot

        public final T getRoot()
        Description copied from interface: Tree
        Return the data at the root of the tree
        Specified by:
        getRoot in interface Tree<T>
        Returns:
        The data
      • getParent

        public final Optional<T> getParent​(T child)
        Description copied from interface: Tree
        Get the parent data for child data
        Specified by:
        getParent in interface Tree<T>
        Parameters:
        child - The child data
        Returns:
        The parent data
      • getDepth

        public final int getDepth()
        Description copied from interface: Tree
        Return the depth of the tree.
        Specified by:
        getDepth in interface Tree<T>
        Returns:
        The depth
      • getWidth

        public final int getWidth()
        Description copied from interface: Tree
        Return the width of the tree
        Specified by:
        getWidth in interface Tree<T>
        Returns:
      • addLeft

        protected void addLeft​(T child)
        Add a left child to the root of the tree
        Parameters:
        child - The child data
      • addRight

        protected void addRight​(T child)
        Add a right child to the root of the tree
        Parameters:
        child - The child data
      • addLeft

        protected void addLeft​(T parent,
                               T child)
        Add a left child to a specific parent.
        Parameters:
        parent - The parent data
        child - The child data
      • addRight

        protected void addRight​(T parent,
                                T child)
        Add a right child to a specific parent.
        Parameters:
        parent - The parent data
        child - The child data
      • 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