Class PreOrderSearch<T>

Type Parameters:
T - The type of the value/data being searched as well as being stored in each node of the binary tree being searched
All Implemented Interfaces:
Function<TreeNode<T>,Optional<TreeNode<T>>>

@Deprecated public class PreOrderSearch<T> extends AbstractPreOrderVisitor<T,Optional<TreeNode<T>>>
Deprecated.
PreOrderSearch reflects an obsolete implementation that doesn't strictly bind the principle of Visitor pattern. Please use the enhanced TreeTraverser
Performs pre-order search on a binary tree for a target value.
  • Method Details

    • forValue

      public static <T> Function<TreeNode<T>,Optional<TreeNode<T>>> forValue(T target)
      Deprecated.
      Creates a search instance for a specified tree node value.
      Type Parameters:
      T - The type of the value/data stored in each node of the binary tree being searched
      Parameters:
      target - The value of the tree node, which is to be found
      Returns:
      a new instance
      Throws:
      NullPointerException - if target is null
    • doSearch

      public Optional<TreeNode<T>> doSearch(TreeNode<T> root)
      Deprecated.
      Searches for a node with a target value in a binary tree and returns that node.

      More formally, the target node whose node value is equal to the target value, determined by Object.equals(Object), is returned.

      This method runs in O(n) time and O(n) space without recursion.

      Parameters:
      root - The root of the binary tree
      Returns:
      the node with the target value wrapped inside Optional or Optional.empty() if no such node is found
      Throws:
      NullPointerException - if root is null
    • getTraversalResult

      protected Optional<TreeNode<T>> getTraversalResult()
      Deprecated.
      Description copied from class: AbstractVisitor
      Returns the final result computed after visiting tree (either all-node visit or partial visit)

      For example, this method can return the max node value of entire tree after iterating through all tree nodes.

      Specified by:
      getTraversalResult in class AbstractVisitor<T,Optional<TreeNode<T>>>
      Returns:
      object computed by a complete or partial traversal
    • visitNode

      protected void visitNode(TreeNode<T> node)
      Deprecated.
      Description copied from class: AbstractVisitor
      Performs actions on each visited node during traversal.
      Specified by:
      visitNode in class AbstractVisitor<T,Optional<TreeNode<T>>>
      Parameters:
      node - Node on which the action is performed against
    • canTerminate

      protected boolean canTerminate()
      Deprecated.
      Description copied from class: AbstractVisitor
      Returns whether or not the traversal should terminate early and call AbstractVisitor.getTraversalResult() right after.

      For example, this could be useful while searching a tree for a target

            1
          /  \
         2   3
        / \   \
       4  5   6
       
      Suppose we are searching for node 2 in the tree above. AbstractVisitor.canTerminate() can be implemented as
       
       if (node.value == 2) {
           return true
       } else {
           return false
       }
       
       
      Specified by:
      canTerminate in class AbstractVisitor<T,Optional<TreeNode<T>>>
      Returns:
      true if traversal result is already publishable.