1、实现二叉树的插入操作,并利用先序遍历的实现打印。
如图所示:
打印的结果为:ABCDFGE
#include"stdlib.h"
#include"stdio.h"
typedef char DataType;
#include"BiTree.h"
void main()
{
BiTreeNode *root;
Initiate(&root);
if(root!=NULL)
root=createbintree();
printf("先序遍历:");
PreOrder(root,visit); //先序遍历
printf("\n");
printf("中序遍历:");
InOrder(root,visit); //中序遍历
printf("\n");
printf("后序遍历:");
PostOrder(root,visit); //后序遍历
printf("\n");
printf("二叉树中结点个数为:%d\n",numofnode(root));
printf("二叉树中叶子结点个数为:%d\n",preleafnum(root));
if(locate(root,'E'))
printf("数据存在!\n");
else
printf("数据不存在!\n");
Destroy(&root);
}
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;
}
void Destroy(BiTreeNode **root)
{
if((*root)!=NULL&&(*root)->leftChild!=NULL)
Destroy(&(*root)->leftChild);
if((*root)!=NULL&&(*root)->rightChild!=NULL)
Destroy(&(*root)->rightChild);
free(*root);
}
/*若当前结点curr非空,在curr的左子树插入元素值为x的新结点*/
/*原curr所指结点的左子树成为新插入结点的左子树*/
/*若插入成功返回新插入结点的指针,否则返回空指针*/
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->rightChild; /*保存原curr所指结点的右子树指针*/
s=(BiTreeNode *)malloc(sizeof(BiTreeNode));
s->data=x;
s->rightChild=t; /*新插入结点的右子树为原curr的右子树*/
s->leftChild=NULL;
curr->rightChild=s; /*新结点成为curr的右子树*/
return curr->rightChild; /*返回新插入结点的指针*/
}
BiTreeNode *DeleteLeftTree(BiTreeNode *curr) /*若curr非空,删除curr所指结点的左子树*/
{
if(curr==NULL||curr->leftChild==NULL)
return NULL;
Destroy(&curr->leftChild);
curr->leftChild=NULL;
return curr; /*若删除成功返回删除结点的双亲结点指针,否则返回空指针*/
}
BiTreeNode *DeleteRightTree(BiTreeNode *curr)
{
if(curr==NULL||curr->rightChild==NULL)
return NULL;
Destroy(&curr->rightChild);
curr->rightChild=NULL;
return curr;
}
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);
}
}
void visit(DataType item)
{
printf("%c",item);
}
BiTreeNode *createbintree(void)
{
BiTreeNode *pbnode;
char ch;
scanf("%c",&ch);
if(ch=='#')
pbnode=NULL;
else
{
pbnode=(BiTreeNode *)malloc(sizeof(BiTreeNode));
if(pbnode==NULL)
return pbnode;
//InsertleftNode(pbnode,ch);
pbnode->data=ch;
pbnode->leftChild=createbintree();
pbnode->rightChild=createbintree();
}
return pbnode;
}
int numofnode(BiTreeNode *t)
{
if(t==NULL)
return 0;
else
return(numofnode(t->leftChild)+numofnode(t->rightChild)+1);
}
int preleafnum(BiTreeNode *t)
{
if(t==NULL)
return 0;
else
{
if((t->leftChild==NULL)&&(t->rightChild==NULL))
{
return 1;
}
return preleafnum(t->leftChild)+preleafnum(t->rightChild);
}
}
int locate(BiTreeNode *t,DataType x)
{
BiTreeNode *p;
if(t==NULL)
return 0;
if(t->data==x)
return 1;
else
{
p=locate(t->leftChild,x);
if(p)
return 1;
else
return locate(t->rightChild,x);
}
}