Home > Back-end >  About the tree program, the program shows that there are no mistakes, how to input the value ran out
About the tree program, the program shows that there are no mistakes, how to input the value ran out

Time:09-24

# include
# include
Typedef struct BiTNode
{
Char data;
Struct lchild BiTNode * and * rchild;
} BiTNode, * BiTree;
Typedef struct
{
Char * base;
Char * top;
Int stacksize;
} SqStack;
SqStack S;
BiTree T;
Void CreateBiTree (BiTree & amp; T)
{
Char ch;
The scanf (" % c ", & amp; Ch);
If (ch=='#')
T=NULL;
The else
{
T=new BiTNode;
T - & gt; data=https://bbs.csdn.net/topics/ch;
CreateBiTree (T - & gt; Lchild);
CreateBiTree (T - & gt; Rchild);
}
}
Void beforeOrderTraverse (BiTree T)
{
If (T)
{
Printf (" % c ", T - & gt; The data);
BeforeOrderTraverse (T - & gt; Lchild);
BeforeOrderTraverse (T - & gt; Rchild);
}
}
Void InOrderTraverse (BiTree T)
{
If (T)
{
InOrderTraverse (T - & gt; Lchild);
Printf (" % c ", T - & gt; The data);
InOrderTraverse (T - & gt; Rchild);
}
}
Void afterOrderTraverse (BiTree T)
{
If (T)
{
AfterOrderTraverse (T - & gt; Lchild);
AfterOrderTraverse (T - & gt; Rchild);
Printf (" % c ", T - & gt; The data);
}
}
Int InitStack (SqStack & amp; S)
{
S.b ase=new char [100].
if(! S.b ase)
exit(0);
S.t op=S.b ase;
S.s tacksize=100;
return 1;
}
Int StackEmpty (SqStack S)
{
If (S.s tacksize==0)
return 1;
The else
return 0;
}
Int a Push (SqStack & amp; S, char e)
{
If (S.t op - S.b ase==S.s tacksize)
return 0;
* S.t op++=e;
return 1;
}
Int Pop (SqStack & amp; S, char & amp; E)
{
If (S.t op=S.b ase)
return 0;
E=* - S.t op;
return 1;
}
Void OrderTraverse (BiTree T)
{
InitStack (S);
BiTree p, q;
P=T;
Q=new BiTNode;
While (p | |! StackEmpty (S))
{
If (p)
{
Push (S, p - & gt; The data);
P=p - & gt; Lchild;
}
The else
{
Pop (S, q - & gt; The data);
Printf (" % c ", q - & gt; The data);
P=q - & gt; Rchild;
}
}
}
Int the Depth (BiTree T)
{
Int m, n;
If (T=NULL)
return 0;
The else
{
M=the Depth (T - & gt; Lchild);
N=the Depth (T - & gt; Rchild);
If (m> N)
Return (m + 1);
The else
Return (n + 1);
}
}
Int NodeCount (BiTree T)
{
If (T=NULL)
return 0;
The else
Return NodeCount (T - & gt; Lchild) + NodeCount (T - & gt; Rchild) + 1;
}
Int main ()
{
int a,b;
Printf (" preorder traversal generated binary tree: ");
CreateBiTree (T);
Printf (" preorder traversal of the above generated binary tree: ");
BeforeOrderTraverse (T);
Printf (" generated sequence traversal of the binary tree: ");
InOrderTraverse (T);
Printf (" sequence traversal generated binary tree after: ");
AfterOrderTraverse (T);
Printf (" non recursive way of traversing binary tree: ");
OrderTraverse (T);
A=the Depth (T);
Printf (" the depth of the output binary tree: % d \ n ", a);
B=NodeCount (T);
Printf (" output node number: % d \ n ", b);
return 0;
}



















CodePudding user response:

This program writing has a problem, but if you want to run to see the results, can enter the string and then press enter
12 # # # # # # #
To have time, you can change for you
  • Related