We know that binary tree is a recursive definition of the data structure, including two-fork tree creation, traversal, tree height, leaf node number and so on. The use of recursion to do the above is very simple, related implementation please refer to the "C language to achieve the basic operation of two fork tree." But today we are slant, using the method of non recursion to realize the first order, middle sequence and subsequent traversal of the tree. Implementation code uploaded to Https://github.com/chenyufeng1991/TraverseBinaryTreeNoRecursion.
(1) Non-recursive implementation of first-order traversal
In non recursive three traversal, the first sequence traversal is the simplest. The idea is as follows: first the root node into the stack, and when the root node is out of the stack to access the root node, at the same time to determine whether the right subtree of the root node is empty, if not empty, then the right child into the stack to determine whether the left child of the root node is empty, if not empty, the left child into the stack. Repeat the above operation when the stack is not empty.
The core code is as follows:
void Preordernorecursion (node *node, Stack<node *> &sta)
{while
(!sta.empty ())
{
node * temp = Sta.top ();
cout << temp->data << "";
Sta.pop ();
if (temp->rchild!= NULL)
{
sta.push (temp->rchild);
}
if (temp->lchild!= NULL)
{
sta.push (temp->lchild);
}
}
return;
}
(2) Non-recursive implementation of the sequence traversal
The idea is as follows: first the root node into the stack, to determine whether the root node of the left child is empty, if not empty, then left the child into the stack, and constantly move the node pointer to the left child, until the left child node is empty.
Then pop the top element of the stack and access the element to determine whether the right child of the pop-up element is empty, if not empty, then put the right child into the stack and point the node pointer to the right child. Repeat the above operation.
The core code is as follows:
void Midordernorecursion (Node *node, Stack<node *>&sta)
{while
(!sta.empty ())
{
while ( Node->lchild!= NULL)
{
sta.push (node->lchild);
node = node->lchild;
}
Node *temp = Sta.top ();
cout << temp->data << "";
Sta.pop ();
if (temp->rchild!= NULL)
{
sta.push (temp->rchild);
node = temp->rchild;
}}}
(3) Non-recursive implementation of the subsequent traversal
Subsequent traversal is the most troublesome of these three kinds of traversal, it is necessary to add a parameter isvisited in the structure of the node to indicate whether a node has been visited. The idea is as follows: first the root node into the stack, and then determine whether the left child is empty, if not empty, then left the child into the stack, and moved node to the left child, until the left child is empty. Then judge the top element of the stack, if the top element of the stack has no right child or right child has been visited, then access the top element of the stack, and out of the stack; if the top element has a right child and the right child has not been visited, then put the right child into the stack and point the node pointer to the right child.
The core code is as follows:
void Postordernorecursion (Node *node, Stack<node *>&sta)
{while
(!sta.empty ())
{
while ( Node->lchild!= NULL)
{
sta.push (node->lchild);
node = node->lchild;
}
Node *temp = Sta.top ();
if (Temp->rchild = NULL | | temp->rchild->isvisited = = true)
{
cout << temp->data << "" ;
Temp->isvisited = true;
Sta.pop ();
}
else if (temp->rchild!= NULL && temp->isvisited = False)
{
sta.push (temp->rchild);
node = temp->rchild;
}}}
The test code is as follows:
int main (int argc, const char * argv[])
{
cout << "Please enter the node of the binary tree, enter-1 end:";
Node *node;
Createbinarytree (&node);
The result of cout << "non-recursive first-order traversal is:";
Stack<node *> Nodestack;
First, the root node is placed into the stack
nodestack.push (node);
Preordernorecursion (node, nodestack);
cout << Endl;
The result of cout << "non-recursive sequence traversal is:";
Stack<node *> nodestackm;
First, the root node is placed into the stack
nodestackm.push (node);
Midordernorecursion (node, nodestackm);
cout << Endl;
cout << "Non-recursive subsequent traversal of the result is:";
Stack<node *> Nodestackp;
First, the root node is placed into the stack
nodestackp.push (node);
Postordernorecursion (node, NODESTACKP);
cout << Endl;
return 0;
}