Binary Tree traversal non-recursive algorithm--sequential traversal

Source: Internet
Author: User

The non-recursive algorithm and the non-recursive algorithm of the sequential traversal of the two-fork tree are introduced in the preceding sequence, and the non-recursive algorithm of the binary tree post-order traversal is introduced, and the non-recursive traversal of the binary tree is really very

Important, because it has unique characteristics (the end of the article will be elaborated), so, in many of the two-fork tree related to the complex algorithm, often use the two-fork tree post-order traversal of the non-recursive algorithm. And in the Internet interview pen

The algorithm is often examined, so we should do a two-fork-tree post-traversal non-recursive algorithm to be confused with the heart.

Like the two-fork-tree sequence traversal, the middle sequence traversal non-recursive algorithm, the post-order traversal non-recursive algorithm also uses the stack to realize: Starting from the root node, all the leftmost nodes are all stacked, whenever a node out of the stack,

Scan the right subtree of the node first, and only if a node's left child and right child node have been visited, can the node itself be accessed.

The binary tree post-traversal non-recursive algorithm is implemented as follows:

  

#include <stdlib.h>#include<stdio.h>#defineMAXSIZE 100//Defining node Typestypedefstructnode{intdata; structnode*Lchild; structnode*Rchild;} Btnode;voidPostorder (btnode*t) {Btnode*Seqstack[maxsize]; inttop =-1; intFalg =1; Btnode*p; if(t! =NULL) {         Do        {             while(t! = NULL)//loop, which pushes all the leftmost nodes to the stack.{Top++; Seqstack[top]=T; T= t->Lchild; } Flag=1;//The auxiliary variable, flag 1, indicates that the left child of the current node is empty or has been accessed.p = NULL;//pointer variable p points to the predecessor node of the current node.             while(Top >-1&& Falg = =1) {T= Seqstack[top];//Note: Here just gets the top element of the stack, and does not have a stack                if(T->rchild = = p)//if the right child of the current node is empty or has been visited, the current node is accessed .{Top--;//current node out of stackprintf"%d", p->data); P= t;//pointer variable points to the current node                }                Else                            //if the current node right child is not empty, go to the right child first .{T= t->rchild;//Handle Right childFlag =0;//*t's left child is not visited, flag is 0                }            }        } while(Top >-1)    }    }

The code above implements a two-fork-tree post-traversal non-recursive algorithm (with a focus on annotations), and then it says one of the features of the algorithm mentioned earlier: that is, when accessing a node, the elements stored in the stack

That 's exactly what this junction is. have ancestors . Then knowing this feature, we can easily solve the following problems:

(1). When given a leaf node, it is required to output all ancestors of the leaf node

(2). The path of the output root node to all leaf nodes

(3). If the value of the binary tree node is numeric, then the sum of the values on each path can be obtained by using the non-recursive algorithm of the second-order traversal of the binary tree.

Binary Tree traversal non-recursive algorithm--sequential traversal

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.