二叉树

1. 链式二叉树结构和操作定义

ChainBinTree.h

//功能:二叉树结构体
typedef struct ChainTree{
	DATA data;
	struct ChainTree *left;
	struct ChainTree *right;
}ChainBinTree;

/*---------------------二叉树所有操作原型声明 start ------------------------*/
//功能:初始化二叉树
ChainBinTree *BinTreeInit();
//功能:添加节点到二叉树
void AddNode(ChainBinTree *bt);
int BinTreeAddNode(ChainBinTree *bt, ChainBinTree *node, int n);
//功能:获取二叉树左右子树
ChainBinTree *BinTreeLeft(ChainBinTree *bt);
ChainBinTree *BinTreeRight(ChainBinTree *bt);
//功能:获取二叉树状态
int BinTreeIsEmpty(ChainBinTree *bt);
int BinTreeDepth(ChainBinTree *bt);
//功能:二叉树中查找数据
ChainBinTree *BinTreeFind(ChainBinTree *bt, DATA data);
//功能:清空二叉树
void BinTreeClear(ChainBinTree *bt);
//功能:遍历二叉树
//1)先序遍历
void BinTree_DLR(ChainBinTree *bt, void (*oper)(ChainBinTree * tmp));
//2)中序遍历
void BinTree_LDR(ChainBinTree *bt, void (*oper)(ChainBinTree * tmp));
//3)后序遍历
void BinTree_LRD(ChainBinTree *bt, void (*oper)(ChainBinTree * tmp));
//4)层次遍历
void BinTree_Level(ChainBinTree *bt, void (*oper)(ChainBinTree * tmp));
/*---------------------二叉树所有操作原型声明 end --------------------------*/

/*---------------------二叉树所有操作具体实现 start ------------------------*/
//功能:初始化二叉树
ChainBinTree *BinTreeInit(){
	ChainBinTree *root;
	if(root = (ChainBinTree *)malloc(sizeof(ChainBinTree))){
		printf("请输入根节点数据:");
		fflush(stdin);
		scanf("%s", &root->data);
		root->left = NULL;
		root->right = NULL;
		return root;
	}
	return NULL;
}
//功能:添加节点到二叉树
void AddNode(ChainBinTree *bt){
	ChainBinTree *node, *parent;
	DATA data;
	int select, select1;
	if(node=(ChainBinTree *)malloc(sizeof(ChainBinTree))){
		printf("请输入二叉树节点数据:");
		fflush(stdin);
		scanf("%s", &node->data);
		node->left = NULL;
		node->right = NULL;
		printf("请输入父节点数据:");
		fflush(stdin);
		scanf("%c", &data);
		parent = BinTreeFind(bt, data);
		if(!parent){
			printf("未找到父节点!!");
			free(node);
			return;
		}
		
		do{		
			printf("请选择:1.添加到左子树  2.添加到右子树\n");
			scanf("%d", &select1);
			if(select1==1 || select1==2)
				BinTreeAddNode(parent, node, select1);
		}while((select1!=1) && (select1!=2));
	}
	return;
}

int BinTreeAddNode(ChainBinTree *bt, ChainBinTree *node, int n){
	if(bt==NULL){
		printf("父节点不存在,请先设置父节点!!");
		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("参数错误!");
			return 0;
	}
	return 1;
}
//功能:获取二叉树左右子树
ChainBinTree *BinTreeLeft(ChainBinTree *bt){
	if(bt)
		return bt->left;
	else
		return NULL;
}
ChainBinTree *BinTreeRight(ChainBinTree *bt){
	if(bt)
		return bt->right;
	else
		return NULL;
}
//功能:获取二叉树状态
//功能:判断二叉树是否为空
int BinTreeIsEmpty(ChainBinTree *bt){
	if(bt)
		return 0;
	else
		return 1;
}
//功能:求二叉树的深度
int BinTreeDepth(ChainBinTree *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);
	}
}
//功能:二叉树中查找数据
ChainBinTree *BinTreeFind(ChainBinTree *bt, DATA data){
	ChainBinTree *tmp;
	if(bt==NULL)
		return NULL;
	else{
		if(bt->data == data)
			return bt;
		else{
			if(tmp=BinTreeFind(bt->left, data))
				return tmp;
			else if(tmp=BinTreeFind(bt->right, data))
				return tmp;
			else
				return NULL;
		}
	}
}
//功能:销毁二叉树
void BinTreeDestory(ChainBinTree *bt){
	if(bt){
		BinTreeClear(bt->left);
		BinTreeClear(bt->right);
		free(bt);
		bt=NULL;
	}
	return ;
}

//功能:清空二叉树
void BinTreeClear(ChainBinTree *bt){
	if(bt){
		bt->left=NULL;
		bt->right=NULL;
	}
	return ;
}

