After a lot of googling around, I discovered that a lot of sites discuss pre-order, in-order and post-order algorithms focused on binary trees. Most of the code are familiar textbook materials. Only a few take up more complicated tree structures like DOM documents.
Both recursive and iterative approaches to tree traversal have their pros and cons. And I'm not here to overemphasize any of these. Instead we'll take a different approach that's as fast, if not better than any other. The algorithm presented here is written in PHP and allows you to combine both pre-order and post-order traversal sequence in a single pass/loop.
We'll be needing a stack that will contain all the nodes we passed along the way as we go through each branch. The traditional method of iterative traversal using a "visited" flag attached to each node has a 1:1 correspondence to the total number of nodes. On the other hand, the use of a stack is proportional to the height (or depth) of the tallest branch.
To determine how we handle each node in the tree, we need some indicator/flag to tell us whether we're traversing downward/right or ascending from the end of a branch (a leaf).
Without much ado:-
The traversal strategy is no different from other algorithms, except the way we use our stack and the $_flag indicator (which is really a state indicator and vector).
Where indicated in the code snippet, you can insert both pre-order and post-order processing routines in the loop. This is more efficient than having to traverse a tree twice to do both. But first you need a situation where you'll be needing both. Now we can talk about that at a later time.
You might also want to take a look at the stackless approach here.
Both recursive and iterative approaches to tree traversal have their pros and cons. And I'm not here to overemphasize any of these. Instead we'll take a different approach that's as fast, if not better than any other. The algorithm presented here is written in PHP and allows you to combine both pre-order and post-order traversal sequence in a single pass/loop.
We'll be needing a stack that will contain all the nodes we passed along the way as we go through each branch. The traditional method of iterative traversal using a "visited" flag attached to each node has a 1:1 correspondence to the total number of nodes. On the other hand, the use of a stack is proportional to the height (or depth) of the tallest branch.
To determine how we handle each node in the tree, we need some indicator/flag to tell us whether we're traversing downward/right or ascending from the end of a branch (a leaf).
Without much ado:-
public function traverse($tree) {
$_stack=array();
$_flag=FALSE;
$_node=$tree->documentElement;
while ($_node) {
if (!$_flag) {
// insert pre-order code here
if ($_node->firstChild) {
array_push($_stack,$_node);
$_flag=FALSE;
$_node=$_node->firstChild;
continue;
}
}
// insert post-order code here
$_flag=FALSE;
$_node=$_node->nextSibling;
if (!$_node && !empty($_stack)) {
$_node=array_pop($_stack);
$_flag=TRUE;
}
}
}
The traversal strategy is no different from other algorithms, except the way we use our stack and the $_flag indicator (which is really a state indicator and vector).
- Initialize stack. Start with a descending vector because we're not traversing the tree at this point from bottom-up.
- If not ascending from a child node, save each node in the stack as we pass along the way to the deepest part of the left-most branch.
- Once we reach a leaf, we go to the leaf's sibling and repeat step 2.
- If there are no more siblings, we pop the stack (effectively going up to the parent node) and repeat step 2.
Where indicated in the code snippet, you can insert both pre-order and post-order processing routines in the loop. This is more efficient than having to traverse a tree twice to do both. But first you need a situation where you'll be needing both. Now we can talk about that at a later time.
You might also want to take a look at the stackless approach here.
Comments
Post a Comment