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 history

Qiao 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:

void Preordertraverse_r (Bitree bt)//recursive traversal of binary tree bt{if (BT! = NULL) {printf ("%c", bt->data)//Output this node (root node) Preordertraverse_r (bt->lchild);  Traverse left subtree Preordertraverse_r (bt->rchild);//Traverse right subtree}}void inordertraverse_r (bitree BT)//recursively traverse binary tree bt{if (BT! = NULL ) {Inordertraverse_r (bt->lchild);  Traverse the left subtree printf ("%c", bt->data);//Output the node (root node) inordertraverse_r (bt->rchild);//Traverse right subtree}}void postordertraverse_r ( Bitree BT)//recursive traversal of binary tree bt{if (BT! = NULL) {postordertraverse_r (bt->lchild) by recursion;  Traverse left subtree Postordertraverse_r (bt->rchild);//Traverse Right sub-tree printf ("%c", bt->data);//Output 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 recursive traversal process, but the idea and expression form is not corresponding to the recursive algorithm, resulting in the confusion of beginners understanding. If the non-recursive algorithm and recursive algorithm can correspond to each other, it can be greatly convenient for beginners to understand the equivalence between them.

We know that when the compiler calls a function, it allocates a stack space to store the necessary information, and if the function calls another function, its stack space is retained until the last statement of the function is completed before its stack space is freed. 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 accessing the top element of the stack, and then traversing the back stack in access to the right subtree, and the first order traversal is the output of its information at a node 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-mentioned rules, and both can correspond. This is illustrated below:




Appendix: The Non-recursive algorithm code is as follows:

void preordertraverse_s (Bitree BT)//recursively traverse binary tree BT using non-recursive method

{

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

Inttop =-1; Stack empty

if (BT! = NULL)//First determine if the tree is empty

{

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; Access stack top element right child and back stack

}

}

}

}

void inordertraverse_s (Bitree BT)//Traversal of binary tree BT using a non-recursive method

{

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

Inttop =-1; Stack empty

if (BT! = NULL)//First determine if the tree is empty

{

p = BT;

while (P | | Top >= 0)

{

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

{

Stack[++top] = p;

p = p->lchild;

}

Else

{

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

p = stack[top--]->rchild; Access stack top element right child and back stack

}

}

}

}

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

{

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

Inttag[maxsize] = {0}; The right child used to flag the top of the stack is visited, 0 means no access, 1 indicates access

Inttop =-1; Stack empty

if (BT! = NULL)//First determine if the tree is empty

{

p = BT;

while (P | | Top >= 0)

{

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

{

Stack[++top] = p;

Tag[top] = 0;

p = p->lchild;

}

else if (tag[top] = = 0)//If the right subtree has not been accessed, access the top element of the stack right child, and do the tagging

{

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.