## edu.stanford.nlp.trees
Class TreeGraphNode

    java.lang.Object
      java.util.AbstractCollection<Tree>
          edu.stanford.nlp.trees.Tree
              edu.stanford.nlp.trees.TreeGraphNode

All Implemented Interfaces:
  Label, Labeled, Scored, Serializable, Iterable<Tree>, Collection<Tree>

    public class TreeGraphNode

extends Tree

A "treegraph" is a tree with additional directed, labeled arcs between arbitrary pairs of nodes. (So, it's a graph with a tree skeleton.) A `TreeGraphNode` represents any node in a treegraph. The additional labeled arcs are represented by using ``CyclicCoreLabel`` labels at each node, which contain `Map`s from arc label strings to `Set`s of `TreeGraphNode`s. Each `TreeGraphNode` should contain a reference to a ``TreeGraph`` object, which is a container for the complete treegraph structure.

This class makes the horrible mistake of changing the semantics of equals and hashCode to go back to "==" and System.identityHashCode, despite the semantics of the superclass's equality.

Author:
  Bill MacCartney
See Also:
  `TreeGraph`, Serialized Form

Field Summary
`protected TreeGraphNode[]` `children` Children of this node.
`protected CyclicCoreLabel` `label` Label for this node.
`protected TreeGraphNode` `parent` Parent of this node.
`protected TreeGraph` `tg` The ``TreeGraph`` of which this node is part.
`protected static TreeGraphNode[]` `ZERO_TGN_CHILDREN` A leaf node should have a zero-length array for its children.

Fields inherited from class edu.stanford.nlp.trees.Tree
`DISPLAY_SCORES, EMPTY_TREE_ARRAY`

Constructor Summary
`` `TreeGraphNode()` Create a new empty `TreeGraphNode`.
`` `TreeGraphNode(Label label)` Create a new `TreeGraphNode` with the supplied label.
`` `TreeGraphNode(Label label, List<Tree> children)` Create a new `TreeGraphNode` with the supplied label and list of child nodes.
`` `TreeGraphNode(TreeGraphNode t)`
`` `TreeGraphNode(Tree t, TreeGraph graph)` Create a new `TreeGraphNode` having the same tree structure and label values as an existing tree (but no shared storage).
`protected` `TreeGraphNode(Tree t, TreeGraphNode parent)` Create a new `TreeGraphNode` having the same tree structure and label values as an existing tree (but no shared storage).

Method Summary

`

`<GR extends GrammaticalRelation.GrammaticalRelationAnnotation> boolean`

`

`addArc(Class<GR> arcLabel, TreeGraphNode node)`
Add a labeled arc from this node to the argument node.

`Set<Class<? extends GrammaticalRelation.GrammaticalRelationAnnotation>>` `arcLabelsToNode(TreeGraphNode destNode)` Finds all arcs between this node and `destNode`, and returns the `Set` of `Object`s which label those arcs.
`Class<? extends GrammaticalRelation.GrammaticalRelationAnnotation>` `arcLabelToNode(TreeGraphNode destNode)` Returns the label of a single arc between this node and `destNode`, or `null` if no such arc exists.
`TreeGraphNode[]` `children()` Returns an array of the children of this node.
`Set<Dependency<Label,Label,Object>>` `dependencies(Filter<Dependency<Label,Label,Object>> f, HeadFinder hf)` Return a set of node-node dependencies, represented as Dependency objects, for the Tree.
`boolean` `equals(Object o)` Implements equality for `TreeGraphNode`s.
`static TreeFactory` `factory()` Return a `TreeFactory` that produces trees of type `TreeGraphNode`.
`static TreeFactory` `factory(LabelFactory lf)` Return a `TreeFactory` that produces trees of type `TreeGraphNode`, with the `Label` made by the supplied `LabelFactory`.
`TreeGraphNode` `followArcToNode(Class<? extends GrammaticalRelation.GrammaticalRelationAnnotation> arcLabel)` Returns a single `TreeGraphNode` to which there exists an arc bearing the specified label from this node, or `null` if no such node exists.
`Set<TreeGraphNode>` `followArcToSet(Class<? extends GrammaticalRelation.GrammaticalRelationAnnotation> arcLabel)` Returns the `Set` of `TreeGraphNode`s to which there exist arcs bearing the specified label from this node, or `null` if no such nodes exist.
`int` `hashCode()` Implements a hashCode for Tree's.
`TreeGraphNode` `headTagNode()` Return the node containing the head tag for this node (or `null` if none), as recorded in this node's ``CyclicCoreLabel``.
`TreeGraphNode` `headWordNode()` Return the node containing the head word for this node (or `null` if none), as recorded in this node's ``CyclicCoreLabel``.
`TreeGraphNode` `highestNodeWithSameHead()` Checks the node's ancestors to find the highest ancestor with the same `headWordNode` as this node.
`int` `index()` Get the index for the current node.
`protected void` `indexNodes()` Assign sequential integer indices (starting with 0) to all nodes of the subtree rooted at this `TreeGraphNode`.
`CyclicCoreLabel` `label()` Returns the label associated with the current node, or null if there is no label.
`static void` `main(String[] args)` Just for testing.
`Tree` `parent()` Get the parent for the current node.
`void` `percolateHeads(HeadFinder hf)` Uses the specified ``HeadFinder`` to determine the heads for this node and all its descendants, and to store references to the head word node and head tag node in this node's ``CyclicCoreLabel`` and the `CyclicCoreLabel`s of all its descendants.
`void` `setChildren(List<? extends Tree> childTreesList)` Set the children of this tree node to the given list.
`void` `setChildren(Tree[] children)` Sets the children of this `TreeGraphNode`.
`protected void` `setIndex(int index)` Set the index for the current node.
`void` `setLabel(CyclicCoreLabel label)` Sets the label associated with the current node.
`void` `setParent(TreeGraphNode parent)` Set the parent for the current node.
`protected void` `setTreeGraph(TreeGraph tg)` Set pointer to the `TreeGraph` of which this node is a part.
`String` `toOneLineString()` Returns a `String` representation of this node and its subtree as a one-line parenthesized list.
`String` `toPrettyString(int indentLevel)` Returns a `String` representation of this node and its subtree with one node per line, indented according to `indentLevel`.
`String` `toPrimes()`
`String` `toString()` Converts parse tree to string in Penn Treebank format.
`TreeFactory` `treeFactory()` Returns a `TreeFactory` that produces `TreeGraphNode`s.
`protected TreeGraph` `treeGraph()` Get the `TreeGraph` of which this node is a part.

Methods inherited from class edu.stanford.nlp.trees.Tree

`addChild, addChild, ancestor, cCommands, constituents, constituents, constituents, deepCopy, deepCopy, deepCopy, dependencies, dependencies, dependencies, depth, depth, dominates, dominationPath, firstChild, flatten, flatten, getChild, getChildrenAsList, getLeaves, getLeaves, getNodeNumber, getSpan, headPreTerminal, headTerminal, headTerminal, indentedListPrint, indentedListPrint, indexLeaves, indexOf, indexSpans, indexSpans, indexSpans, insertDtr, isLeaf, isPhrasal, isPrePreTerminal, isPreTerminal, isUnaryRewrite, iterator, joinNode, labeledYield, labeledYield, labelFactory, labels, lastChild, leftCharEdge, localTree, localTrees, mapDependencies, mapDependencies, nodeNumber, nodeString, numChildren, parent, pathNodeToNode, pennPrint, pennPrint, pennPrint, pennString, postOrderNodeList, preOrderNodeList, preTerminalYield, preTerminalYield, printLocalTree, printLocalTree, prune, prune, removeChild, rightCharEdge, score, setChild, setFromString, setLabel, setLabels, setScore, setSpans, setValue, siblings, size, skipRoot, spliceOut, spliceOut, subTreeList, subTrees, subTrees, taggedDependencies, taggedDependencies, taggedDependencies, taggedDependencies, taggedLabeledYield, taggedYield, taggedYield, toStringBuilder, toStringBuilder, toStructureDebugString, transform, transform, treeSkeletonCopy, treeSkeletonCopy, upperMostUnary, value, valueOf, valueOf, yield, yield, yield, yieldHasWord, yieldHasWord, yieldWords, yieldWords`

Methods inherited from class java.util.AbstractCollection
`add, addAll, clear, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray`

Methods inherited from class java.lang.Object
`clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

Field Detail

### label

    protected CyclicCoreLabel label

  Label for this node.

### parent

    protected TreeGraphNode parent

  Parent of this node.

### children

    protected TreeGraphNode[] children

  Children of this node.

### tg

    protected TreeGraph tg

  The ``TreeGraph`` of which this node is part.

### ZERO_TGN_CHILDREN

    protected static final TreeGraphNode[] ZERO_TGN_CHILDREN

  A leaf node should have a zero-length array for its children. For efficiency, subclasses can use this array as a return value for children() for leaf nodes if desired. Should this be public instead?

Constructor Detail

### TreeGraphNode

    public TreeGraphNode()

  Create a new empty `TreeGraphNode`.

### TreeGraphNode

    public TreeGraphNode(Label label)

  Create a new `TreeGraphNode` with the supplied label.

  Parameters:
    `label` - the label for this node.

### TreeGraphNode

    public TreeGraphNode(Label label,
                         List<Tree> children)

  Create a new `TreeGraphNode` with the supplied label and list of child nodes.

  Parameters:
    `label` - the label for this node.
    `children` - the list of child `TreeGraphNode`s for this node.

### TreeGraphNode

    public TreeGraphNode(Tree t,
                         TreeGraph graph)

  Create a new `TreeGraphNode` having the same tree structure and label values as an existing tree (but no shared storage).

  Parameters:
    `t` - the tree to copy
    `graph` - the graph of which this node is a part

### TreeGraphNode

    public TreeGraphNode(TreeGraphNode t)

### TreeGraphNode

    protected TreeGraphNode(Tree t,
                            TreeGraphNode parent)

  Create a new `TreeGraphNode` having the same tree structure and label values as an existing tree (but no shared storage). Operates recursively to construct an entire subtree.

  Parameters:
    `t` - the tree to copy
    `parent` - the parent node

Method Detail

### equals

    public boolean equals(Object o)

  Implements equality for `TreeGraphNode`s. Unlike `Tree`s, `TreeGraphNode`s should be considered equal only if they are ==. Implementation note: TODO: This should be changed via introducing a Tree interface with the current Tree and this class implementing it, since what is done here breaks the equals() contract.

  Specified by:
    `equals` in interface `Collection<Tree>`
  Overrides:
    `equals` in class `Tree`
  Parameters:
    `o` - The object to compare with
  Returns:
    Whether two things are equal

### hashCode

    public int hashCode()

  Description copied from class: `Tree`
  Implements a hashCode for Tree's. Two trees should have the same hashcode if they are equal, so we hash on the label value and the children's label values.

  Specified by:
    `hashCode` in interface `Collection<Tree>`
  Overrides:
    `hashCode` in class `Tree`
  Returns:
    The hash code

### label

    public CyclicCoreLabel label()

  Returns the label associated with the current node, or null if there is no label.

  Specified by:
    `label` in interface `Labeled`
  Overrides:
    `label` in class `Tree`
  Returns:
    the label of the node

### setLabel

    public void setLabel(CyclicCoreLabel label)

  Sets the label associated with the current node.

  Parameters:
    `label` - the new label to use.

### index

    public int index()

  Get the index for the current node.

### setIndex

    protected void setIndex(int index)

  Set the index for the current node.

### indexNodes

    protected void indexNodes()

  Assign sequential integer indices (starting with 0) to all nodes of the subtree rooted at this `TreeGraphNode`. The leaves are indexed first, from left to right. Then the internal nodes are indexed, using a pre-order tree traversal.

### parent

    public Tree parent()

  Get the parent for the current node.

  Overrides:
    `parent` in class `Tree`
  Returns:
    The parent `Tree` node or `null`
  See Also:
    `Tree.parent(Tree)`

### setParent

    public void setParent(TreeGraphNode parent)

  Set the parent for the current node.

### children

    public TreeGraphNode[] children()

  Returns an array of the children of this node.

  Specified by:
    `children` in class `Tree`
  Returns:
    The children of the node
  See Also:
    `Tree.getChildrenAsList()`

### setChildren

    public void setChildren(Tree[] children)

  Sets the children of this `TreeGraphNode`. If given `null`, this method sets the node's children to the canonical zero-length Tree[] array.

  Overrides:
    `setChildren` in class `Tree`
  Parameters:
    `children` - an array of child trees
  See Also:
    `Tree.setChildren(List)`

### setChildren

    public void setChildren(List<? extends Tree> childTreesList)

  Set the children of this tree node to the given list. This method is implemented in the `Tree` class by converting the `List` into a tree array and calling the array-based method. Subclasses which use a `List`-based representation of tree children should override this method. This implementation allows the case that the `List` is `null`: it yields a node with no children (represented by a canonical zero-length children() array).

  Overrides:
    `setChildren` in class `Tree`
  Parameters:
    `childTreesList` - A list of trees to become children of the node. This method does not retain the List that you pass it (copying is done), but it will retain the individual children (they are not copied).
  See Also:
    `Tree.setChildren(Tree[])`

### treeGraph

    protected TreeGraph treeGraph()

  Get the `TreeGraph` of which this node is a part.

### setTreeGraph

    protected void setTreeGraph(TreeGraph tg)

  Set pointer to the `TreeGraph` of which this node is a part. Operates recursively to set pointer for all descendants too.

### addArc

    public <GR extends GrammaticalRelation.GrammaticalRelationAnnotation> boolean addArc(Class<GR> arcLabel,
                                                                                         TreeGraphNode node)

  Add a labeled arc from this node to the argument node.

  Parameters:
    `arcLabel` - the `Class<? extends GrammaticalRelationAnnotation>` with which the new arc is to be labeled.
    `node` - the `TreeGraphNode` to which the new arc should point.
  Returns:
    `true` iff the arc did not already exist.

### followArcToSet

    public Set<TreeGraphNode> followArcToSet(Class<? extends GrammaticalRelation.GrammaticalRelationAnnotation> arcLabel)

  Returns the `Set` of `TreeGraphNode`s to which there exist arcs bearing the specified label from this node, or `null` if no such nodes exist.

  Parameters:
    `arcLabel` - the `Object` which labels the arc(s) to be followed.
  Returns:
    a `Set` containing only and all the `TreeGraphNode`s to which there exist arcs bearing the specified label from this node.

### followArcToNode

    public TreeGraphNode followArcToNode(Class<? extends GrammaticalRelation.GrammaticalRelationAnnotation> arcLabel)

  Returns a single `TreeGraphNode` to which there exists an arc bearing the specified label from this node, or `null` if no such node exists. If more than one such node exists, this method will return an arbitrary node from among them; if this is a possibility, you might want to use ``followArcToSet`` instead.

  Parameters:
    `arcLabel` - a `Object` containing the label of the arc(s) to be followed
  Returns:
    a `TreeGraphNode` to which there exists an arc bearing the specified label from this node

### arcLabelsToNode

    public Set<Class<? extends GrammaticalRelation.GrammaticalRelationAnnotation>> arcLabelsToNode(TreeGraphNode destNode)

  Finds all arcs between this node and `destNode`, and returns the `Set` of `Object`s which label those arcs. If no such arcs exist, returns an empty `Set`.

  Parameters:
    `destNode` - the destination node
  Returns:
    the `Set` of `Object`s which label arcs between this node and `destNode`

### arcLabelToNode

    public Class<? extends GrammaticalRelation.GrammaticalRelationAnnotation> arcLabelToNode(TreeGraphNode destNode)

  Returns the label of a single arc between this node and `destNode`, or `null` if no such arc exists. If more than one such arc exists, this method will return an arbitrary arc label from among them; if this is a possibility, you might want to use ``arcLabelsToNode`` instead.

  Parameters:
    `destNode` - the destination node
  Returns:
    the `Object` which labels one arc between this node and `destNode`

### percolateHeads

    public void percolateHeads(HeadFinder hf)

  Uses the specified ``HeadFinder`` to determine the heads for this node and all its descendants, and to store references to the head word node and head tag node in this node's ``CyclicCoreLabel`` and the `CyclicCoreLabel`s of all its descendants.

  Note that, in contrast to ``Tree.percolateHeads()``, which assumes ``CategoryWordTag`` labels and therefore stores head words and head tags merely as `String`s, this method stores references to the actual nodes. This mitigates potential problems in sentences which contain the same word more than once.

  Overrides:
    `percolateHeads` in class `Tree`
  Parameters:
    `hf` - The headfinding algorithm to use

### dependencies

    public Set<Dependency<Label,Label,Object>> dependencies(Filter<Dependency<Label,Label,Object>> f,
                                                            HeadFinder hf)

  Return a set of node-node dependencies, represented as Dependency objects, for the Tree.

  Overrides:
    `dependencies` in class `Tree`
  Parameters:
    `hf` - The HeadFinder to use to identify the head of constituents. If this is `null`, then nodes are assumed to already be marked with their heads.
    `f` - Dependencies are excluded for which the Dependency is not accepted by the Filter
  Returns:
    Set of dependencies (each a `Dependency`)

### headWordNode

    public TreeGraphNode headWordNode()

  Return the node containing the head word for this node (or `null` if none), as recorded in this node's ``CyclicCoreLabel``. (In contrast to ``CategoryWordTag``, we store head words and head tags as references to nodes, not merely as `String`s.)

  Returns:
    the node containing the head word for this node

### headTagNode

    public TreeGraphNode headTagNode()

  Return the node containing the head tag for this node (or `null` if none), as recorded in this node's ``CyclicCoreLabel``. (In contrast to ``CategoryWordTag``, we store head words and head tags as references to nodes, not merely as `String`s.)

  Returns:
    the node containing the head tag for this node

### highestNodeWithSameHead

    public TreeGraphNode highestNodeWithSameHead()

  Checks the node's ancestors to find the highest ancestor with the same `headWordNode` as this node.

### treeFactory

    public TreeFactory treeFactory()

  Returns a `TreeFactory` that produces `TreeGraphNode`s. The `Label` of `this` is examined, and providing it is not `null`, a `LabelFactory` which will produce that kind of `Label` is supplied to the `TreeFactory`. If the `Label` is `null`, a `CyclicCoreLabel.factory()` will be used. The factories returned on different calls are different: a new one is allocated each time.

  Specified by:
    `treeFactory` in class `Tree`
  Returns:
    a factory to produce treegraphs

### factory

    public static TreeFactory factory()

  Return a `TreeFactory` that produces trees of type `TreeGraphNode`. The factory returned is always the same one (a singleton).

  Returns:
    a factory to produce treegraphs

### factory

    public static TreeFactory factory(LabelFactory lf)

  Return a `TreeFactory` that produces trees of type `TreeGraphNode`, with the `Label` made by the supplied `LabelFactory`. The factory returned is a different one each time.

  Parameters:
    `lf` - The `LabelFactory` to use
  Returns:
    a factory to produce treegraphs

### toPrettyString

    public String toPrettyString(int indentLevel)

  Returns a `String` representation of this node and its subtree with one node per line, indented according to `indentLevel`.

  Parameters:
    `indentLevel` - how many levels to indent (0 for root node)
  Returns:
    `String` representation of this subtree

### toOneLineString

    public String toOneLineString()

  Returns a `String` representation of this node and its subtree as a one-line parenthesized list.

  Returns:
    `String` representation of this subtree

### toPrimes

    public String toPrimes()

### toString

    public String toString()

  Description copied from class: `Tree`
  Converts parse tree to string in Penn Treebank format.

  Implementation note: Internally, the method gains efficiency by chaining use of a single `StringBuilder` through all the printing.

  Specified by:
    `toString` in interface `Label`
  Overrides:
    `toString` in class `Tree`
  Returns:
    the tree as a bracketed list on one line

### main

    public static void main(String[] args)

  Just for testing.

Stanford NLP Group