edu.stanford.nlp.trees
Class Tree

java.lang.Object
  extended byjava.util.AbstractCollection
      extended byedu.stanford.nlp.trees.Tree
All Implemented Interfaces:
Collection, Comparable, Labeled, Scored, Serializable
Direct Known Subclasses:
LabeledScoredTreeLeaf, LabeledScoredTreeNode, SimpleTree

public abstract class Tree
extends AbstractCollection
implements Labeled, Scored, Comparable, Serializable

The abstract class Tree is used to collect all of the tree types, and acts as a generic composite type. This is the standard implementation of inheritance-based polymorphism. Trees, like standard containers, are not parametrized in any way by the type of object in the data field. All Tree objects support accessors for their children (a Tree[]), their data (a Label), and their score (a double). However, different concrete implementations may or may not include the latter two, in which case a default value is returned. The class Tree defines no data fields. The two abstract methods that must be implemented are: children(), and treeFactory(). Note that setChildren(Tree[]) is now an optional operation, whereas it was previously was required to be implemented. There is now support for finding the parent of a tree. This may be done by search from a tree root, or via a directly stored parent. The Tree class now implements the Collection interface: in terms of this, each node of the tree is an element of the collection; hence one can explore the tree by using the methods of this interface. A Tree is regarded as a read-only Collection (even though the Tree class has various methods that modify trees). Moreover, the implementation is not thread-safe: no attempt is made to detect and report concurrent modifications.

Author:
Christopher Manning, Dan Klein
See Also:
Serialized Form

Field Summary
protected static Tree[] ZEROCHILDREN
           
 
Constructor Summary
Tree()
           
 
Method Summary
abstract  Tree[] children()
          Returns an array of children for the current node.
 int compareTo(Object o)
           
 Set constituents()
          Returns the Constituents generated by the parse tree.
 Set constituents(ConstituentFactory cf)
          Returns the Constituents generated by the parse tree.
 Tree deepCopy()
          Create a deep copy of the tree.
 Tree deepCopy(TreeFactory tf)
          Create a deep copy of the tree.
 Set dependencies()
          Return a set of Word-Word dependencies, represented as edu.stanford.nlp.util.Pair objects (where the first() is the head) for the Tree.
 Set dependencies(Filter f)
          Return a set of Word-Word dependencies, represented as Pair objects (where the first() is the head) for the Tree.
 Set dependencies(Filter f, HeadFinder hf)
          Return a set of Word-Word dependencies, represented as Pair objects (where the first() is the head) for the Tree.
 Set dependencies(HeadFinder hf)
          Return a set of Word-Word dependencies, represented as Pair objects (where the first() is the head) for the Tree.
 int depth()
          Finds the depth of the tree.
 boolean dominates(Tree t)
          returns true if this dominates the Tree passed in as an argument.
 List dominationPath(Tree t)
          returns the path of nodes leading down to a dominated node, including this and the dominated node itself.
 boolean equals(Object o)
          Implements equality for Tree's.
 Tree firstChild()
          Returns the first child of a tree, or null if none.
 Tree flatten()
          Return a flattened version of a tree.
 Tree flatten(TreeFactory tf)
          Return a flattened version of a tree.
 Tree getChild(int i)
          Return the child at some daughter index.
 List getChildrenAsList()
          Returns an array of children for the current node.
 int hashCode()
          Implements a hashCode for Tree's.
 Tree headPreTerminal(HeadFinder hf)
          Returns the preterminal tree that is the head of the tree.
 Tree headTerminal(HeadFinder hf)
          Returns the tree leaf that is the head of the tree.
 void indentedListPrint()
          Indented list printing of a tree.
 void indentedListPrint(PrintWriter pw)
          Indented list printing of a tree.
 void indentedListPrint(String indent, String pad, PrintWriter pw)
          Indented list printing of a tree.
 int indexOf(Tree tree)
          Returns the position of a Tree in the children list, if present, or -1 if it is not present.
 void insertDtr(Tree dtr, int position)
          insert dtr after position existing daughters in this.
 boolean isLeaf()
          Says whether a node is a leaf.
 boolean isPhrasal()
          Return whether this node is a phrasal node or not.
 boolean isPrePreTerminal()
          Return whether all the children of this node are preterminals or not.
 boolean isPreTerminal()
          Return whether this node is a preterminal or not.
 Iterator iterator()
          Returns an iterator over the nodes of the tree.
 Label label()
          Returns the label associated with the current node, or null if there is no label.
 Collection labels()
          Get the set of all node and leaf Labels, null or otherwise, contained in the tree.
 Tree lastChild()
          Returns the last child of a tree, or null if none.
 String nodeString()
           
 Tree parent()
          Return the parent of the tree node.
 Tree parent(Tree root)
          Return the parent of the tree node.
 void pennPrint()
          Print the tree as done in Penn Treebank merged files.
 void pennPrint(PrintStream ps)
          Print the tree as done in Penn Treebank merged files.
 void pennPrint(PrintWriter pw)
          Print the tree as done in Penn Treebank merged files.
 void percolateHeads(HeadFinder hf)
          Finds the heads of the tree.
 List preTerminalYield()
          Gets the preterminal yield (i.e., tags) of the tree.
 List preTerminalYield(List y)
          Gets the preterminal yield (i.e., tags) of the tree.
 Tree prune(Filter filter)
          Creates a deep copy of the tree, where all nodes that the filter does not accept and all children of such nodes are pruned.
 Tree prune(Filter filter, TreeFactory tf)
          Creates a deep copy of the tree, where all nodes that the filter does not accept and all children of such nodes are pruned.
 double score()
          Returns the score associated with the current node, or NaN if there is no score.
 void setChildren(List childTreesList)
          Set the children of this tree node to the given list.
 void setChildren(Tree[] children)
          Set the children of this node to be the children given in the array.
 void setLabel(Label label)
          Sets the label associated with the current node, if there is one.
 void setLabels(Collection c)
          Sets the labels associated with this object.
 void setScore(double score)
          Sets the score associated with the current node, if there is one.
 int size()
          Returns the number of nodes the tree contains.
 Tree spliceOut(Filter nodeFilter)
          Creates a (partial) deep copy of the tree, where all nodes that the filter does not accept are spliced out.
 Tree spliceOut(Filter nodeFilter, TreeFactory tf)
          Creates a (partial) deep copy of the tree, where all nodes that the filter does not accept are spliced out.
 List subTreeList()
          Get the list of all subtrees inside the tree by returning a tree rooted at each node.
 Set subTrees()
          Get the set of all subtrees inside the tree by returning a tree rooted at each node.
 Collection subTrees(Collection n)
          Add the set of all subtrees inside a tree (including the tree itself) to the given Collection.
 Set taggedDependencies()
          Return a Set of TaggedWord-TaggedWord dependencies, represented as edu.stanford.nlp.util.Pair objects (where the first() is the head) for the Tree.
 Set taggedDependencies(Filter f)
          Return a set of TaggedWord-TaggedWord dependencies, represented as Pair objects (where the first() is the head) for the Tree.
 Sentence taggedYield()
          Gets the tagged yield of the tree.
 List taggedYield(List ty)
          Gets the tagged yield of the tree -- that is, get the preterminals as well as the terminals.
 String toString()
          Converts parse tree to string in Penn Treebank format.
 StringBuffer toStringBuffer(StringBuffer sb)
          Appends the printed form of a parse tree (as a bracketed String) to a StringBuffer.
 Tree transform(TreeTransformer transformer)
          Create a transformed Tree.
 Tree transform(TreeTransformer transformer, TreeFactory tf)
          Create a transformed Tree.
abstract  TreeFactory treeFactory()
          Return a TreeFactory that produces trees of the appropriate type.
static Tree valueOf(String str)
          This gives you a tree from a String representation (as a bracketed Tree, of the kind produced by toString(), pennPrint(), or as in the Penn Treebank).