//功能:遍历二叉树
//1)先序遍历
void BinTree_DLR(ChainBinTree *bt, void (*oper)(ChainBinTree * tmp)){
	if(bt){
		oper(bt);
		BinTree_DLR(bt->left, oper);
		BinTree_DLR(bt->right, oper);
	}	
	return;
}
//2)中序遍历
void BinTree_LDR(ChainBinTree *bt, void (*oper)(ChainBinTree * tmp)){
	if(bt){
		BinTree_DLR(bt->left, oper);
		oper(bt);
		BinTree_DLR(bt->right, oper);
	}
	return;
}
//3)后序遍历
void BinTree_LRD(ChainBinTree *bt, void (*oper)(ChainBinTree * tmp)){
	if(bt){
		BinTree_DLR(bt->left, oper);
		BinTree_DLR(bt->right, oper);
		oper(bt);
	}
	return;
}
//4)层次遍历
void BinTree_Level(ChainBinTree *bt, void (*oper)(ChainBinTree * tmp)){
	ChainBinTree *tmp;
	ChainBinTree *q[QUEUE_MAXSIZE];
	int head=0, tail=0;
	if(bt){
		tail = (tail + 1)%QUEUE_MAXSIZE;
		q[tail] = bt;
	}
	while(head != tail){
		head = (head+1)%QUEUE_MAXSIZE;
		tmp = q[head];
		oper(tmp);
		if((tmp->left)!=NULL){
			tail = (tail+1)%QUEUE_MAXSIZE;
			q[tail] = tmp->left;
		}
		if((tmp->right)!=NULL){
			tail = (tail+1)%QUEUE_MAXSIZE;
			q[tail] = tmp->right;
		}
	}
	return ;
}
/*---------------------二叉树所有操作具体实现 end ------------------------*/

2.二叉树操作测试

ChainBinTreeText.cpp

#include<stdio.h>
#include<stdlib.h>

typedef  char DATA;
#define QUEUE_MAXSIZE 50		//层次遍历时用到

#include"ChainBinTree.h"

//功能:操作二叉树的函数
void oper(ChainBinTree *tmp){
	printf("%c ", tmp->data);
	return;
}

int main(){
	int select, len, isTrue;
	ChainBinTree *Root, *node, *parent, *child;
	DATA data;
	do{
		printf("\n---------------------------\n");
		printf("1.初始化二叉树                  2.添加节点到二叉树中\n");
		printf("3.获取二叉树的左子树            4.获取二叉树的右子树\n");
		printf("5.查找指定数据节点              6.判断二叉树是否为空\n");
		printf("7.求二叉树的深度                8.销毁二叉树(一旦销毁必须重新建立二叉树才能使用其他操作)\n");
		printf("9.清空二叉树                    10.先序遍历二叉树\n");
		printf("11.中序遍历二叉树               12.后序遍历二叉树\n");
		printf("13.层次遍历二叉树\n");
		printf("0.退出\n");
		printf("请选择执行的操作序号:");
		fflush(stdin);
		scanf("%d", &select);
		switch(select){
			case 1:
				Root = BinTreeInit();
				break;
			case 2:
				AddNode(Root);
				break;
			case 3:
				printf("请输入父节点数据:");
				fflush(stdin);
				scanf("%s", &data);
				parent = BinTreeFind(Root, data);
				if(parent){
					child = BinTreeLeft(parent);
					if(child)
						printf("父节点%c的左子树为:%c\n", parent->data, child->data);
					else
						printf("父节点%c不存在左子树!\n", parent->data);
				}
				else
					printf("父节点%c不存在!\n", parent->data);
				break;
			case 4:
				printf("请输入父节点数据:");
				fflush(stdin);
				scanf("%s", &data);
				parent = BinTreeFind(Root, data);
				if(parent){
					child = BinTreeRight(parent);
					if(child)
						printf("父节点%c的右子树为:%c\n", parent->data, child->data);
					else
						printf("父节点%c不存在右子树!\n", parent->data);
				}
				else
					printf("父节点%c不存在!\n", parent->data);			
				break;
			case 5:
				printf("请输入要查找节点数据:");
				fflush(stdin);
				scanf("%s", &data);
				node = BinTreeFind(Root, data);
				printf("你查找的节点:%c存在!!\n ", node->data);
				break;
			case 6:
				isTrue = BinTreeIsEmpty(Root);
				if(isTrue)
					printf("二叉树为空\n");
				else
					printf("二叉树不空\n");
				break;
			case 7:
				len = BinTreeDepth(Root);
				printf("二叉树的深度为:%d\n", len);
				break;
			case 8:
				BinTreeDestory(Root);
				printf("二叉树已被销毁,不能再使用!!");
				break;
			case 9:
				BinTreeClear(Root);
				break;
			case 10:
				BinTree_DLR(Root, oper);
				break;
			case 11:
				BinTree_LDR(Root, oper);
				break;
			case 12:
				BinTree_LRD(Root, oper);
				break;
			case 13:
				BinTree_Level(Root, oper);
				break;
		}
	}while(select != 0);
	system("pause");
	return 1;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值