Non-recursive C language for first-order, middle-order, and post-order traversal of Binary Trees

Source: Internet
Author: User

Non-recursive C language for first-order, middle-order, and post-order traversal of Binary Trees

# Include
 
  
# Include
  
   
# Define INIT_STACK_SIZE 100 # define STACKINCREMENT 10 // ***** Binary Tree binary linked list storage structure ***** // typedef struct BiNode {char data; struct BiNode * lchild, * rchild; int visitcount; // number of accesses (used in post-order traversal)} BiNode, * BiTree; typedef struct {BiNode ** base; BiNode ** top; int stacksize ;} sqStack; // ****** initialize the stack ***** // void InitStack (SqStack & S) {if (! (S. base = (BiNode **) malloc (sizeof (BiTree) * INIT_STACK_SIZE) {return;} S. top = S. base; S. stacksize = INIT_STACK_SIZE; return;} // ***** element into Stack ***** // void Push (SqStack & S, BiNode * e) {if (S. top-S. base> = S. stacksize) {if (! (S. base = (BiNode **) realloc (S. base, sizeof (BiTree) * (STACKINCREMENT + S. stacksize) {return;} S. stacksize + = STACKINCREMENT;} * S. top ++ = e; return;} // ***** element output stack ***** // void Pop (SqStack & S, BiNode *** e) {if (S. base = S. top) {return;} * e = * -- S. top; return;} // ***** obtain the top element of the stack ***** // int GetTop (SqStack S, BiNode *** e) {if (S. base = S. top) {return 0;} * e = * (S. top-1); return 1;} // ***** determines whether the stack is No blank ***** // int StackEmpty (SqStack S) {if (S. top = S. base) return 1; else return 0;} // ***** enter the node value (one character) in the binary tree in the first order ), the space character indicates that the empty tree constructs the binary tree T ***** represented by the binary chain table. // void CreateBiTree (BiTree & T) {char ch; scanf ("% c ", & ch); if (ch = '') {T = NULL;} else {if (! (T = (BiNode *) malloc (sizeof (BiNode) {return;} T-> data = ch; // generate the root node CreateBiTree (T-> lchild ); // construct the left subtree CreateBiTree (T-> rchild); // construct the right subtree} return ;} // ***** first traverse the binary tree method 1 ***** // void PreorderTraverse_1 (BiTree T) {bitp REE; SqStack S; InitStack (S ); push (S, T); // The root pointer goes into the stack while (! StackEmpty (S) {while (GetTop (S, & p) {printf ("% c", p-> data); Push (S, p-> lchild);} Pop (S, & p); // empty pointer stack rollback if (! StackEmpty (S) {Pop (S, & p); // click the root node to Push the stack (S, p-> rchild); // right child to stack} return ;} // ****** method 2 ****** // void PreorderTraverse_2 (BiTree T) {bitp REE = T; SqStack S; InitStack (S ); while (p |! StackEmpty (S) {if (p) {printf ("% c", p-> data); Push (S, p); p = p-> lchild ;} else {Pop (S, & p); p = p-> rchild ;}}} // ****** method 1 ***** of the central traversal Binary Tree // void InOrderTraverse_1 (BiTree T) {SqStack S; BiTree p; InitStack (S ); push (S, T); while (! StackEmpty (S) {while (GetTop (S, & p) {Push (S, p-> lchild ); // left to the end} Pop (S, & p); // empty pointer back stack if (! StackEmpty (S) {Pop (S, & p); printf ("% c", p-> data); Push (S, p-> rchild );}} return;} // ***** method 2 ***** of the central traversal Binary Tree // void InOrderTraverse_2 (BiTree T) {BiTree p = T; SqStack S; initStack (S); while (p |! StackEmpty (S) {if (p) {Push (S, p); p = p-> lchild;} else {Pop (S, & p ); printf ("% c", p-> data); p = p-> rchild;} return ;} // ***** post-order traversal of Binary Trees ***** // void PostOrderTraverse (BiTree T) {SqStack S; BiTree p = T; InitStack (S ); while (p |! StackEmpty (S) {if (p-> visitcount! = 2) {p-> visitcount = 1; Push (S, p);} p = p-> lchild;} else {Pop (S, & p ); if (p-> visitcount = 2) {printf ("% c", p-> data);} else {p-> visitcount ++; Push (S, p);} p = p-> rchild;} return;} int main (void) {BiTree T; printf ("Enter the node value (character) in the binary tree in the FIFO order, and the space character indicates the empty tree: \ n"); CreateBiTree (T ); printf ("first order traversal method 1 Result:"); PreorderTraverse_1 (T); printf ("\ n"); printf ("first order traversal method 2 result: "); PreorderTraverse_2 (T); printf (" \ n "); printf (" result 1 of the sequential Traversal method: "); InOrderTraverse_1 (T ); printf ("\ n"); printf ("ordinal Traversal method 2:"); InOrderTraverse_2 (T); printf ("\ n "); printf ("the result of post-order traversal is:"); PostOrderTraverse (T); printf ("\ n"); return 0 ;}
  
 

The following binary tree is used as an example to describe the value (character) of the node input in the binary tree in the FIFO order, and construct the binary tree according to the algorithm given in this article.


The order of input characters is:-+ a Space * B space-c space d space/e space f space, which can verify the traversal algorithm provided in this Article.

Related Article

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.