您的位置:首页 > 编程语言

二叉树基本操作C代码

2015-07-03 20:48 330 查看
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct ChainTree)
struct ChainTree
{
int num;
struct ChainTree *left;
struct ChainTree *right;
};
/*函数功能:进行查找操作。*/
ChainTree *BinTreeFind(ChainTree *bt,int data)//在二叉树中查找值为data的结点。
{
ChainTree *p;
if(bt==NULL)
return NULL;
else
{
if(bt->num==data)
return bt;
else{
if(p=BinTreeFind(bt->left,data))
return p;
else if(p=BinTreeFind(bt->right,data))
return p;
else
return NULL;
}
}
}
/*函数功能:先序遍历。*/
void BinTree_DLR(ChainTree *bt)
{
if(bt)
{
printf("%d",bt->num);
if(bt->left)
{
BinTree_DLR(bt->left);
}
if(bt->right)
{
BinTree_DLR(bt->right);
}
}
return;
}
/*函数定义:后序遍历。*/
void BinTree_LRD(ChainTree *bt)
{
if(bt)
{
BinTree_LRD(bt->left);
BinTree_LRD(bt->right);
printf("%d",bt->num);
}
return;
}
/*函数功能:中序遍历。*/
void BinTree_LDR(ChainTree *bt)
{
if(bt)
{
BinTree_LDR(bt->left);
printf("%d",bt->num);
BinTree_LDR(bt->right);
}
return;
}
/*函数功能初始化一个二叉树,建立根节点。*/
ChainTree *InitRoot()
{
ChainTree *node;
node=(struct ChainTree*)malloc(LEN);
printf("请输入根节点的值:");
scanf("%d",&node->num);
node->left=NULL;
node->right=NULL;
return node;
}

/*添加数据到二叉树*/
int BinTreeAddNode(ChainTree *bt,ChainTree *node,int n)
{
if(bt==NULL)
{
printf("\n父结点不在,请先设置父结点。");
return 0;
}
switch(n)
{
case 1:
if(bt->left)
{
printf("左子树结点不为空。");
return 0;
}
else
bt->left=node;
break;
case 2:
if(bt->right)
{
printf("右子树结点不为空。");
return 0;
}
else
bt->right=node;
break;
default:
printf("参数错误!\n");
return 0;
}
return 1;
}

/*函数功能:添加结点到二叉树。*/
void AddNode(ChainTree *bt)
{
int data;
int select;
ChainTree *node,*parent;
node=(struct ChainTree*)malloc(LEN);
printf("\n输入二叉树结点数据");
scanf("%d",&node->num);
node->left=NULL;
node->right=NULL;
printf("\n输入父结点数据:");
scanf("%d",&data);
parent=BinTreeFind(bt,data);
if(!parent)
{
printf("\n未找到父结点。");
}
printf("\n1.添加到左子树\n2.添加到右子树\n");
do{
scanf("%d",&select);
if(select==1||select==2)
BinTreeAddNode(parent,node,select);
}while(select!=1&&select!=2);
}

/*求二叉树叶子结点个数*/
void CountLeaf(ChainTree *bt,int &count)
{
if(bt)
{
if((!bt->left)&&(!bt->right))
count++;
CountLeaf(bt->left,count);
CountLeaf(bt->right,count);
}
}

/*求二叉树深度*/
int BinTreeDepth(ChainTree *bt)
{
int dep1,dep2;
if(bt==NULL)
return 0;
else
{
dep1=BinTreeDepth(bt->left);                //左子树深度(递归调用)
dep2=BinTreeDepth(bt->right);              //右子树深度(递归调用)
if(dep1>dep2)
return dep1+1;
else
return dep2+1;
}
}

int main()
{
struct ChainTree *p1,*p2,*head,*p3;
int select = 1000;
int countleaf=0;                                        //该变量计算叶子结点个数
while(select!=0){
printf("\n1.设置二叉树根元素\n2.添加二叉树根节点\n3.先序遍历\n4.中序遍历\n5.后序遍历\n6.输出叶子结点个数\n7.求二叉树深度\n0.退出");
scanf("%d",&select);
switch(select)
{
case 1:
head=InitRoot();
break;
case 2:
AddNode(head);
break;
case 3:
BinTree_DLR(head);
break;
case 4:
BinTree_LDR(head);
break;
case 5:
BinTree_LRD(head);
break;
case 6:
countleaf=0;                                              //求二叉树叶子结点数
CountLeaf(head,countleaf);
printf("\n%d",countleaf);
break;
case 7:
printf("二叉树深度为:%d\n",BinTreeDepth(head));
break;
case 0:
select = 0;
break;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: