org.foray.common
Class AbstractOrderedTreeNode

java.lang.Object
  extended by org.foray.common.AbstractOrderedTreeNode
All Implemented Interfaces:
TreeNode, OrderedTreeNode
Direct Known Subclasses:
AreaNode, FObj

public abstract class AbstractOrderedTreeNode
extends Object
implements OrderedTreeNode

An implementation of the TreeNode interface that provides methods for various traversal needs.

Consideration was given to using DefaultMutableTreeNode instead of creating this class. However, the data portion of that class was not deemed suitable.


Nested Class Summary
 class AbstractOrderedTreeNode.PostOrderDescendantIterator
          A post-order iterator over the descendants of a given node.
 
Constructor Summary
AbstractOrderedTreeNode()
           
 
Method Summary
 Enumeration<? extends OrderedTreeNode> children()
           
abstract  boolean getAllowsChildren()
           
 int getChildCount()
           
abstract  List<? extends OrderedTreeNode> getChildren()
          Return the List of this node's children.
 OrderedTreeNode getFirstChild()
          Returns the first child of this node.
 OrderedTreeNode getFirstLeaf()
          Finds and returns the first leaf that is a descendant of this node -- either this node or its first child's first leaf.
 int getIndex(TreeNode node)
           
 OrderedTreeNode getLastChild()
          Returns the last child of this node.
 OrderedTreeNode getLastLeaf()
          Finds and returns the last leaf that is a descendant of this node -- either this node or its last child's last leaf.
 int getLevel()
          Returns the number of levels above this node -- the distance from the root to this node.
 OrderedTreeNode getNextLeaf()
          Returns the leaf after this node or null if this node is the last leaf in the tree.
 OrderedTreeNode getNextSibling()
          Returns the next sibling node.
 OrderedTreeNode getPreviousLeaf()
          Returns the leaf before this node or null if this node is the first leaf in the tree.
 OrderedTreeNode getPreviousSibling()
          Returns the previous sibling node.
 OrderedTreeNode getSharedAncestor(OrderedTreeNode aNode)
          Returns the nearest common ancestor to this node and aNode.
abstract  List<? extends OrderedTreeNode> getSiblings()
          Returns the List of this node's parent's children, which includes this node.
 boolean hasChildren()
          Reports whether this node has any children.
 boolean isLeaf()
           
 boolean isNodeAncestor(OrderedTreeNode anotherNode)
          Returns true if anotherNode is an ancestor of this node -- if it is this node, this node's parent, or an ancestor of this node's parent.
 boolean isNodeDescendant(OrderedTreeNode anotherNode)
          Returns true if anotherNode is a descendant of this node -- if it is this node, one of this node's children, or a descendant of one of this node's children.
 OrderedTreeNode nextPostOrderNode()
          Returns the next node in the tree relative to the current node, in post-order traversal order.
 OrderedTreeNode nextPreOrderNode()
          Returns the next node in the tree relative to the current node, in pre-order traversal order.
 Iterator<OrderedTreeNode> postOrderDescendantIterator()
          Returns an iterator that will iterate the descendant nodes of this node in post-traversal (depth-first) order.
 int siblingIndex()
          Returns this node's position within the siblings.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.foray.common.OrderedTreeNode
getOrderedParent
 
Methods inherited from interface javax.swing.tree.TreeNode
getChildAt, getParent
 

Constructor Detail

AbstractOrderedTreeNode

public AbstractOrderedTreeNode()
Method Detail

children

public Enumeration<? extends OrderedTreeNode> children()
Specified by:
children in interface TreeNode

postOrderDescendantIterator

public Iterator<OrderedTreeNode> postOrderDescendantIterator()
Returns an iterator that will iterate the descendant nodes of this node in post-traversal (depth-first) order.

Returns:
A post-traversal iterator of this node's descendants.

getChildren

public abstract List<? extends OrderedTreeNode> getChildren()
Return the List of this node's children.

