#include
# define the ERROR 0
# define OK 1
# define TRUE 1
# define FALSE 0
Typedef int the Status;
Typedef char TElemType;/* assume binary tree node element types for the characters */
Typedef struct BiTNode
{
TElemType data;/* * data domain/
Struct lchild BiTNode * and * rchild;/* the left and right child pointer field */
} BiTNode, * BiTree; Binary list/* */
Void InitBiTree (BiTree & amp; T)
{
T=NULL;
}
BiTree MakeBiTree (BiTree BiTree TElemType e, L, R)
{//T create a binary tree, the root node of the value of e, L and R, respectively, as the left tree and right subtree
BiTree t;
T=(BiTree) malloc (sizeof (BiTNode));
If return NULL (NULL==t);
T - & gt; Data=https://bbs.csdn.net/topics/e;
T - & gt; Lchild=L;
T - & gt; Rchild=R.
return t;
}
Void DestoryBiTree (BiTree & amp; T)
//initial conditions: binary tree T already exist, operation results: destruction of binary tree T,
{
If (T) {
DestoryBiTree (T - & gt; Lchild);
DestoryBiTree (T - & gt; Rchild);
Free (T);
}
}
The Status BiTreeEmpty (BiTree T)
//initial conditions: binary tree T already exist, results: the operation of binary tree to empty, if is empty return TRUE, otherwise it returns FALSE,
{
If (T==NULL)/* root node is empty, the empty tree */
return TRUE;
The else
return FALSE;
}
The Status BreakBiTree (BiTree & amp; T, BiTree & amp; L, BiTree & amp; R)
//initial conditions: binary tree T already exist, results: the operation will be broken down into a binary tree T root, left child tree and right subtree three parts,
{
If (T)
{
L=T - & gt; Lchild;
R=T - & gt; Rchild;
T - & gt; Lchild=NULL;
T - & gt; Rchild=NULL;
Return OK;
}
The else return ERROR;
}
The Status RePlaceLeft (BiTree & amp; T, BiTree & amp; LT)
//initial conditions: binary tree T already exist, results: the operation to replace the left subtree, if T is not empty, replace with LT T left subtree, and LT to return to the original left subtree, T
{BiTree temp.
If (NULL==T) return the ERROR;
Temp=T - & gt; Lchild;
T - & gt; Lchild=LT;
LT=temp;
Return OK;
}
The Status RePlaceRight (BiTree & amp; T, BiTree & amp; RT)
//initial conditions: binary tree T already exist, operation results: replace right subtree, if T is not empty, replace with RT T left subtree, and RT to return to the original right subtree, T
{BiTree temp.
If (NULL==T) return the ERROR;
Temp=T - & gt; Rchild;
T - & gt; Rchild=RT;
RT=temp;
Return OK;
}
The Status Display (BiTree T)
{if (T==NULL)
{printf (" # ");
Return the ERROR;
} the else
{
Printf (" % c ", T - & gt; The data);
Printf (" (");
The Display (T - & gt; Lchild);
Printf (", ");
The Display (T - & gt; Rchild);
Printf (") ");
Return OK; }
}
Int main (void)
{
int i;
TElemType e;
BiTree T, L, R, A, B, C, D, E, F, G;
InitBiTree (T);
E='D';
D=MakeBiTree (e, NULL, NULL);
E='e';
E=MakeBiTree (E, NULL, NULL);
E='B';
B=MakeBiTree (e, D, e);
E='F'.
F=MakeBiTree (e, NULL, NULL);
E='G';
G=MakeBiTree (e, NULL, NULL);
E='C';
C=MakeBiTree (e, F, G);
E='A';
A=MakeBiTree (e, B, C);
T=A;
Printf (" T sequence \ n ");
The Display (T);
Printf (" \ n found empty \ n ");
I=BiTreeEmpty (T);
If (I) printf (" \ n ");
The else printf (" n \ n ");
Printf (" \ n to replace, replace with LR T about subtree \ n ");
RePlaceLeft (T, L);
RePlaceRight (T, R);
Printf (" T sequence \ n ");
The Display (T);
Printf (" \ n \ nL sequence ");
The Display (L);
Printf (" \ n \ nR sequence ");
The Display (R);
Printf (" \ n decomposition operation, the left and right assigned to the LR \ n ");
BreakBiTree (T, L, R);
Printf (" T sequence \ n ");
The Display (T);
Printf (" \ n \ nL sequence ");
The Display (L);
Printf (" \ n \ nR sequence ");
The Display (R);
Printf (" \ n \ n destroy T ");
DestoryBiTree (T);
Printf (" T sequence \ n ");
The Display (T);
Printf (" \ n found empty \ n ");
I=BiTreeEmpty (T);
If (I) printf (" \ n ");
The else printf (" n \ n ");
}
CodePudding user response:
Look at the Call StackCodePudding user response:
L and R value is random, not been initialized, as a pointer might be disorderly refers to, is likely to point to to the outside of the program memory, which caused by the page protection of operating system is unusual,