# include
# include
Typedef char ElemType;//the type of data domain
Static int b=0;
Typedef struct node
{
ElemType data;//data domain
Struct node * lc;//the left child pointer
Struct node * rc;//right child pointer
} BitTree;
BitTree * CreBiTree ()
{BitTree * bt; ElemType x; The scanf (" % c ", & amp; X);
If (x==' ') bt=NULL;
The else {bt=(BitTree *) malloc (sizeof (BitTree)); Bt - & gt; data=https://bbs.csdn.net/topics/x;
Bt - & gt; Lc=CreBiTree ();
Bt - & gt; Rc=CreBiTree ();
}}
Int PreOrder BitTree * (bt)
{
If (bt!!!!=NULL)
{printf (" % c ", bt - & gt; The data); PreOrder (bt - & gt; Lc); PreOrder (bt - & gt; Rc);
}}
Int NodeCount (bt) BitTree * {
If (bt!!!!=NULL) NodeCount (bt - & gt; Lc) + NodeCount (bt - & gt; Rc) + 1;
{} int hightree BitTree * (bt)
Int H, H1, H2,
If (bt==NULL) H=0;
The else {H1=hightree (bt - & gt; Lc); The H2=hightree (bt - & gt; Rc); H=(H1 & gt; The H2? H1, H2) + 1; }
Return H;
}
Int onechild BitTree * (bt)
{
Static int a=0;
If (bt!!!!=NULL)
{if (bt - & gt; Lc!=NULL& & Bt - & gt; Rc==NULL) +; Onechild (bt - & gt; Lc); Onechild (bt - & gt; Rc);
}
return a;
}
Int LeafCount (bt) BitTree * {static int n=0; If (bt!!!!=NULL) {if (bt - & gt; Lc==NULL & amp; & Bt - & gt; Rc==NULL) {n++; } LeafCount (bt - & gt; Lc); LeafCount (bt - & gt; Rc); } return n. } int main () {int choice, I; BitTree * R=NULL; Int Running=1; While (Running) {printf (" -- -- -- -- -- - the operation of the binary tree -- -- -- -- -- - \ n "); Printf (" 1. The importation of the binary tree \ n "); Printf (" (2) of the binary tree traversal \ n "); Printf (" 3. Statistical binary tree node number \ n "); Printf (" 4. Calculate the depth of a binary tree \ n "); Printf (" 5. Statistical binary tree the single child node number \ n "); Printf (" 6. Statistical binary number leaves nodes \ n "); Printf (" 0. Quit \ n "); Printf (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- \ n "); Printf (" \ n in accordance with the above function of digital input you want: "); The scanf (" % d ", & amp; Choice); The switch (choice) {case 1: printf (" please input data: \ n "); The fflush (stdin); R=CreBiTree (); Printf (" create success \ n "); B=1; break; Case 2: PreOrder (R); break; Case 3: I=NodeCount (R); Printf (" the leaves of the tree node number is: % d \ n ", I);
Case 4: I=hightree (R); Printf (" the height of the tree is: % d \ n ", I); Case 5: if (b==0) {printf (" has not been established binary tree \ n "); } else {I=onechild (R); Printf (" single child node number is: % d \ n ", I); } break; Case 6: I=LeafCount (R); Printf (" the leaves of the tree node number is: % d \ n ", I); break; Case 0: printf (" quit!" ); Default: printf (" please input the correct number! \n"); break; }
}
}
CodePudding user response:
BitTree * CreBiTree (){
Bt BitTree *;
ElemType x;
The scanf (" % c ", & amp; X);
If (x==' ')
Bt=NULL;
The else {
Bt=(BitTree *) malloc (sizeof (BitTree));
Bt - & gt; data=https://bbs.csdn.net/topics/x;
Bt - & gt; Lc=CreBiTree ();
Bt - & gt; Rc=CreBiTree ();
}
return bt;//the lack of the
}
This is the common fault of the you, have no return, no return will not be able to do the subsequent processing
Several other function also is the problem
CodePudding user response:
PreOrder sequence is wrong, should be left tree own numerical first, then the order of the right tree left right; Your sequence output is about in theNodeCount count is wrong, no return, the algorithm is {
Int c=1;
If the left subtree is not empty c +=NodeCount (left subtree)
If the right subtree is not empty c +=NodeCount (right subtree)
return c;
} you like HightTree algorithm
Behind a pile of lumps of things is not interested to see, if you want to help others, at least make things let a person look not vexed!
CodePudding user response:
Fun