java.lang.Object
org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree<T>
All Implemented Interfaces:
Serializable

public final class Tree<T> extends Object implements Serializable
A tree data structure with a tree-shaped public API.

Iteration order over the nodes at a given level is not promised. Because children are keyed by node value, sibling branches that resolve to the same value are represented as a single node; callers that require every path to be preserved should use Path instead.

Author:
Marko A. Rodriguez (http://markorodriguez.com)
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
     
    Tree(T... rootValues)
    Creates a tree with the given values as root keys, each mapping to an empty Tree.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addTree(Tree<T> other)
    Recursively merges other into this tree.
    childAt(T key)
    Returns the child subtree for the given key.
    boolean
    contains(T value)
    Returns true if the given value appears as a key anywhere in this tree (recursive).
    boolean
    equals(Object other)
    Structural recursive equality.
    Recursively searches the tree for the first subtree rooted at key and returns it.
    Returns all keys whose subtrees are leaves.
    Returns single-key trees representing each leaf key in this tree, preserving the original key-to-empty-subtree mapping.
    getNodesAtDepth(int depth)
    Returns the keys at the given depth.
    Returns the existing child for key, or inserts and returns a new empty Tree if absent.
    getTreesAtDepth(int depth)
    Returns the trees at the given depth.
    boolean
    hasChild(T key)
    Returns true if the given key is an immediate child of this tree.
    int
     
    boolean
    Returns true if this tree has no children.
    int
    Returns the total number of nodes (keys) in the tree, counted recursively.
    Produces a formatted string representation of the tree structure using a |-- ASCII style.
    Returns the set of keys at the root of this tree.
    Splits this tree into one tree per root key.
     

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Tree

      public Tree()
    • Tree

      @SafeVarargs public Tree(T... rootValues)
      Creates a tree with the given values as root keys, each mapping to an empty Tree.
  • Method Details

    • rootNodes

      public Set<T> rootNodes()
      Returns the set of keys at the root of this tree. The returned set is unmodifiable.
    • childAt

      public Tree<T> childAt(T key)
      Returns the child subtree for the given key.
      Throws:
      IllegalArgumentException - if no child exists for the given key
    • hasChild

      public boolean hasChild(T key)
      Returns true if the given key is an immediate child of this tree.
    • contains

      public boolean contains(T value)
      Returns true if the given value appears as a key anywhere in this tree (recursive).
    • findSubtree

      public Optional<Tree<T>> findSubtree(T key)
      Recursively searches the tree for the first subtree rooted at key and returns it. The search visits direct children first and then recurses, but iteration order across siblings is unspecified.
    • getOrCreateChild

      public Tree<T> getOrCreateChild(T key)
      Returns the existing child for key, or inserts and returns a new empty Tree if absent.
    • isLeaf

      public boolean isLeaf()
      Returns true if this tree has no children. An empty tree is considered a leaf.
    • nodeCount

      public int nodeCount()
      Returns the total number of nodes (keys) in the tree, counted recursively.
    • getNodesAtDepth

      public List<T> getNodesAtDepth(int depth)
      Returns the keys at the given depth. Depth 0 returns the root keys.
    • getTreesAtDepth

      public List<Tree<T>> getTreesAtDepth(int depth)
      Returns the trees at the given depth. Depth 0 returns a singleton list containing this tree. Depths beyond the tree's height return an empty list.
    • getLeafNodes

      public List<T> getLeafNodes()
      Returns all keys whose subtrees are leaves.
    • getLeafTrees

      public List<Tree<T>> getLeafTrees()
      Returns single-key trees representing each leaf key in this tree, preserving the original key-to-empty-subtree mapping.
    • addTree

      public void addTree(Tree<T> other)
      Recursively merges other into this tree. For overlapping keys, child subtrees are merged in turn. For keys present only in other, the corresponding subtree reference is adopted directly.
    • splitParents

      public List<Tree<T>> splitParents()
      Splits this tree into one tree per root key. If the tree has a single root, returns a singleton list containing this tree.
    • prettyPrint

      public String prettyPrint()
      Produces a formatted string representation of the tree structure using a |-- ASCII style.
    • equals

      public boolean equals(Object other)
      Structural recursive equality. Order across siblings is irrelevant; two trees are equal if they have the same set of keys and each key's subtree is recursively equal.
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object