Returns:
The List of this node's children.

getAllowsChildren

public abstract boolean getAllowsChildren()
Specified by:
getAllowsChildren in interface TreeNode

getChildCount

public int getChildCount()
Specified by:
getChildCount in interface TreeNode

getIndex

public int getIndex(TreeNode node)
Specified by:
getIndex in interface TreeNode

isLeaf

public boolean isLeaf()
Specified by:
isLeaf in interface TreeNode

getSiblings

public abstract List<? extends OrderedTreeNode> getSiblings()
Returns the List of this node's parent's children, which includes this node.

Returns:
The List of this node's parent's children.

siblingIndex

public int siblingIndex()
Description copied from interface: OrderedTreeNode
Returns this node's position within the siblings.

Specified by:
siblingIndex in interface OrderedTreeNode
Returns:
The index to "this" in the parent's children.

getPreviousSibling

public OrderedTreeNode getPreviousSibling()
Description copied from interface: OrderedTreeNode
Returns the previous sibling node.

Specified by:
getPreviousSibling in interface OrderedTreeNode
Returns:
Return the sibling immediately preceding "this" in the parent's children, or null if this is the first child.

getNextSibling

public OrderedTreeNode getNextSibling()
Description copied from interface: OrderedTreeNode
Returns the next sibling node.

Specified by:
getNextSibling in interface OrderedTreeNode
Returns:
Return the sibling immediately following "this" in the parent's children, or null if this is the last child.

hasChildren

public boolean hasChildren()
Description copied from interface: OrderedTreeNode
Reports whether this node has any children.

Specified by:
hasChildren in interface OrderedTreeNode
Returns:
True if this node has any children, false if it has none.

getFirstChild

public OrderedTreeNode getFirstChild()
Description copied from interface: OrderedTreeNode
Returns the first child of this node.

Specified by:
getFirstChild in interface OrderedTreeNode
Returns:
The first child of this node, or null if this node has no children.

getLastChild

public OrderedTreeNode getLastChild()
Description copied from interface: OrderedTreeNode
Returns the last child of this node.

Specified by:
getLastChild in interface OrderedTreeNode
Returns:
The last child of this node, or null if this node has no children.

nextPreOrderNode

public OrderedTreeNode nextPreOrderNode()
Description copied from interface: OrderedTreeNode
Returns the next node in the tree relative to the current node, in pre-order traversal order. This is also known as breadth-first order.

Specified by:
nextPreOrderNode in interface OrderedTreeNode
Returns:
The next pre-order node, or null if there is none.

nextPostOrderNode

public OrderedTreeNode nextPostOrderNode()
Returns the next node in the tree relative to the current node, in post-order traversal order. This is also known as depth-first order.

Specified by:
nextPostOrderNode in interface OrderedTreeNode
Returns:
The next post-order node, or null if there is none.

getFirstLeaf

public OrderedTreeNode getFirstLeaf()
Finds and returns the first leaf that is a descendant of this node -- either this node or its first child's first leaf. Returns this node if it is a leaf. (Liberated from DefaultMutableTreeNode). This implementation was liberated from DefaultMutableTreeNode).

Specified by:
getFirstLeaf in interface OrderedTreeNode
Returns:
the first leaf in the subtree rooted at this node
See Also:
TreeNode.isLeaf(), DefaultMutableTreeNode.isNodeDescendant(javax.swing.tree.DefaultMutableTreeNode)

getLastLeaf

public OrderedTreeNode getLastLeaf()
Finds and returns the last leaf that is a descendant of this node -- either this node or its last child's last leaf. Returns this node if it is a leaf. (Liberated from DefaultMutableTreeNode). This implementation was liberated from DefaultMutableTreeNode).

Specified by:
getLastLeaf in interface OrderedTreeNode
Returns:
the last leaf in the subtree rooted at this node
See Also:
TreeNode.isLeaf(), DefaultMutableTreeNode.isNodeDescendant(javax.swing.tree.DefaultMutableTreeNode)

