gshute.trees
Class TreeEdge

java.lang.Object
  |
  +--gshute.trees.TreeEdge

public class TreeEdge
extends java.lang.Object

A TreeEdge is an edge between two nodes in a binary tree, providing operations for navigation and obtaining information about nearby nodes, and providing support for implementing iterators. An edge is directed downward, either from an external node to the root node of a tree, or from a node to one of its children. The root node or the child may be external (null).

For an edge from an external node to the root, the from node of the edge is null, the to node is the root, and the edge type is TreeEdge.ROOT. For an edge from a parent node to a child, the from node of the edge is the parent node, the to node is the child, and the edge type is either TreeEdge.LEFT or TreeEdge.RIGHT.

If the underlying tree is modified, a tree edge retains its type and its from node is unaltered. Its to node may change as a result of the modification. If the modification results in removal of the from node then the edge will be left in an invalid state.

When an edge used to implement an iterator, the next element of the iteration is the to node of the edge. The to node of the edge is non-null until the iteration is complete. The current implementation of tree edges only supports the Iterator interface. A planned modification will add support for the ListIterator interface.


Field Summary
protected  int edgeType
          edgeType is the type of this edge.
protected  TreeNode from
          from is the binary tree node from which this edge originates.
static int LEFT
          TreeEdge.LEFT is the type constant for describing an edge from a node to its left child.
static int RIGHT
          TreeEdge.RIGHT is the type constant for describing an edge from a node to its right child.
static int ROOT
          TreeEdge.ROOT is the type constant for describing the edge from an external node to the root.
protected  Tree tree
          tree is the binary tree that this edge belongs to.
 
Constructor Summary
TreeEdge(Tree t)
          new TreeEdge(t) returns a new tree edge to the root of t.
 
Method Summary
 void advanceInorder()
          e.advanceInorder() moves e to the edge position whose to node is the inorder successor of the current to node.
 void advancePostorder()
          e.advancePostorder() moves e to the edge position whose to node is the postorder successor of the current to node.
 void advancePreorder()
          e.advancePreorder() moves e to the edge position whose to node is the preorder successor of the current to node.
 TreeEdge getCopy()
          e.getCopy() returns a copy of e.
 int getEdgeType()
          e.getEdgeType() returns the edge type of e.
 TreeNode getFarNiece()
          e.getFarNiece() returns the far child of the sibling of the to node of e.
 TreeNode getFrom()
          e.getFrom() returns the node that e originates from.
 TreeNode getNearNiece()
          e.getNearNiece() returns the near child of the sibling of the to node of e.
 TreeNode getSibling()
          e.getSibling() returns the sibling of the to node of e.
 TreeNode getTo()
          e.getTo() returns the node that e points to.
 Tree getTree()
          e.getTree() returns the tree that e belongs to.
 void goLeft()
          e.goLeft() moves e down and to the left.
 void goLeftToLastNonNull()
          e.goLeftToLastNonNull() moves e left, stopping at the last non-null in the chain of left edges from its current position.
 void goLeftToNull()
          e.goLeftToNull() moves e left, stopping at the last edge in the chain of left edges from its current position.
 void goRight()
          e.goRight() moves e down and to the right.
 void goRightToLastNonNull()
          e.goRightToLastNonNull() moves e right, stopping at the last non-null in the chain of right edges from its current position.
 void goRightToNull()
          e.goRightToNull() moves e right, stopping at the last edge in the chain of right edges from its current position.
 void goToRoot()
          e.goToRoot() moves e to the root edge of its tree.
 void goUp()
          e.goUp() moves e one step towards the root of its tree.
 void goUpToNonLeft()
          e.goUpToNonLeft() moves e up to the lowest non-left tree edge at or above e.
 void goUpToNonRight()
          e.goUpToNonRight() moves e up to the lowest non-right tree edge at or above e.
 void startInorder()
          e.startInorder() moves e to the edge position whose to node is the first node in a inorder traversal.
 void startPostorder()
          e.startPostorder() moves e to the edge position whose to node is the first node in a postorder traversal.
 void startPreorder()
          e.startPreorder() moves e to the edge position whose to node is the first node in a preorder traversal.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LEFT

