数据结构:二叉树

头文件

#ifndef __BTREE_H__
#define __BTREE_H__ 

#define LCHILD 0
#define RCHILD 1

#include"error.h"

typedef char BTREEDATA;

typedef struct _btreenode
{
	BTREEDATA data;
	struct _btreenode *lchild;
	struct _btreenode *rchild;
}BTREENODE;

typedef struct _btree
{
	struct _btreenode *root;
	int count;
}BTREE;

//创建二叉树
BTREE * Creat_BTree();

//插入一个二叉树
//pos 用整数表示插入的路径
//count 表示行走路径的次数
//flag 表示被插入后子节点所在新节点的位置
int Insert_BTree(BTREE* btree,BTREEDATA data,int pos,int count,int flag);

//打印二叉树
int Risplay(BTREE* btree);

//删除二叉树一个节点
int Del_BTree(BTREE* btree,int pos,int count);

//求二叉树的高度
int BTree_Height(BTREE* btree);

//求二叉树的度
int BTree_degree(BTREE* btree);

//清空二叉树
int BTree_clear(BTREE* btree);

//删除二叉树
int BTree_Destroy(BTREE** btree);

//前序遍历二叉树
void pre_order (BTREENODE* node);

//中序遍历二叉树
void mid_order (BTREENODE* node);

//后序遍历二叉树
void last_order (BTREENODE* node);

#endif


主函数

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


//创建二叉树
BTREE* Creat_BTree()
{
	BTREE* btree = (BTREE*)malloc(sizeof(BTREE)/sizeof(char));
	if (btree == NULL)
	{
		errno = MALLOC_ERROR;
		printf("创建失败\n");
		return NULL;
	}
	else 
	{
		printf("创建成功\n");
	}	
	btree->root = NULL;
	btree->count = 0;
	
	return btree;
}

//插入一个二叉树
int Insert_BTree(BTREE* btree,BTREEDATA data,int pos,int count,int flag)
{
	//创建新节点
	BTREENODE * node = (BTREENODE *)malloc(sizeof(BTREENODE)/sizeof(char));
	if (node == NULL)
	{
		errno = MALLOC_ERROR;
		printf("创建失败\n");
		return FALSE;
	}
	node->data = data;
	node->lchild = NULL;
 	node->rchild = NULL;
	
	BTREENODE * parent = NULL;    		 //保存行走之后的父节点
	BTREENODE * current = btree->root;    //表示要插入的节点
	int way;
	while(count > 0 && current != NULL)    //找要插入的位置
	{
		way = pos & 1;
		pos = pos >> 1;
		parent = current;
		if (way == LCHILD)
			current = current->lchild;
		else 
			current = current->rchild;
		
		count--;
	}
	
	//决定后面的插在新节点的哪里
	if (flag == LCHILD)
		node->lchild = current;
	else 
		node->rchild = current;

	if(parent != NULL)
	{
		if (way == LCHILD)
			parent->lchild = node;
		else 
			parent->rchild = node;
	}
	else 
	{
		btree->root = node;
	}
	btree->count++;
	
	return TRUE;
}

//递归打印
void r_display(BTREENODE* node,int flag)
{
	int i;
	if(node == NULL)
	{
		for(i=0; i<flag; i++)
		{
			printf("-");
		}
		printf("\n");
		return;
	}
	
	for(i=0; i<flag; i++)
		{
			printf("-");
		}
		
	printf("%c\n",node->data);
	
	if(node->lchild == NULL && node->rchild == NULL)
		return ;
	r_display(node->lchild,flag+4);
	r_display(node->rchild,flag+4);
	
}

//打印二叉树
int Risplay(BTREE* btree)
{
	if(btree == NULL)
	{
		errno = ERROR;
		return FALSE;
	}
	r_display(btree->root,0);
}

//递归删除
void r_del_BTree(BTREE* btree,BTREENODE* node)
{
	if (btree == NULL || node == NULL)
		return ;
	
	r_del_BTree(btree,node->lchild);    //删除左子节点
	
	r_del_BTree(btree,node->rchild);    //删除右子节点
	
	free(node);
	
	btree->count--;
	
}

//删除二叉树一个节点
int Del_BTree(BTREE* btree,int pos,int count)
{
	if(btree == NULL)
	{
		errno = ERROR;
		return FALSE;
	}
	
	BTREENODE * parent = NULL;    		 //保存行走之后的父节点
	BTREENODE * current = btree->root;    //表示要插入的节点
	int way;
	while(count > 0 && current != NULL)    //找要插入的位置
	{
		way = pos & 1;
		pos = pos >> 1;
		parent = current;
		if (way == LCHILD)
			current = current->lchild;
		else 
			current = current->rchild;
		
		count--;
	}
	
	if(parent != NULL)                 //删除
	{
		if (way == LCHILD)
			parent->lchild = NULL;
		else 
			parent->rchild = NULL;
	}
	
	r_del_BTree(btree,current);        //调用递归函数,释放删除的空间
}

//递归求高度
int r_height(BTREENODE* node)
{
	if (node == NULL)
		return 0;
	
	int lh = r_height(node->lchild)+1;
	int rh = r_height(node->rchild)+1;
	
	return (lh>rh ? lh : rh);
}


//求二叉树的高度
int BTree_Height(BTREE* btree)
{
	if (btree == NULL)
	{
		errno = ERROR;
		return -1;
	}
	
	BTREENODE* current = btree->root;
	int height = r_height(current);
	
	return height;
}

int r_degree(BTREENODE* node)
{
	if (node == NULL)
		return 0;
	
	int degree = 0;
	if (node->lchild != NULL)
		degree++;
	if (node->rchild != NULL)
		degree++;
	
	//只要判断degree是1的情况
	if(degree == 1)
	{
		int ld =  r_degree(node->lchild);
		if (ld == 2)
			return 2;
		int rd =  r_degree(node->rchild);
		if (rd == 2)
			return 2;
	}
	
	return degree;
}

//求二叉树的度
int BTree_degree(BTREE* btree)
{
	if (btree == NULL)
	{
		errno = ERROR;
		return -1;
	}
	
	BTREENODE* current = btree->root;
	int degree = r_degree(current);
	
	return degree;
}

//清空二叉树
int BTree_clear(BTREE* btree)
{
	if (btree == NULL)
	{
		errno = ERROR;
		return FALSE;
	}
	
	if(btree->root == NULL)
		return TRUE;
	Del_BTree(btree,0,0);   //删除根节点
	btree->root = NULL;
	
	return TRUE;
}

//删除二叉树
int BTree_Destroy(BTREE** btree)
{
	if (btree == NULL  || *btree == NULL)
	{
		errno = ERROR;
		return FALSE;
	}
	
	BTree_clear(*btree);
	free(*btree);
	*btree = NULL;
	
	return TRUE;
}

//前序遍历二叉树
void pre_order (BTREENODE* node)
{
	if (node == NULL)
		return ;
	printf("%4c",node->data);
	pre_order (node->lchild);
	pre_order (node->rchild);
}

//中序遍历二叉树
void mid_order (BTREENODE* node)
{
	if (node == NULL)
		return ;
	pre_order (node->lchild);
	printf("%4c",node->data);
	pre_order (node->rchild);
}

//后序遍历二叉树
void last_order (BTREENODE* node)
{
	if (node == NULL)
		return ;
	pre_order (node->lchild);
	pre_order (node->rchild);
	printf("%4c",node->data);
}






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值