二叉树的各种操作

定义二叉树节点;实现二叉树的生成、遍历(前序,中序和后序)、查询二叉树中某个节点、统计二叉树节点个数、统计二叉树叶子节点个数、求二叉树的深度、复制二叉树等函数。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct BitNode
{
	int data;	//数据域
	struct BitNode *lchild, *rchild;	//左右孩子指针
}BitNode, *BiTree;
BitNode*Init_BTNode()	//初始化二叉树
{
	int a;
	BitNode*T;
	T = (BitNode*)malloc(sizeof(BitNode));
	printf("输入根节点:(0表示空树)\n");
	scanf("%d", &a);	//输入0表示空树
	if (a == 0)
	{
		printf("这是空树!");
		system("pause");
		exit(0);
	}
	T->data = a;
	T->lchild = NULL;	//左子树节点
	T->rchild = NULL;	//右子树节点
	return T;
}

int createBiTree(BitNode*bt)	//树的生成
{
	int a;
	BitNode*Node;
	printf("请输入%d节点的左孩子(0为空)\n", bt->data);
	scanf("%d",&a);
	if (a != 0)			//递归法为树添加元素
	{
		Node = (BitNode*)malloc(sizeof(BitNode));
		Node->data = a;
		Node->lchild = NULL;
		Node->rchild = NULL;
		bt->lchild = Node;
		createBiTree(bt->lchild);
	}
	printf("请输入%d节点的右孩子(0为空)\n", bt->data);	//输入右子树
	scanf("%d", &a);
	if (a != 0)
	{
		Node = (BiTree)malloc(sizeof(BitNode));
		Node->data = a;
		Node->lchild = NULL;
		Node->rchild = NULL;
		bt->rchild = Node;
		createBiTree(bt->rchild);
	}
	return 0;
}
void visit(BiTree T)
{
	printf("%d ", T->data);
}
void PreOrder(BitNode*T)	//前序遍历
{
	BitNode*temp = T;
	if (temp!= NULL)
	{
		visit(temp);
		PreOrder(temp->lchild);	//遍历左子树
		PreOrder(temp->rchild);	//遍历右子树
	}
}
void InOrder(BitNode*T)	//中序遍历
{
	BitNode*temp = T;
	if (temp != NULL) {
		InOrder(temp->lchild);
		visit(temp);
		InOrder(temp->rchild);
	}
}
void PostOrder(BitNode*T) {
	BitNode*temp = T;
	if (temp != NULL) {
		PostOrder(temp->lchild);
		PostOrder(temp->rchild);
		visit(temp);
	}
}

bool IsFind(BiTree T, int find) {	//判断某个结点是否在树中
	if (T == NULL)
		return false;
	if (T->data == find)
		return true;
	if (IsFind(T->lchild, find))
		return true;
	return IsFind(T->rchild, find);
}
int sumNodeTree(BiTree T)	//统计二叉树中结点的个数
{
	BitNode*temp = T;
	int sum = 0;
	if (temp == NULL)
		return 0;
	else {
		sum = 1 + sumNodeTree(temp->lchild) + sumNodeTree(temp->rchild);
	}
	return sum;
}
int yeziNodeTree(BiTree T)	//求叶子结点的个数
{
	BitNode*temp = T;
	int sum = 0;
	if (temp == NULL)
		return 0;
	else if (temp->lchild == NULL&&temp->rchild == NULL)
		return 1;
	else
		sum = yeziNodeTree(temp->lchild) + yeziNodeTree(temp->rchild);
	return sum;
}
int treeDeep(BiTree T)	//树的深度
{
	BitNode*temp = T;
	int deep = 0;
	if (temp) {
		int leftDeep = treeDeep(temp->lchild);
		int rightDeep = treeDeep(temp->rchild);
		deep = leftDeep >= rightDeep ? leftDeep + 1 : rightDeep + 1;
	}
	return deep;
}
BiTree copyTree(BiTree T)	//复制二叉树
{
	BiTree newnode;
	if (!T) return NULL;
	else {
		newnode = (BiTree)malloc(sizeof(BitNode));
		newnode->data = T->data;
		newnode->lchild = copyTree(T->lchild);
		newnode->rchild = copyTree(T->rchild);
		return newnode;
	}
}
void menu()
{
	system("cls");
	printf("-----------------------------学生管理系统--------------------------------\n");
	printf("\n\t1.前序遍历    2.中序遍历    3.后序遍历\n");
	printf("\t4.查询结点    5.统计结点    6.叶子结点\n");
	printf("\t7.二叉深度    8.复制二叉    0.结束操作\n");
	printf("\n-------------------------------------------------------------------------\n");
	printf("\n请选择操作(0-8):\n");
}
void ShowMenu(BiTree T)
{
	BitNode*copy;
	int find;
	bool judge;
	int choose;
	menu();
	scanf("%d", &choose);
	system("cls");
	switch (choose)
	{
	case 1:printf("先序遍历得到的节点序列为:\t");
		PreOrder(T);
		break;
	case 2:printf("中序遍历得到的节点序列为:\t");
		InOrder(T);
		break;
	case 3:printf("后序遍历得到的节点序列为:\t");
		PostOrder(T);
		break;
	case 4:printf("请输入你要查询的结点:\t");
		scanf("%d", &find);
		judge = IsFind(T, find);
		if (judge)
			printf("%d结点在树中.\n", find);
		else
			printf("%d结点不在树中.\n", find);
		break;
	case 5:find = sumNodeTree(T);
		printf("结点的总个数为:\t%d", find);
		break;
	case 6:find = yeziNodeTree(T);
		printf("叶子结点的个数为:\t%d",find);
		break;
	case 7:find = treeDeep(T);
		printf("二叉树的深度为:\t%d", find);
		break;
	case 8:copy = copyTree(T);
		printf("先序遍历输出copy过得二叉树:\t");
		PreOrder(copy);
		break;
	case 0:exit(0);
	default :
		printf("输入错误,按回车继续:");
	}
	printf("\n");
	system("pause");
}

int main()
{
	BitNode*T;
	T = Init_BTNode();
	createBiTree(T);
	while (1) {
		ShowMenu(T);
	}
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值