Type Parameters:
T - The type of value store in the tree node
All Implemented Interfaces:
Function<TreeNode<T>,Long>

@Deprecated public class Size<T> extends AbstractPreOrderVisitor<T,Long>
Deprecated.
Size reflects an obsolete implementation that doesn't strictly bind the principle of Visitor pattern. Please use the enhanced TreeTraverser
Computes the size of a binary tree.

The size of a binary tree is defined to be the number of nodes it has.

  • Constructor Details

    • Size

      public Size()
      Deprecated.
  • Method Details

    • of

      public static <T> Long of(TreeNode<T> root)
      Deprecated.
      One-time computes the size of a specified binary tree.
      Type Parameters:
      T - The type of value store in the tree node
      Parameters:
      root - The root of the specified binary tree
      Returns:
      the number of nodes in the tree
      Throws:
      NullPointerException - if root is null
    • getTraversalResult

      protected Long 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,Long>
      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,Long>
      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,Long>
      Returns:
      true if traversal result is already publishable.