Home > Back-end >  Binary tree is flawed, the output of the great god, please help to look at
Binary tree is flawed, the output of the great god, please help to look at

Time:12-19

Should be output to A (B (G), (D), C (E, F))

#include
using namespace std;
#include
# define MAXSIZE 100

Struct BTNode
{
char data;
BTNode * lchild;
BTNode * rchild;
};

Void CreateBTree (BTNode * & amp; B, char * STR)
{
BTNode * st [MAXSIZE];
BTNode * p;
Int top=1, k, j=0;
char ch;
B=NULL;
Ch=STR [j];
While (ch!='\ 0')
{
The switch (ch)
{
Case '(' : top++;
St [top]=p;
k=1;
break;
Case ') : top -;
break;
Case ', ': k=2;
break;
Default: p=(BTNode *) malloc (sizeof (BTNode));//to create a space p
P - & gt; data=https://bbs.csdn.net/topics/ch;
P - & gt; Lchild=p - & gt; Rchild=NULL;
If (b==NULL)
{
B=p;//p address is assigned to the b
}
The else
{
The switch (k)
{
Case 1: st [top] - & gt; Lchild=p;//p new address it will pay the st [TOP] - & gt; Lchild
break;
Case 2: st [top] - & gt; Rchild=p;
break;
}
}
}
j++;
Ch=STR [j];
}
}

Void DispBTree (BTNode * b)
{
If (b!=NULL)
{
CoutIf (b - & gt; Lchild! B=NULL | | - & gt; Rchild!=NULL)
{
Cout<& lt;" (";
DispBTree (b - & gt; Lchild);
If (b - & gt; Rchild!=NULL)
Cout<& lt;" , ";
DispBTree (b - & gt; rchild);
Cout<& lt;" ) ";
}
}
}

/* void sameDispBTree (BTNode * b, BTNode * m)
{
If (b! M=NULL | |!=NULL)
{
Cout<& lt;" Both don't want to such as "& lt; }
The else
{
If (b - & gt; data=https://bbs.csdn.net/topics/=m-> data)
{
If (b - & gt; Lchild!=NULL& & M - & gt; Lchild!=NULL)
SameDispBTree (b - & gt; Lchild, m - & gt; Lchild);
Else if (b - & gt; Rchild!=NULL& & M - & gt; Rchild!=NULL)
SameDispBTree (b - & gt; Rchild, m - & gt; rchild);
The else
Cout<& lt;" Both does not equal "& lt; }
The else
Cout<& lt;" Both does not equal "& lt; }
} */

Int main ()
{
BTNode *, b * m;
Char str1 [MAXSIZE]={' A ', '(',' B ', '(',' D ', '(',', ', 'G', ') ', ') ', ', ', '(',' E ', ', ', 'F', ') ', ') '};
Char str2 [MAXSIZE]={' M ', '(',' B ', '(',' D ', '(',', ', 'G', ') ', ') ', ', ', '(',' E ', ', ', 'F', ') ', ') '};
CreateBTree (b, str1);
CreateBTree (m, str2);
DispBTree (b);
CoutDispBTree (m);
//sameDispBTree (b, m);
}

CodePudding user response:

Output do not want to compare, and why a c
  • Related