public static final int LEFT
TreeEdge.LEFT is the type constant for describing an edge from a node to its left child.

ROOT

public static final int ROOT
TreeEdge.ROOT is the type constant for describing the edge from an external node to the root.

RIGHT

public static final int RIGHT
TreeEdge.RIGHT is the type constant for describing an edge from a node to its right child.

tree

protected Tree tree
tree is the binary tree that this edge belongs to.

from

protected TreeNode from
from is the binary tree node from which this edge originates.

edgeType

protected int edgeType
edgeType is the type of this edge.
Constructor Detail

TreeEdge

public TreeEdge(Tree t)
new TreeEdge(t) returns a new tree edge to the root of t.
Method Detail

getTree

public Tree getTree()
e.getTree() returns the tree that e belongs to.

getCopy

public TreeEdge getCopy()
e.getCopy() returns a copy of e.

getEdgeType

public int getEdgeType()
e.getEdgeType() returns the edge type of e. The returned value is one of the constants TreeEdge.ROOT, TreeEdge.LEFT, or TreeEdge.RIGHT.

getFrom

public TreeNode getFrom()
e.getFrom() returns the node that e originates from.

getTo

public TreeNode getTo()
e.getTo() returns the node that e points to.

getSibling

public TreeNode getSibling()
e.getSibling() returns the sibling of the to node of e.

Preconditions:
e.getEdgeType() != TreeEdge.ROOT


getNearNiece

public TreeNode getNearNiece()
e.getNearNiece() returns the near child of the sibling of the to node of e.

Preconditions:
e.getEdgeType() != TreeEdge.ROOT e.getSibling() != null


getFarNiece

public TreeNode getFarNiece()
e.getFarNiece() returns the far child of the sibling of the to node of e.

Preconditions:
e.getEdgeType() != TreeEdge.ROOT e.getSibling() != null


goToRoot

public void goToRoot()
e.goToRoot() moves e to the root edge of its tree.

goUp

public void goUp()
e.goUp() moves e one step towards the root of its tree.

Preconditions: e.getType() != TreeEdge.ROOT


goUpToNonLeft

public void goUpToNonLeft()
e.goUpToNonLeft() moves e up to the lowest non-left tree edge at or above e.

goUpToNonRight

public void goUpToNonRight()
e.goUpToNonRight() moves e up to the lowest non-right tree edge at or above e.

goLeft

public void goLeft()
e.goLeft() moves e down and to the left.

Preconditions:
e.getTo() != null


goLeftToNull

public void goLeftToNull()
e.goLeftToNull() moves e left, stopping at the last edge in the chain of left edges from its current position.

goLeftToLastNonNull

public void goLeftToLastNonNull()
e.goLeftToLastNonNull() moves e left, stopping at the last non-null in the chain of left edges from its current position.

Preconditions:
e.getTo() != null


goRight

public void goRight()
e.goRight() moves e down and to the right.

Preconditions:
e.getTo() != null


goRightToNull

public void goRightToNull()
e.goRightToNull() moves e right, stopping at the last edge in the chain of right edges from its current position.

goRightToLastNonNull

public void goRightToLastNonNull()
e.goRightToLastNonNull() moves e right, stopping at the last non-null in the chain of right edges from its current position.

Preconditions:
e.getTo() != null


startPreorder

public void startPreorder()
e.startPreorder() moves e to the edge position whose to node is the first node in a preorder traversal.

advancePreorder

public void advancePreorder()
e.advancePreorder() moves e to the edge position whose to node is the preorder successor of the current to node.

Preconditions:
e.getTo() != null


startInorder

public void startInorder()
e.startInorder() moves e to the edge position whose to node is the first node in a inorder traversal.

advanceInorder

public void advanceInorder()
e.advanceInorder() moves e to the edge position whose to node is the inorder successor of the current to node.

Preconditions:
e.getTo() != null


startPostorder

public void startPostorder()
e.startPostorder() moves e to the edge position whose to node is the first node in a postorder traversal.

advancePostorder

public void advancePostorder()
e.advancePostorder() moves e to the edge position whose to node is the postorder successor of the current to node.

Preconditions:
e.getTo() != null