Problem Description
(1) Establishment of a two-fork tree (2) based on the square bracket notation of the binary tree;
gets its bracket notation string
(3) Output binary tree depth (4) The width of the output binary tree
(5) The number
of nodes of the output binary tree (6) Output binary tree leaf node number
Code
/* Function: (1) build two-fork tree (2) according to the square bracket notation of the binary tree; gets its bracket notation string (3) output binary tree depth (4) Output binary tree width (5) Output Binary tree node number (6) Output fork Number of leaf nodes Author: pussy Date: 2015-11-22 * * # include<stdio.h> # include<malloc.h> # include<string.h> #define MAX. typedef struct binode{Char data;//record the node information of the binary tree int cs;//record the number of layers in the binary tree struct Binode *lchild,*rchild;}
Binode,*bitree;
typedef struct{Bitree Data[max];
int top;
}stack;
typedef struct{Bitree Data[max];
int front,rear;
}queue;
void Initstack (Stack &s);
int Isfull (Stack s);
int IsEmpty (Stack s);
int Push (Stack &s,bitree p);
int Pop (Stack &s);
int GetTop (Stack &s,bitree &p);
int Createbitree (Bitree &t,char str[]);
Bitree NewNode (char c);
void Printbitree (Bitree T);
int getheight (Bitree T);
int getwidth (Bitree T);
void Getnum (Bitree t,int &num1,int &num2);
void Initqueue (Queue &q);
int EnQueue (Queue &q,bitree t);
Bitree DeQueue (Queue &q); int main () {int num1,num2;//NUM1 is used to record the number of nodes in a binary tree, num2 is used to record the number of leaf nodes of the binary tree char khdata[max];//Two The parentheses of the tree are denoted by the string printf ("Please enter the parentheses notation of the binary tree: \ n");
scanf ("%s", Khdata);
Bitree T=null;
int T=createbitree (t,khdata);
if (t==1) printf ("Binary tree established successfully!\n");
else {printf ("Binary tree Setup failed!\n");
return 0;
} printf ("Gets the parentheses notation string according to the binary tree that was built:");
Printbitree (T);
printf ("\ n Tree Depth:%d\n", GetHeight (T));
printf ("The width of the tree is:%d\n", GetWidth (T));
Getnum (T,NUM1,NUM2);
printf ("The number of nodes in the binary tree is:%d, the number of leaf nodes is:%d\n", num1,num2);
return 0;
}//reconstruct binary tree int createbitree (bitree &t,char str[]) {int i=0 according to the square bracket notation of the binary tree;
Stack s;
Initstack (s);
if (strlen (str) ==0)//empty tree {t=null;
return 0;
} t=newnode (str[i++]);
Bitree t=t;
Push (s,t); End flag: Only root nodes are left in the stack, and the string is scanned while (I<strlen (str) | |
(GetTop (s,t) &&t!=t)) {switch (Str[i]) {case ' (': if (str[i+1]!= ', ') {//gettop (
S,T); T->lchIld=newnode (str[i+1]);
Push (S,t->lchild);
t=t->lchild;
i=i+2;
} else {Push (s,null);
i++;
} break;
Case ', ': Pop (s);
GetTop (s,t);
T->rchild=newnode (str[i+1]);
Push (S,t->rchild);
t=t->rchild;
i=i+2;
Break
Case ') ': Pop (s);
i++;
Break
}} return 1;
}//Bracket notation prints out two fork tree void Printbitree (Bitree T) {if (t==null) return;
printf ("%c", t->data);
if (t->lchild==null&&t->rchild==null) {return;
} else {printf (");
if (t->lchild!=null) Printbitree (t->lchild);
if (t->rchild!=null) {printf (",");
Printbitree (T->rchild);
} printf (")");
}
}Get the depth of the binary tree//Two The depth of the fork tree is equal to its left subtree, the higher right subtree depth plus an int getheight (Bitree T) {if (t==null) return 0; else{return (getheight (t->lchild) >getheight (t->rchild) getheight (t->lchild): GetHeight (T->rchild)
) +1;
}}//hierarchy traverse binary tree to get two fork tree width//First to find out the number of nodes per layer, the binary tree width is equal to the number of nodes the most node of the number of nodes int getwidth (Bitree T) {int Num[max];
memset (num,0,sizeof (num));
if (T==null) return 0;
else {Queue q;
Bitree T=null;
Initqueue (q);
EnQueue (q,t);
t->cs=1;
num[t->cs]++; while (!) (
q.front==q.rear&&q.data[q.front]==0))//Queue not empty {t=dequeue (q);
if (t->lchild) {t->lchild->cs=t->cs+1;
num[t->lchild->cs]++;
EnQueue (Q,t->lchild);
} if (T->rchild) {t->rchild->cs=t->cs+1;
num[t->rchild->cs]++; EnQueue (q,t->rchild);
}} int i,max=0;
for (i=1;num[i];i++) {if (Num[i]>max) max=num[i];
} return max;
}}//sequential traversal Gets the number of nodes of the two-fork tree and the number of leaf nodes void Getnum (Bitree t,int &num1,int &num2) {num1=0;num2=0;
if (t==null)//empty tree {return;
} else {Stack s;
Initstack (s);
Push (s,t);
num1++;
if (t->lchild==null&&t->rchild==null) num2++;
Bitree t=t;
while (!isempty (s)) {while (t) {Push (s,t->lchild);
if (t->lchild) {num1++;
if (t->lchild->lchild==null&&t->lchild->rchild==null) num2++;
} t=t->lchild;
} Pop (s);
if (!isempty (s)) {getTop (s,t); Pop (s);
Push (S,t->rchild);
if (t->rchild) {num1++;
if (t->rchild->lchild==null&&t->rchild->rchild==null) num2++;
} t=t->rchild;
}}}}//into the stack, if successful, return 1, otherwise return 0 int Push (Stack &s,bitree p) {if (Isfull (s)) return 0;
else {s.data[s.top]=p;
s.top++;
return 1;
}}//Out of the stack, if the stack succeeds, return 1, otherwise return 0 int Pop (Stack &s) {if (IsEmpty (s)) return 0;
else {s.top--;
return 1;
}}//Get stack top element, successfully returned 1, failed to return 0 int getTop (stack &s,bitree &p) {if (IsEmpty (s)) return 0;
else {p=s.data[s.top-1];
return 1;
}}//Assign a new node Bitree newnode (char c) {Bitree t= (bitree) malloc (sizeof (Binode));
t->data=c;
t->lchild=t->rchild=null;
return t; }//Initialize stack void InitstaCK (Stack &s) {s.top=0;}
Determines whether the stack is full, if so, returns 1, otherwise returns 0.
int Isfull (Stack s) {if (S.top>=max) return 1;
else return 0;
}//Determine if the stack is empty, and if so, return 1, otherwise return 0 int isEmpty (stack s) {if (s.top==0) return 1;
else return 0;
}//Initialize queue void Initqueue (queue &q) {q.front=q.rear=0; memset (q.data,0,max*sizeof (char));//init queue data is 0}//enqueue (circular queue), successful return 1, failure returns 0 int EnQueue (Queue &q,bitree t) {if (Q.fro
nt==q.rear&&q.data[q.front]!=0)//queue full return 0;
else {q.data[q.rear]=t;
Q.rear= (q.rear+1)%max;
return 1;
}}//Out of team, failed to return 0 bitree DeQueue (Queue &q) {bitree T=q.data[q.front];
q.data[q.front]=0;
Q.front= (q.front+1)%max;
return t;
}
Run Results