com.accountingenhancements.treearray
Class TreeCrawler<E>

java.lang.Object
  extended by com.accountingenhancements.treearray.TreeCrawler<E>

public class TreeCrawler<E>
extends java.lang.Object

Crawl through all leaves of all branches in tree.
Leaves of the current branch are iterated through first before the branches. The nextLeaf() method returns all leaves within the current branch before going into the next branch. Once that branch is fully processed, the next branch is processed until all branches are completed.
When getNextLeaf() returns null the TreeCrawler will automatically jump to the next branch.
Use getDepth() to gauge how deep you are.
Use jumpedDeeper() to determine whether you jumped deeper when jumping from last time (Useful in combo with justJumpedBranches() layout a similar structure using simple logic).
Use justJumpedBranches() to determine whether a getNextLeaf()==null is really a null value or is because we ran out of leaves and jumped to a new branch (Next access to getNextLeaf() after a jump will set this back to false).
Use endOfTree() to determine whether the whole tree has been traversed.


Field Summary
protected  BranchNode<E> branchNode
           
protected  java.util.ArrayList<BranchNode<E>> branchPath
           
protected  int depth
           
protected  boolean endOfTree
           
protected  boolean jumpedDeeper
           
protected  boolean justJumpedBranches
           
protected  int leafIndex
           
protected  java.util.ArrayList<java.lang.Integer> path
           
protected  int prevDepth
           
protected  BranchNode<E> trunkBranchNode
           
 
Constructor Summary
TreeCrawler(BranchNode<E> branchNode)
          Creates a new instance of TreeCrawler
 
Method Summary
 boolean endOfTree()
          Determine whether the entire tree has been traversed.
 int getDepth()
          Returns the depth of the curent BranchNode.
The starting branch is 0.
 E getNextLeaf()
          Get the next leaf from the current branch.
If a null is returned, then we may have run out of leaves on the branch and have now jumped to a new branch.
Use justJumpedBranches to determine whether the current leaf really was null or whether we just jumped to a new branch.
 java.util.ArrayList<java.lang.Integer> getPath()
           
 int getPrevDepth()
          Used in conjunction with getDepth() to determine how far the last jump between branches was.
If it is the same as the current depth, then the last jump went back 1 BranchNode then forward again on the next branch.
This value only gets updated by nextBranchNode(), not prevBranchNode since nextBranchNode() may call prevBranchNode() which would, otherwise, make the value meaningless.
 boolean jumpedDeeper()
          Used to determine whether class is contining down the current set of branches or whether it backtracked.
Note: level could stay the same if backing up to the previous branch and then moving forward again on the next branch down.
You can use level to determine just how far you moved.
 void jumpToBeforeFirstLeaf()
          Reset the current BranchNode index so that the first leaf is returned from getNextLeaf().
 void jumptoLastLeaf()
          Jump to last leaf so that getNextLeaf() returns null and causes the class to jump to the next branch.
 boolean justJumpedBranches()
           
 boolean nextBranchNode()
          Jump to the next available branch.
protected  boolean nextBranchNodeInternal()
          Mose of nextBranchNode() is here so that prevDepth() doesn't get overridden by prevBranchNode() when it moves backward, then forward again.
 boolean prevBranchNode()
          Stop going down the current branch path and continue as if this were the end of a branch.
Ususally called because we were at the end of one of a branch.
 void reset()
          Reset this class to the beginning of the trunk so that we can start over (as if the class were just created).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

depth

protected transient int depth

prevDepth

protected transient int prevDepth

path

protected transient java.util.ArrayList<java.lang.Integer> path

branchPath

protected transient java.util.ArrayList<BranchNode<E>> branchPath

jumpedDeeper

protected transient boolean jumpedDeeper

justJumpedBranches

protected transient boolean justJumpedBranches

leafIndex

protected transient int leafIndex

branchNode

protected transient BranchNode<E> branchNode

endOfTree

protected transient boolean endOfTree

trunkBranchNode

protected BranchNode<E> trunkBranchNode
Constructor Detail

TreeCrawler

public TreeCrawler(BranchNode<E> branchNode)
Creates a new instance of TreeCrawler

Parameters:
branchNode - is the BranchNode to be processed. Usually is tree.getTrunk()
Method Detail

getDepth

public int getDepth()
Returns the depth of the curent BranchNode.
The starting branch is 0. This is not the branch path. If you want to know the path such that you could get there using a an expression such as tree.getTrunck().get(1).get(0).get(2). Then use getPath();

Returns:
the current depth.

getPrevDepth

public int getPrevDepth()
Used in conjunction with getDepth() to determine how far the last jump between branches was.
If it is the same as the current depth, then the last jump went back 1 BranchNode then forward again on the next branch.
This value only gets updated by nextBranchNode(), not prevBranchNode since nextBranchNode() may call prevBranchNode() which would, otherwise, make the value meaningless.

Returns:
value of previous depth before nextBranchNode() was last executed.

getPath

public java.util.ArrayList<java.lang.Integer> getPath()
Returns:
the path to the current branchNode [0]=index of branch off of original brachNode. -1 means that no branches have been processed off of the BranchNode
This will only be true for the last entry in the array and only if there are more leafs yet to be read.

reset

public void reset()
Reset this class to the beginning of the trunk so that we can start over (as if the class were just created).


jumpToBeforeFirstLeaf

public void jumpToBeforeFirstLeaf()
Reset the current BranchNode index so that the first leaf is returned from getNextLeaf().


jumptoLastLeaf

public void jumptoLastLeaf()
Jump to last leaf so that getNextLeaf() returns null and causes the class to jump to the next branch.


nextBranchNode

public boolean nextBranchNode()
Jump to the next available branch. This may be forward or backward.

Returns:
false if no more branches, meaning that we are done with the entire tree.

nextBranchNodeInternal

protected boolean nextBranchNodeInternal()
Mose of nextBranchNode() is here so that prevDepth() doesn't get overridden by prevBranchNode() when it moves backward, then forward again.

Returns:
false if no more branches, meaning that we are done with the entire tree.

prevBranchNode

public boolean prevBranchNode()
Stop going down the current branch path and continue as if this were the end of a branch.
Ususally called because we were at the end of one of a branch.

Returns:
false if no more branches, meaning that we are done with the entire tree.

getNextLeaf

public E getNextLeaf()
Get the next leaf from the current branch.
If a null is returned, then we may have run out of leaves on the branch and have now jumped to a new branch.
Use justJumpedBranches to determine whether the current leaf really was null or whether we just jumped to a new branch. Also use endOfTree() to determine whether you have traversed the entire tree.

Returns:
the Object that was stored in the next leaf.

justJumpedBranches

public boolean justJumpedBranches()
Returns:
true if the getNextLeaf() method caused this class to jump to a new branch.
If so, then the next call to getNextLeaf() will return the first leaf of the new branch.
This should always be used in conjunction with endOfTree(). This should return false if at end of tree unless you arrived there by using nextbranchNode() or prevBranchNode instead of getNextLeaf() (which is responsible for setting justJumpedBranches=false).

jumpedDeeper

public boolean jumpedDeeper()
Used to determine whether class is contining down the current set of branches or whether it backtracked.
Note: level could stay the same if backing up to the previous branch and then moving forward again on the next branch down.
You can use level to determine just how far you moved. If same as previous, then you went one branch back then forward again.

Returns:
if continued deeper into the current branch chain, false if had to backtrack at least 1 BranchNode.

endOfTree

public boolean endOfTree()
Determine whether the entire tree has been traversed. Usually used oin conjunction with justJumedBranches()

Returns:
true if the entire tree has been traversed.