void
tree_depth_traversal1 (struct t_node *root,
void (*process)(struct t_node *node, int first_flag))
{
// TRUE 1st exploration of node, continue with 1st subnode
// FALSE node completely explored, continue with sibling
int dir_flag = TRUE;
// the current node during exploration
struct t_node *current = root;
//----- no tree or no user function , nothing to do
if ((root == NULL) || (process == NULL)) return;
//----- the root pre order
(* process) (root, TRUE);
if (has_sub(root))
{
//----- the exploration loop
current = get_sub(root);
while (node_exist(current)
&& ((current != root) || (dir_flag == TRUE)))
{
//--- process the node : TRUE prefix, FALSE postfix
(* process) (current, dir_flag);
//--- 1st expl. of the node, continue with 1st subnode
// or stay for 2nd part of the process
if (dir_flag == TRUE)
{
if (has_sub(current))
current = get_sub(current);
else
// 1st exploration of current node is finished
// go to 2nd exploration of the current node
dir_flag = FALSE;
}
//--- second expl. of the node, continue with sibling
// or the parent if no sibling
else
{
if (has_sibling(current))
{
// goto 1st exploration of the sibling
// (now current)
current = get_sibling(current);
dir_flag = TRUE;
}
else
current = get_parent(current);
}
}
// ASSERT ((current == root) && (dir_flag == FALSE));
}
//----- the root post order
(* process) (root, FALSE);
}