求大佬帮我看看代码。一直运行不了
头文件:
#include<stdlib.h>
#include<stdio.h>
#include <malloc.h>
typedef char DataType;
typedef struct Node
{
DataType data;
struct Node*leftChild;
struct Node*rightChild;
}BiTreeNode;
void Initiate(BiTreeNode **root)
{
*root=( BiTreeNode*)malloc(sizeof(BiTreeNode));
(*root)->leftChild=NULL;
(*root)->rightChild=NULL;
}
BiTreeNode *InsertLeftNode(BiTreeNode *curr,DataType x)
{
BiTreeNode *s,*t;
if(curr==NULL)return NULL;
t=curr->leftChild;
s=( BiTreeNode*)malloc(sizeof(BiTreeNode));
s->data=x;
s->leftChild=t;
s->rightChild=NULL;
curr->leftChild=s;
return curr->leftChild;
}
BiTreeNode *InsertRightNode(BiTreeNode *curr,DataType x)
{
BiTreeNode *s,*t;
if(curr==NULL)return NULL;
t=curr->leftChild;
s=( BiTreeNode*)malloc(sizeof(BiTreeNode));
s->data=x;
s->leftChild=t;
s->rightChild=NULL;
curr->rightChild=s;
return curr->rightChild;
}
void Destroy(BiTreeNode **root)
{
if((*root)!=NULL&&(*root)->leftChild!=NULL)
Destroy(&(*root)->leftChild);
if((*root)!=NULL&&(*root)->rightChild!=NULL)
Destroy(&(*root)->rightChild);
free(*root);
}
BiTreeNode *DeleteLeftNode(BiTreeNode*curr)
{
if(curr==NULL||curr->leftChild==NULL)return NULL;
Destroy(&curr->leftChild);
curr->leftChild=NULL;
return curr;
}
BiTreeNode *DeleteRightNode(BiTreeNode *curr)
{
if(curr==NULL||curr->rightChild==NULL)return NULL;
Destroy(&curr->rightChild);
curr->leftChild=NULL;
return curr;
}
BiTreeNode *Search(BiTreeNode *bt,DataType x)
{
BiTreeNode *p;
if(bt==NULL)return NULL;
if(bt->data==x)return bt;
if(bt->leftChild!=NULL)
{
p=Search(bt->leftChild,x);
if(p!=NULL)return p;
}
if(bt->rightChild!=NULL)
{
p=Search(bt->rightChild,x);
if(p!=NULL)return p;
}
return NULL;
}
void PreOrder(BiTreeNode *t,void Visit(DataType item))
{
if(t!=NULL)
{
Visit(t->data);
PreOrder(t->leftChild,Visit);
PreOrder(t->rightChild,Visit);
}
}
void InOrder(BiTreeNode *t,void Visit(DataType item))
{
if(t!=NULL)
{
InOrder(t->leftChild,Visit);
Visit(t->data);
InOrder(t->rightChild,Visit);
}
}
void PostOrder(BiTreeNode *t,void Visit(DataType item))
{
if(t!=NULL)
{
PostOrder(t->leftChild,Visit);
PostOrder(t->rightChild,Visit);
Visit(t->data);
}
}
int like(BiTreeNode *A,BiTreeNode *B)
{
int like1,like2;
if (A==NULL&&B==NULL)
return 1; /*两个为空子树、相似*/
else if(A==NULL||B==NULL)
return 0; /*两个中某一个为空子树、不相似*/
else
{
like1=like(A->leftChild,B->leftChild);
like2=like(A->rightChild,B->rightChild);
return(like1&&like2);
}
}
源文件:
#include<stdlib.h>
#include<stdio.h>
#include <malloc.h>
typedef char DataType;
#include"Bitree.h"
void Visit(DataType item)
{
printf("%c",item);
}
void PrintBiTree(BiTreeNode *bt,int n)
{
int i;
if(bt==NULL)return;
PrintBiTree(bt->rightChild,n+1);
for(i=1;i<n;i++)printf(" ");
if(n>=0)
{
printf("%c\n",bt->data);
}
PrintBiTree(bt->leftChild,n+1);
}
void main(void)
{
BiTreeNode *T1,*T2,*P,*PP;
Initiate(&T1);
P=InsertLeftNode(T1,'1');
InsertLeftNode(P,'2');
P=InsertRightNode(T1,'3');
P=PP;
InsertLeftNode(P,'4');
InsertLeftNode(T1->leftChild,'6');
InsertRightNode(PP,'5');
//printf("请输入二叉树T1中的元素:\n");
//PrintBiTree(T1,0);
//printf("前序遍历:");
Initiate(&T2);
P=InsertLeftNode(T1,'2');
InsertLeftNode(P,'2');
T2=InsertRightNode(T1,'3');
InsertLeftNode(T2,'4');
InsertLeftNode(T2->leftChild,'6');
InsertRightNode(T2,'5');
//printf("请输入二叉树T2中的元素:\n");
//PrintBiTree(T2,0);
//printf("前序遍历:");
if(like(T1,T2)==1)
printf("T1和T2相似!\n");
else
printf("T1和T2不相似!\n");
Destroy(&T1);
Destroy(&T2);
}