二叉树的基本操作

#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct ChainTree)
#define NULL 0
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;
	}
}

void 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;
		}
	}
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值