The binary tree of C language programming

Source: Internet
Author: User

Using the linked list to build two-fork tree, complete the pre-sequence traversal, middle sequence traversal, post-order traversal.

Build a two-fork tree using a pre-sequence traversal to build a two-fork tree:

  1#include <stdio.h>2#include <stdlib.h>3#include <math.h>4#include <malloc.h>5   6   7typedefintelement; 8typedefstructtree{9         structTree *lchild,*Rchild;Ten         Chardata; One}*Ptree,pnode; A   - //Char str[]= "abd#e# #FG # # # #CI # #H # #";  - Charstr[]="abdh#k## #E # #CFI # # #G #j##";  theElement index=0;  - //Init a empty tree  -Element Init_tree (PTree *T) -         {  +*t=NULL; -printf"init a empty tree\n");  +         }  A   at //Build a tree  - voidCreat_tree (Pnode * *T) -         {  -            Charnum; -num=str[index++];  -PTree p=NULL; in            //p=*t;  -            if(num=='#')  to*t=NULL; +            Else  -              {  the               //if (! ( *t))  *                 //exit (OVERFLOW);  $(*t) = (pTree) malloc (sizeof(Pnode)); Panax Notoginseng                //memset (*t,0,sizeof (Pnode));  -(*t)->data=num; theCreat_tree (& (*t),lchild);  +Creat_tree (& (*t),rchild);  A              }  the         }  +   - //Pro Order Traversal  $element Pro_trav (pTree T) $         {  -            if(t==NULL) -                 return ;  theprintf"Pro Order Traversal data:%c\n",t->data);  -Pro_trav (t->lchild); WuyiPro_trav (t->rchild);  the         }  -   Wu //midle Order Traversal  -element Mid_trav (pTree T) About         {  $            if(t==NULL) -                 return;  -Mid_trav (t->lchild);  -printf"Mid order traversal data:%c\n",t->data);  AMid_trav (t->rchild);  +         }  the   - //After traversal  $element Aft_trav (pTree T) the         {  the            if(t==NULL) the                 return;  theAft_trav (t->lchild);  -Aft_trav (t->rchild);  inprintf"After order traversal data:%c\n",t->data);  the         }  the   Aboutelement Main () the         {  thePTree T; thet= (PTree) malloc (sizeof(structTree));  +            //Init_tree (&t);  -printf"before is ok\n");  theCreat_tree (&T); BayiPro_trav (T); theMid_trav (T); theAft_trav (T); -            return 0;  -         }  the 

Output Result:

1Before isOK2 Pro Order Traversal data:a3 Pro Order Traversal data:b4 Pro Order Traversal data:d5 Pro Order Traversal data:h6 Pro Order Traversal data:k7 Pro Order Traversal data:e8 Pro Order Traversal DATA:C9 Pro Order Traversal data:fTen Pro Order Traversal DATA:I One Pro Order Traversal data:g A Pro Order Traversal data:j - Mid order traversal data:h - Mid order traversal data:k the Mid order traversal data:d - Mid order traversal data:b - Mid order traversal data:e - Mid order traversal data:a + Mid order traversal DATA:I - Mid order traversal data:f + Mid order traversal DATA:C A Mid order traversal data:g at Mid order traversal data:j - After order traversal data:k - After order traversal data:h - After order traversal data:d - After order traversal data:e - After order traversal data:b in After order traversal DATA:I - After order traversal data:f to After order traversal data:j + After order traversal data:g - After order traversal data:c theAfter order traversal data:a

The binary tree of C language programming

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.