MyArch.com

Java Frameworks

   

API   Contents

Limiting the traversal depth

Tree traversal algorithms usually go over all nodes of a tree assuming that full traversal is required by the calling code. However, oftentimes calling code needs to process only the subset of the tree's levels meaning that it doesn't need to go all the way to the leaf level of a tree. Needless to say that in this case full traversal carries significant overhead since it traverses the nodes that are simply ignored by the calling code.

setMaxDepth() method or maxDepth parameter of the tree iterator constructor set the maximum level (path length from the root of a tree) that should be traversed by the iterator. This parameter is checked in the next() method of the iterator before moving to the children of the current node. Effectively, all "low growing" nodes of a tree (on levels below specified) won't be traversed. Note that root is considered level 0. Example:


public static final int idLevel = 2;
//...
// xdoc -- tree root
TIterator tIterator = new TIterator ( xdoc , new DOMAdapter(), idLevel );
for( Node node = xdoc; node!=null; node=(Node)tIterator.next() ){
  // perform processing...
}

   

API   Contents