< two fork Tree basic operation >

Source: Internet
Author: User

#include <stdio.h>#include<stdlib.h>#include<string.h>#defineNum 100#defineOK 1typedefintStatus;typedefCharDatatype;typedefstructnode{DataType data; structNode *lchild,*Rchild;} Bintnode,*Bintree; Status Createbitree (Bintree&t) {//A two-fork tree is built recursively in the order of first order traversal. //[email protected]@[email protected]@@[email protected]@    Charch; scanf ("%c",&ch); if(ch = ='@') bt =NULL; Else{BT= (bintnode*)malloc(sizeof(Bintnode)); BT->data = ch;//Generating root nodesCreatebitree (Bt->lchild);//constructing the left sub-treeCreatebitree (Bt->rchild);//Constructing the right sub-tree    }    returnOK;} Status inorder (Bintree bt) {//A non-recursive algorithm for sequential traversal in two-fork treeBintnode *stack[num];//Defining stack Arrays    inttop =0;//Initialize StackStack[top] =BT;  Do    {         while(null!=Stack[top]) {//Scan the root node and all of its left nodes into the stack .top = top+1; Stack[top]= stack[top-1]->Lchild; } Top= top-1;//Back Stack        if(top>=0)//determine if the stack is empty{printf ("%c", Stack[top]->data);//Access NodesStack[top] = stack[top]->rchild;//Scan Right sub-tree        }    } while(top>=0); returnOK;}/*void vist (char ch) {printf ("%c", ch);}*/voidPostorder (Bintree bt) {//Recursive algorithm for post-sequential traversal of two fork tree    if(BT) {postorder (BT-lchild); Postorder (BT-rchild); printf ("%c",bt->data); }}intSize (Bintree bt) {//statistics of the number of nodes in a binary tree    intnum1,num2; if(bt==NULL)return 0; Else if(Bt->lchild==null && bt->rchild==NULL)return 1; Else{NUM1= Size (bt->lchild); Num2= Size (bt->rchild); return(num1+num2+1); }}intLeafcount (Bintree bt) {//the total number of leaf nodes is    intLeafnum; if(bt==NULL) Leafnum=0; Else if((Bt->lchild==null) && (bt->rchild==null)) Leafnum =1; ElseLeafnum = Leafcount (bt->lchild) +leafcount (bt->rchild); //The number of leaves is the sum of leaves of left and right subtree    returnLeafnum;}intDepth (Bintree bt) {//Statistics binary Tree depth    intHl,hr,max; if(bt!=NULL) {HL= Depth (Bt->lchild);//find the depth of the left subtreehr = Depth (bt->rchild);//to find the depth of the right sub-treemax = hl>hr?hl:hr; return(max+1);//returns the depth of the tree    }    Else        return 0;}voidExchange (Bintree bt) {//swap left and right binary trees    if(BT = =NULL)return; Bintnode*temp; Temp= bt->Lchild; BT->lchild = bt->Rchild; BT->rchild =temp; Exchange (BT-lchild); Exchange (BT-rchild);}voidMain () {Bintree BT; intXZ =1; intYZ,SD;  while(XZ) {printf ("establishment of two fork tree and its basic operation \ n"); printf ("===========================\n"); printf ("1, build the storage structure of the two-fork tree \ n"); printf ("2, the basic operation of the binary tree \ n"); printf ("3, swap the binary tree around \ n"); printf ("0 exiting the system \ n"); printf ("==========================\n"); printf ("Please select: (0~3) \ n"); scanf ("%d",&XZ);        GetChar (); Switch(XZ) {//input: [email protected]@[email protected]@@[email protected]@@ output: Cbegdfa         Case 1: printf ("Input binary tree First order sequence node value: \ n");            Createbitree (BT); printf ("two-tree chain storage structure completed \ n"); printf ("\ n");  Break;  Case 2: printf ("the sequential traversal sequence of the two-fork tree is:");            Postorder (BT); printf ("\ n");//Output CGEFDBAprintf"The Middle sequence traversal sequence of the two-fork tree is:");            Inorder (BT); printf ("\ n");//Output CBEGDFAprintf"the tree of the nodes of the two fork tree is:%d\n", Size (BT)); YZ=Leafcount (BT); printf ("the number of leaf nodes is:%d\n", YZ); SD=Depth (BT); printf ("the depth of the two-fork tree is:%d\n", SD); printf ("\ n");  Break;  Case 3: Exchange (BT); printf ("The two fork tree has been exchanged around the subtree! \ n"); printf ("\ n");  Break;  Case 0:             Break; }    }}

< two fork Tree basic operation >

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.