The most concise and understandable non-recursive traversal binary tree algorithm in history

Source: Internet
Author: User

The most concise and understandable non-recursive traversal binary tree algorithm in historyQiao If clumsy (welcome reprint, but please specify Source: Http://blog.csdn.net/qiaoruozhuo)

The recursive function of traversing binary tree is a ingenious algorithm which embodies the beauty of the algorithm, with clear thinking, simple code and pleasing reading. The code is as follows:
Program code:
voidPreordertraverse_r (Bitree BT)//Recursive first-order traversal of binary tree BT
{
if(BT = NULL)
{
printf"%c", Bt->data);//outputs the node (root node) .
Preordertraverse_r (Bt->lchild);//traversing the left sub-tree
Preordertraverse_r (Bt->rchild);//traversing the right sub-tree
}
}

voidInordertraverse_r (Bitree BT)//using recursive method to traverse binary tree BT in sequence
{
if(BT = NULL)
{
Inordertraverse_r (Bt->lchild);//traversing the left sub-tree
printf"%c", Bt->data);//outputs the node (root node) .
Inordertraverse_r (Bt->rchild);//traversing the right sub-tree
}
}

voidPostordertraverse_r (Bitree BT)//using recursive method to traverse binary tree BT
{
if(BT = NULL)
{
Postordertraverse_r (Bt->lchild);//traversing the left sub-tree
Postordertraverse_r (Bt->rchild);//traversing the right sub-tree
printf"%c", Bt->data);//outputs the node (root node) .
}
}

In contrast , most of the non-recursive traversal binary tree algorithm language is obscure, ugly, although the use of the stack data structure to simulate the recursive traversal process, but the idea and expression form is not corresponding to the recursive algorithm, resulting in the initial learning of the confusion of the understanding of the people. Assuming that non-recursive algorithms and recursive algorithms can be matched to each other, it can be generous to understand the equivalence between those who are just beginning to learn.
we know that the compiler allocates a stack space when calling a function to store the necessary information, assuming that the function calls a different function, and that its stack space remains until the last statement of the function is completed before releasing its stack space. When simulating a non-recursive algorithm, we need to manually allocate and release the stack space.
three different traversal modes differ in the timing of the release of the stack space and the timing of the output node information: The first order and the middle order traversal are in the right child (right subtree) before visiting the top element of the stack, and then traversing the back stack of the right subtree, and the first sequence traversal is the output of its information when a node is in The middle and post-order traversal outputs its information when the node is retired from the stack.
both recursive and non-recursive algorithms follow the above rules, which can be one by one corresponding. Figure Demo Sample For example the following:


Appendix: Non-recursive algorithm codes such as the following:

void preordertraverse_s (Bitree BT)//using a non-recursive method of first-order traversal of binary tree BT

{

Bitreep, stack[maxsize];//p represents the current node, stack stack[] is used to store nodes.

Inttop =-1; Stack empty

if (BT! = NULL)//First infer whether it is an empty tree

{

p = BT;

while (P | | Top >= 0)

{

if (P! = NULL)//Output node data first, then traverse left child

{

printf ("%c", p->data);//outputs the node.

Stack[++top] = p;

p = p->lchild;

}

Else

{

p = stack[top--]->rchild; Visit stack top element right child and retire stack

}

}

}

}

void inordertraverse_s (Bitree BT)//using a non-recursive method to traverse binary tree BT

{

Bitreep, stack[maxsize];//p represents the current node, stack stack[] is used to store nodes.

Inttop =-1; Stack empty

if (BT! = NULL)//First infer whether it is an empty tree

{

p = BT;

while (P | | Top >= 0)

{

if (P! = NULL)//First interview Zuozi

{

Stack[++top] = p;

p = p->lchild;

}

Else

{

printf ("%c", stack[top]->data);//output stack top element

p = stack[top--]->rchild; Visit stack top element right child and retire stack

}

}

}

}

void postordertraverse_s (Bitree BT)//Adopt a non-recursive way to traverse binary tree BT

{

Bitreep, stack[maxsize];//p represents the current node, stack stack[] is used to store nodes.

Inttag[maxsize] = {0}; The right child to mark the top of the stack has been interviewed, 0 indicates no access, 1 indicates an interview

Inttop =-1; Stack empty

if (BT! = NULL)//First infer whether it is an empty tree

{

p = BT;

while (P | | Top >= 0)

{

if (P! = NULL)//First interview Zuozi

{

Stack[++top] = p;

Tag[top] = 0;

p = p->lchild;

}

else if (tag[top] = = 0)//If the right subtree has not yet been visited, visit the top element of the stack right child and mark it well

{

p= stack[top]->rchild;

Tag[top]= 1;

}

else//otherwise output stack top element and back stack

{

printf ("%c", stack[top--]->data);

}

}

}

}






The most concise and understandable non-recursive traversal binary tree algorithm in history

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.