static Tree valueOf(String str, TreeReaderFactory trf)
          This gives you a tree from a String representation (as a bracketed Tree, of the kind produced by toString(), pennPrint(), or as in the Penn Treebank.
 Sentence yield()
          Gets the yield of the tree.
 List yield(List y)
          Gets the yield of the tree.
 
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

ZEROCHILDREN

protected static final Tree[] ZEROCHILDREN
Constructor Detail

Tree

public Tree()
Method Detail

isLeaf

public boolean isLeaf()
Says whether a node is a leaf. Can be used on an arbitrary Tree. Being a leaf is defined as having no children. This must be implemented as returning a zero-length Tree[] array for children(). This is the preferred alternative to running meta checks on types, as it works independent of Tree implementation.

Returns:
true if this object is a leaf

isPreTerminal

public boolean isPreTerminal()
Return whether this node is a preterminal or not. A preterminal is defined to be a node with one child which is itself a leaf.

Returns:
true if the node is a preterminal; false otherwise

isPrePreTerminal

public boolean isPrePreTerminal()
Return whether all the children of this node are preterminals or not. A preterminal is defined to be a node with one child which is itself a leaf. Considered false if the node has no children

Returns:
true if the node is a prepreterminal; false otherwise

isPhrasal

public boolean isPhrasal()
Return whether this node is a phrasal node or not. A phrasal node is defined to be a node which is not a leaf or a preterminal. Worded positively, this means that it must have two or more children, or one child that is not a leaf.

Returns:
true if the node is phrasal; false otherwise

equals

public boolean equals(Object o)
Implements equality for Tree's. Two Tree objects are equal if they have equal Labels, the same number of children, and their children are pairwise equal.

Specified by:
equals in interface Collection
Parameters:
o - The object to compare with
Returns:
Whether two things are equal

hashCode

public int hashCode()
Implements a hashCode for Tree's. Two trees should have the same hashcode if they are equal, so we hash on the label, the label and the children's labels.

Specified by:
hashCode in interface Collection
Returns:
The hash code

compareTo

public int compareTo(Object o)
Specified by:
compareTo in interface Comparable

indexOf

public int indexOf(Tree tree)
Returns the position of a Tree in the children list, if present, or -1 if it is not present. Trees are checked for presence with equals().

Parameters:
tree - The tree to look for in children list
Returns:
Its index in the list or -1

children

public abstract Tree[] children()
Returns an array of children for the current node. If there are no children (if the node is a leaf), this must return a Tree[] array of length 0. A null children() value for tree leaves was previously supported, but no longer is. A caller may assume that either isLeaf() returns true, or this node has a nonzero number of children.

Returns:
The children of the node
See Also:
getChildrenAsList()

getChildrenAsList

public List getChildrenAsList()
Returns an array of children for the current node. If there are no children, then a (non-null) List of size 0 will be returned.

Returns:
The children of the node

setChildren

public void setChildren(Tree[] children)
Set the children of this node to be the children given in the array. This is an optional operation; by default it is unsupported. Note for subclasses that if there are no children, the children() method must return a Tree[] array of length 0. This class gives subclasses access to a protected ZEROCHILDREN canonical zero-length Tree[] array to represent zero children, but it is not required that leaf nodes use this particular zero-length array to represent a leaf node.

Parameters:
children - The array of children, each a Tree
See Also:
setChildren(List)

setChildren

public void setChildren(List 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).

Parameters:
childTreesList - list of trees to become children
See Also:
setChildren(Tree[])

label

public Label label()
Returns the label associated with the current node, or null if there is no label. The default implementation always returns null.

Specified by:
label in interface Labeled
Returns:
The label of the node

setLabel

public void setLabel(Label label)
Sets the label associated with the current node, if there is one.

Specified by:
setLabel in interface Labeled
Parameters:
label - The label

score

public double score()
Returns the score associated with the current node, or NaN if there is no score. The default implementation returns NaN.

Specified by:
score in interface Scored
Returns:
The score

setScore

public void setScore(double score)
Sets the score associated with the current node, if there is one.

Parameters:
score - The score

firstChild

public Tree firstChild()
Returns the first child of a tree, or null if none.

Returns:
The first child

lastChild

public Tree lastChild()
Returns the last child of a tree, or null if none.

Returns:
The last child

constituents

public Set constituents()
Returns the Constituents generated by the parse tree.

Returns:
a Set of the constituents as constituents of type SimpleConstituent

constituents

public Set constituents(ConstituentFactory cf)
Returns the Constituents generated by the parse tree. The Constituents of a sentence include the preterminal categories but not the leaves.

Parameters:
cf - ConstituentFactory used to build the Constituent objects
Returns:
a Set of the constituents as SimpleConstituent type (in the current implementation, a HashSet

toStringBuffer

public StringBuffer toStringBuffer(StringBuffer sb)
Appends the printed form of a parse tree (as a bracketed String) to a StringBuffer. The implementation of this may be more efficient than for toString() on complex trees.

Parameters:
sb - The StringBuffer to append to
Returns:
StringBuffer returns the StringBuffer

toString

public String toString()
Converts parse tree to string in Penn Treebank format. Get efficiency by chaining a single StringBuffer through it all.

Returns:
the tree as a bracketed list on one line

indentedListPrint

public void indentedListPrint()
Indented list printing of a tree. The tree is printed in an indented list notation, with nodel labels followed by node scores.


indentedListPrint

public void indentedListPrint(PrintWriter pw)
Indented list printing of a tree. The tree is printed in an indented list notation, with nodel labels followed by node scores.

Parameters:
pw - The PrintWriter to print the tree to

indentedListPrint

public void indentedListPrint(String indent,
                              String pad,
                              PrintWriter pw)
Indented list printing of a tree. The tree is printed in an indented list notation, with nodel labels followed by node scores. String parameters are used rather than integer levels for efficiency.

Parameters:
indent - The base String (normally just spaces) to print before each line of tree
pad - The additional String (normally just more spaces) to add when going to a deeper level of Tree.
pw - The PrintWriter to print the tree to

nodeString

public String nodeString()

pennPrint

public void pennPrint(PrintWriter pw)
Print the tree as done in Penn Treebank merged files. The formatting should be exactly the same, but we don't print the trailing whitespace found in Penn Treebank trees. The basic deviation from a bracketed indented tree is to in general collapse the printing of adjacent preterminals onto one line of tags and words. Additional complexities are that conjunctions (tag CC) are not collapsed in this way, and that the unlabeled outer brackets are collapsed onto the same line as the next bracket down.

Parameters:
pw - The tree is printed to this PrintWriter

pennPrint

public void pennPrint(PrintStream ps)
Print the tree as done in Penn Treebank merged files. The formatting should be exactly the same, but we don't print the trailing whitespace found in Penn Treebank trees. The basic deviation from a bracketed indented tree is to in general collapse the printing of adjacent preterminals onto one line of tags and words. Additional complexities are that conjunctions (tag CC) are not collapsed in this way, and that the unlabeled outer brackets are collapsed onto the same line as the next bracket down.

Parameters:
ps - The tree is printed to this PrintStream

pennPrint

public void pennPrint()
Print the tree as done in Penn Treebank merged files. The formatting should be exactly the same, but we don't print the trailing whitespace found in Penn Treebank trees. The tree is printed to System.out. The basic deviation from a bracketed indented tree is to in general collapse the printing of adjacent preterminals onto one line of tags and words. Additional complexities are that conjunctions (tag CC) are not collapsed in this way, and that the unlabeled outer brackets are collapsed onto the same line as the next bracket down.


depth

public int depth()
Finds the depth of the tree. The depth is defined as the length of the longest path from this node to a leaf node. Leaf nodes have depth zero.

Returns:
the depth

headTerminal

public Tree headTerminal(HeadFinder hf)
Returns the tree leaf that is the head of the tree.

Parameters:
hf - The headfinding algorithm to use
Returns:
The head tree leaf if any, else null

headPreTerminal

public Tree headPreTerminal(HeadFinder hf)
Returns the preterminal tree that is the head of the tree. See isPreTerminal() for the definition of a preterminal node. Beware that some tree nodes may have no preterminal head.

Parameters:
hf - The headfinding algorithm to use
Returns:
The head preterminal tree, if any, else null

percolateHeads

public void percolateHeads(HeadFinder hf)
Finds the heads of the tree. This code assumes that the label does store and return sensible values for the category, word, and tag. It will be a no-op otherwise. The tree is modified. The routine assumes the Tree has word leaves and tag preterminals, and copies their category to word and tag respectively, if they have a null value.

Parameters:
hf - The headfinding algorithm to use

dependencies

public Set dependencies()
Return a set of Word-Word dependencies, represented as edu.stanford.nlp.util.Pair objects (where the first() is the head) for the Tree. This will only give useful results if the internal tree node labels support HasWord and head percolation has already been done (see percolateHeads()).

Returns:
Set of dependencies (each a Pair)

dependencies

public Set dependencies(Filter f)
Return a set of Word-Word dependencies, represented as Pair objects (where the first() is the head) for the Tree. This will only give useful results if the internal tree node labels support HasWord and head percolation has already been done (see percolateHeads()).

Parameters:
f - Dependencies are excluded for which the Pair is not accepted by the Filter
Returns:
Set of dependencies (each a Pair)

dependencies

public Set dependencies(HeadFinder hf)
Return a set of Word-Word dependencies, represented as Pair objects (where the first() is the head) for the Tree.

Parameters:
hf - The HeadFinder to use to identify the head of constituents
Returns:
Set of dependencies (each a Pair)

dependencies

public Set dependencies(Filter f,
                        HeadFinder hf)
Return a set of Word-Word dependencies, represented as Pair objects (where the first() is the head) for the Tree.

Parameters:
f - Dependencies are excluded for which the Pair is not accepted by the Filter
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.
Returns:
Set of dependencies (each a Pair)

taggedDependencies

public Set taggedDependencies()
Return a Set of TaggedWord-TaggedWord dependencies, represented as edu.stanford.nlp.util.Pair objects (where the first() is the head) for the Tree. This will only give useful results if the internal tree node labels support HasWord and HasTag, and head percolation has already been done (see percolateHeads()).

Returns:
Set of dependencies (each a Pair)

taggedDependencies

public Set taggedDependencies(Filter f)
Return a set of TaggedWord-TaggedWord dependencies, represented as Pair objects (where the first() is the head) for the Tree. This will only give useful results if the internal tree node labels support HasWord and head percolation has already been done (see percolateHeads()).

Implementation note: It would be nice to generalize this with dependencies() to use a LabelFactory, but it seems impossible with the current setup.

Parameters:
f - Dependencies are excluded for which the Pair is not accepted by the Filter
Returns:
Set of dependencies (each a Pair)

yield

public Sentence yield()
Gets the yield of the tree. The Label of all leaf nodes is returned as a list ordered by the natural left to right order of the leaves. Null values, if any, are inserted into the list like any other value.

Returns:
a List of the data in the tree's leaves.

yield

public List yield(List y)
Gets the yield of the tree. The Label of all leaf nodes is returned as a list ordered by the natural left to right order of the leaves. Null values, if any, are inserted into the list like any other value. This has been rewritten to thread, so only one List is used.

Parameters:
y - The list in which the yield of the tree will be placed. Normally, this will be empty when the routine is called, but if not, the new yield is added to the end of the list.
Returns:
a List of the data in the tree's leaves.

taggedYield

public Sentence taggedYield()
Gets the tagged yield of the tree. The Label of all leaf nodes is returned as a list ordered by the natural left to right order of the leaves. Null values, if any, are inserted into the list like any other value.

Returns:
a List of the data in the tree's leaves.

taggedYield

public List taggedYield(List ty)
Gets the tagged yield of the tree -- that is, get the preterminals as well as the terminals. The Label of all leaf nodes is returned as a list ordered by the natural left to right order of the leaves. Null values, if any, are inserted into the list like any other value. This has been rewritten to thread, so only one List is used.

Parameters:
ty - The list in which the tagged yield of the tree will be placed. Normally, this will be empty when the routine is called, but if not, the new yield is added to the end of the list.
Returns:
a List of the data in the tree's leaves.

preTerminalYield

public List preTerminalYield()
Gets the preterminal yield (i.e., tags) of the tree. All data in preleaf nodes is returned as a list ordered by the natural left to right order of the tree. Null values, if any, are inserted into the list like any other value. Pre-leaves are nodes of height 1.

Returns:
a List of the data in the tree's pre-leaves.

preTerminalYield

public List preTerminalYield(List y)
Gets the preterminal yield (i.e., tags) of the tree. All data in preleaf nodes is returned as a list ordered by the natural left to right order of the tree. Null values, if any, are inserted into the list like any other value. Pre-leaves are nodes of height 1.

Parameters:
y - The list in which the preterminals of the tree will be placed. Normally, this will be empty when the routine is called, but if not, the new yield is added to the end of the list.
Returns:
a List of the data in the tree's pre-leaves.

labels

public Collection labels()
Get the set of all node and leaf Labels, null or otherwise, contained in the tree.

Specified by:
labels in interface Labeled
Returns:
the Collection (actually, Set) of all values in the tree.

setLabels

public void setLabels(Collection c)
Description copied from interface: Labeled
Sets the labels associated with this object.

Specified by:
setLabels in interface Labeled
Parameters:
c - The set of Label values

flatten

public Tree flatten()
Return a flattened version of a tree. In many circumstances, this will just return the tree, but if the tree is something like a binarized version of a dependency grammar tree, then it will be flattened back to a dependency grammar tree representation. Formally, a node will be removed from the tree when: it is not a terminal or preterminal, and its label()equal() to the label() of its parent, and all its children will then be promoted to become children of the parent (in the same position in the sequence of daughters.

Returns:
A flattened version of this tree.

flatten

public Tree flatten(TreeFactory tf)
Return a flattened version of a tree. In many circumstances, this will just return the tree, but if the tree is something like a binarized version of a dependency grammar tree, then it will be flattened back to a dependency grammar tree representation. Formally, a node will be removed from the tree when: it is not a terminal or preterminal, and its label()equal() to the label() of its parent, and all its children will then be promoted to become children of the parent (in the same position in the sequence of daughters.

Note: In the current implementation, the tree structure is mainly duplicated, but the links between preterminals and terminals aren't.

Parameters:
tf - TreeFactory used to create tree structure for flattened tree
Returns:
A flattened version of this tree.

subTrees

public Set subTrees()
Get the set of all subtrees inside the tree by returning a tree rooted at each node. These are not copies, but all share structure. The tree is regarded as a subtree of itself.

Returns:
the Set of all subtrees in the tree.

subTreeList

public List subTreeList()
Get the list of all subtrees inside the tree by returning a tree rooted at each node. These are not copies, but all share structure. The tree is regarded as a subtree of itself.

Returns:
the List of all subtrees in the tree.

subTrees

public Collection subTrees(Collection n)
Add the set of all subtrees inside a tree (including the tree itself) to the given Collection.

Parameters:
n - A collection of nodes to which the subtrees will be added
Returns:
The collection parameter with the subtrees added

deepCopy

public Tree deepCopy()
Create a deep copy of the tree. The entire structure is recursively copied, but label data themselves are not cloned. The copy is built using a TreeFactory that will produce a Tree like the input one.

Returns:
a deep structural copy of the tree.

deepCopy

public Tree deepCopy(TreeFactory tf)
Create a deep copy of the tree. The entire structure is recursively copied, but label data themselves are not cloned. By specifying an appropriate TreeFactory, this method can be used to change the type of a Tree.

Parameters:
tf - The TreeFactory to be used for creating the returned Tree
Returns:
a deep structural copy of the tree.

transform

public Tree transform(TreeTransformer transformer)
Create a transformed Tree. The tree is traversed in a depth-first, left-to-right order, and the TreeTransformer is called on each node. It returns some Tree. The transformed tree has a new tree structure (i.e., a "deep copy" is done), but it will usually share its labels with the original tree.

Parameters:
transformer - The function that transforms tree nodes or subtrees
Returns:
a transformation of this Tree

transform

public Tree transform(TreeTransformer transformer,
                      TreeFactory tf)
Create a transformed Tree. The tree is traversed in a depth-first, left-to-right order, and the TreeTransformer is called on each node. It returns some Tree. The transformed tree has a new tree structure (i.e., a "deep copy" is done), but it will usually share its labels with the original tree.

Parameters:
transformer - The function that transforms tree nodes or subtrees
tf - The TreeFactory which will be used for creating new nodes for the returned Tree
Returns:
a transformation of this Tree

spliceOut

public Tree spliceOut(Filter nodeFilter)
Creates a (partial) deep copy of the tree, where all nodes that the filter does not accept are spliced out. If the result is not a tree (that is, it's a forest), an empty root node is generated.

Parameters:
nodeFilter - a Filter method which returns true to mean keep this node, false to mean delete it
Returns:
a filtered copy of the tree

spliceOut

public Tree spliceOut(Filter nodeFilter,
                      TreeFactory tf)
Creates a (partial) deep copy of the tree, where all nodes that the filter does not accept are spliced out. That is, the particular modes for which the Filter returns false are removed from the Tree, but those nodes' children are kept (assuming they pass the Filter, and they are added in the appropriate left-to-right ordering as new children of the parent node. If the root node is deleted, so that the result would not be a tree (that is, it's a forest), an empty root node is generated. If nothing is accepted, null is returned.

Parameters:
nodeFilter - a Filter method which returns true to mean keep this node, false to mean delete it
tf - A TreeFactory for making new trees. Used if the root node is deleted.
Returns:
a filtered copy of the tree.

prune

public Tree prune(Filter filter)
Creates a deep copy of the tree, where all nodes that the filter does not accept and all children of such nodes are pruned. If all ' of a node's children are pruned, that node is cut as well. A Filter can assume that it will not be called with a null argument.

Parameters:
filter - the filter to be apply
Returns:
a filtered copy of the tree.

prune

public Tree prune(Filter filter,
                  TreeFactory tf)
Creates a deep copy of the tree, where all nodes that the filter does not accept and all children of such nodes are pruned. If all of a node's children are pruned, that node is cut as well. A Filter can assume that it will not be called with a null argument.

Parameters:
filter - the filter to be apply
tf - the TreeFactory to be used to make new Tree nodes if needed
Returns:
a filtered copy of the tree, including the possibility of null if the root node of the tree is filtered

treeFactory

public abstract TreeFactory treeFactory()
Return a TreeFactory that produces trees of the appropriate type.

Returns:
A factory to produce Trees

parent

public Tree parent()
Return the parent of the tree node. This routine may return null meaning simply that the implementation doesn't know how to determine the parent node, rather than there is no such node.

Returns:
The parent Tree node or null
See Also:
parent(Tree)

parent

public Tree parent(Tree root)
Return the parent of the tree node. This routine will traverse a tree (depth first) from the given root, and will correctly find the parent, regardless of whether the concrete class stores parents. It will only return null if this node is the root node, or if this node is not contained within the tree rooted at root.

Parameters:
root - The root node of the whole Tree
Returns:
the parent Tree node if any; else null

size

public int size()
Returns the number of nodes the tree contains. This method implements the size() function required by the Collections interface. The size of the tree is the number of nodes it contains (of all types, including the leaf nodes and the root).

Specified by:
size in interface Collection
Returns:
The size of the tree
See Also:
depth()

iterator

public Iterator iterator()
Returns an iterator over the nodes of the tree. This method implements the iterator() method required by the Collections interface. It does a preorder (children after node) traversal of the tree. (A possible extension to the class at some point would be to allow different traversal orderings via variant iterators.)

Specified by:
iterator in interface Collection
Returns:
An interator over the nodes of the tree

valueOf

public static Tree valueOf(String str)
                    throws IOException
This gives you a tree from a String representation (as a bracketed Tree, of the kind produced by toString(), pennPrint(), or as in the Penn Treebank). It's not the most efficient thing to do for heavy duty usage. The Tree returned is created by a StringLabeledScoredTreeReaderFactory.

Parameters:
str - The tree as a bracketed list in a String.
Returns:
The Tree
Throws:
IOException - If Tree format is not valid

valueOf

public static Tree valueOf(String str,
                           TreeReaderFactory trf)
                    throws IOException
This gives you a tree from a String representation (as a bracketed Tree, of the kind produced by toString(), pennPrint(), or as in the Penn Treebank. It's not the most efficient thing to do for heavy duty usage.

Parameters:
str - The tree as a bracketed list in a String.
Returns:
The Tree
Throws:
IOException - If Tree format is not valid

getChild

public Tree getChild(int i)
Return the child at some daughter index.

Parameters:
i - The daughter index
Returns:
The tree at that daughter index

dominates

public boolean dominates(Tree t)
returns true if this dominates the Tree passed in as an argument.


dominationPath

public List dominationPath(Tree t)
returns the path of nodes leading down to a dominated node, including this and the dominated node itself. Returns null if t is not dominated by this. Object equality (==) is the relevant criterion.


insertDtr

public void insertDtr(Tree dtr,
                      int position)
insert dtr after position existing daughters in this.



Stanford NLP Group