getNextLeaf

public OrderedTreeNode getNextLeaf()
Returns the leaf after this node or null if this node is the last leaf in the tree.

In this implementation of the MutableNode interface, this operation is very inefficient. In order to determine the next node, this method first performs a linear search in the parent's child-list in order to find the current node.

That implementation makes the operation suitable for short traversals from a known position. But to traverse all of the leaves in the tree, you should use depthFirstEnumeration to enumerate the nodes in the tree and use isLeaf on each node to determine which are leaves. (Liberated from DefaultMutableTreeNode). This implementation was liberated from DefaultMutableTreeNode).

Specified by:
getNextLeaf in interface OrderedTreeNode
Returns:
returns the next leaf past this node
See Also:
DefaultMutableTreeNode.depthFirstEnumeration(), TreeNode.isLeaf()

getPreviousLeaf

public OrderedTreeNode getPreviousLeaf()
Returns the leaf before this node or null if this node is the first leaf in the tree.

In this implementation of the MutableNode interface, this operation is very inefficient. In order to determine the previous node, this method first performs a linear search in the parent's child-list in order to find the current node.

That implementation makes the operation suitable for short traversals from a known position. But to traverse all of the leaves in the tree, you should use depthFirstEnumeration to enumerate the nodes in the tree and use isLeaf on each node to determine which are leaves. (Liberated from DefaultMutableTreeNode). This implementation was liberated from DefaultMutableTreeNode).

Specified by:
getPreviousLeaf in interface OrderedTreeNode
Returns:
returns the leaf before this node
See Also:
DefaultMutableTreeNode.depthFirstEnumeration(), TreeNode.isLeaf()

isNodeAncestor

public boolean isNodeAncestor(OrderedTreeNode anotherNode)
Description copied from interface: OrderedTreeNode
Returns true if anotherNode is an ancestor of this node -- if it is this node, this node's parent, or an ancestor of this node's parent. (Note that a node is considered an ancestor of itself.) If anotherNode is null, this method returns false. This operation is at worst O(h) where h is the distance from the root to this node.

Specified by:
isNodeAncestor in interface OrderedTreeNode
Parameters:
anotherNode - node to test as an ancestor of this node
Returns:
true if this node is a descendant of anotherNode

isNodeDescendant

public boolean isNodeDescendant(OrderedTreeNode anotherNode)
Returns true if anotherNode is a descendant of this node -- if it is this node, one of this node's children, or a descendant of one of this node's children. Note that a node is considered a descendant of itself. If anotherNode is null, returns false. This operation is at worst O(h) where h is the distance from the root to anotherNode. This implementation was liberated from DefaultMutableTreeNode).

Parameters:
anotherNode - Node to test as descendant of this node.
Returns:
True if this node is an ancestor of anotherNode
See Also:
isNodeAncestor(org.foray.common.OrderedTreeNode), getSharedAncestor(org.foray.common.OrderedTreeNode)

getSharedAncestor

public OrderedTreeNode getSharedAncestor(OrderedTreeNode aNode)
Returns the nearest common ancestor to this node and aNode. Returns null, if no such ancestor exists -- if this node and aNode are in different trees or if aNode is null. A node is considered an ancestor of itself. This implementation was liberated from DefaultMutableTreeNode).

Parameters:
aNode - node to find common ancestor with
Returns:
nearest ancestor common to this node and aNode, or null if none
See Also:
isNodeAncestor(org.foray.common.OrderedTreeNode), isNodeDescendant(org.foray.common.OrderedTreeNode)

getLevel

public int getLevel()
Description copied from interface: OrderedTreeNode
Returns the number of levels above this node -- the distance from the root to this node. If this node is the root, returns 0. This implementation was liberated from DefaultMutableTreeNode).

Specified by:
getLevel in interface OrderedTreeNode
Returns:
The number of levels above this node.


Copyright © 2017. All rights reserved.