//binary tree. CPP: this file contains the "main" function, the program execution will start and end here,
//
# include
using namespace std;
Typedef struct BiTNode
{
char data;
Struct lchild BiTNode * and * rchild;
} BiTNode, * BiTree;
Void InitBiTree (BiTree& T)
{
char c;
Cin & gt;> c;
If (c=='#')
T=NULL;
The else
{
T=new BiTNode;
T - & gt; Data=https://bbs.csdn.net/topics/c;
InitBiTree (T - & gt; Lchild);
InitBiTree (T - & gt; Rchild);
}
}
Void PreOrderTravel1 (BiTree T)
{
If (T)
{
cout
PreOrderTravel1 (T - & gt; Rchild);
}
}
//in order
Void InOrderTravel1 (BiTree T)
{
If (T)
{
InOrderTravel1 (T - & gt; Lchild);
cout
}
}
//after order
Void TailOrderTravel1 (BiTree T)
{
If (T)
{
TailOrderTravel1 (T - & gt; Lchild);
TailOrderTravel1 (T - & gt; Rchild);
cout
}
//stack
Typedef struct
{
BiTNode * stack [1000];
Int top;
} SqStack;
Void InitStack (SqStack& S)
{
S.t op=1;
}
Int StackEmpty (SqStack S)
{
If (S.t op==1)
return 1;
The else
return 0;
}
Void Push (SqStack& S, BiTNode * e)
{
S.t op++;
S.s tack [S.t op]=e;
}
Pop (SqStack& is void; S, BiTNode * e)
{
E=S.s tack [S.t op];
S.top--;
}
//non-recursive
Void InOrderTravel2 (BiTree T)
{
SqStack S;
InitStack (S);
BiTree p=T;
BiTNode * q=new BiTNode;
While (p | |! StackEmpty (S))
{
If (p)
{
Push (S, p);
P=p - & gt; Lchild;
}
The else
{
Pop (S, q);
cout P=q - & gt; Rchild;
}
}
}
//copy
Void Copy (BiTree T, BiTree& NewT)
{
If (T==NULL)
{
NewT=NULL;
return;
}
The else
{
NewT=new BiTNode;
NewT - & gt; Data=https://bbs.csdn.net/topics/T-> data;
Copy (T - & gt; Lchild, newT - & gt; Lchild);
Copy (T - & gt; Rchild, newT - & gt; Rchild);
}
}
//depth
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 & gt; N)
Return (m + 1);
The else
Return (n + 1);
}
}
Int main ()
{
BiTree T;
cout <"Please enter to establish binary sequence list: \ n";
InitBiTree (T);
cout <"First sequence traversal results for: \ n";
PreOrderTravel1 (T);
cout <"\ n";
cout
cout <"\ n";
cout <"In the sequence traversal results for (non-recursive) : \ n";
InOrderTravel2 (T);
cout <"\ n";
cout <"After the sequence traversal results for: \ n";
TailOrderTravel1 (T);
cout <"\ n";
BiTree newT.
Copy (T, newT);
cout <"Copy of the new tree sequence traversal results as follows: first \ n";
PreOrderTravel1 (newT);
cout <"\ n";
cout <"The new tree sequence traversal results of system as follows: \ n";
InOrderTravel1 (newT);
cout <"\ n";
cout <"The new tree sequence traversal results of system as follows: \ n";
TailOrderTravel1 (newT);
cout <"\ n";
cout <"The depth of the tree for: \ n";
cout
}
CodePudding user response:
No left and right subtrees initialization of nodes, so the address is a random valueCodePudding user response: