//头文件BTREE.h
#ifndef _BTREE_H_
#define _BTREE_H_
#define true 1
#define false 0
#define BRIGHT 1
#define BLEFT 0
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 *create_btree();//创建一个二杈树
//二杈树的插入
int insert_Btree(BTree *btree,BTreeData data,int pos,int count,int flag);
//树的打印
void Display(BTree *btree);
//删除结点
int Delete(BTree *btree, int pos, int count);
//树的高度
int BTree_Height(BTree *btree);
//树的度
int BTree_Degree(BTree *btree);
//结点的清除
int BTree_Clear(BTree *btree);
//树的销毁
int Destroy(BTree **btree);
//前序遍历
void PRE (BTreeNode *node);
//中序遍历
void MID (BTreeNode *node);
//后序遍历
void LAST (BTreeNode *node);
#endif
//函数源代码BTREE.c
#include "BTree.h"
#include <stdlib.h>
#include <stdio.h>
BTree *create_btree()
{
BTree *btree = (BTree *)malloc(sizeof(BTree *) / sizeof(char));
if(btree == NULL)
{
return NULL;
}
btree->count = 0;
btree->root = NULL;
return btree;
}
//二杈树的插入
int insert_Btree(BTree *btree,BTreeData data,int pos,int count,int flag)
{
if(btree == NULL || (flag != BLEFT && flag != BRIGHT))
{
return false;
}
BTreeNode *node = (BTreeNode *)malloc(sizeof(BTreeNode) / sizeof(char));
if(node == NULL)
{
return false;
}
node->data = data;
node->Lchild = NULL;
node->Rchild = NULL;
BTreeNode *parent = NULL; //指向父节点的指针
BTreeNode *current = btree->root; //current为指向根节点的指针
int way;
while(count > 0 && current != NULL)
{
way = pos & 1;//当前走的方向
pos = pos >> 1;//下一步走的方向
parent = current;
if(way == BLEFT)
{
current = current->Lchild;
}
else
{
current = current->Rchild;
}
count--;
}
if(flag == BLEFT)//把被替换掉的结点插入到新结点下面(左/右)
{
node->Lchild = current;
}
else
{
node->Rchild = current;
}
//把新结点插入到二杈树中
if(parent != NULL)
{
if(way == BLEFT)
{
parent->Lchild = node;
}
else
{
parent->Rchild = node;
}
}
else
{
btree->root = node;
}
return true;
}
//递归删除
void R_Delete(BTree *btree,BTreeNode *node)
{
if(node == NULL || btree ==NULL)
return;
R_Delete(btree,node->Lchild);
R_Delete(btree,node->Rchild);
free(node);
btree->count--;
}
//删除结点
int Delete(BTree *btree, int pos, int count)
{
if(btree == NULL)
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 == BLEFT)
{
current = current->Lchild;
}
else
{
current = current->Rchild;
}
count--;
}
if(parent != NULL)
{
if(way == BLEFT)
{
parent->Lchild = NULL;
}
else
{
parent->Rchild = NULL;
}
}
else
{
btree->root = NULL;
}
R_Delete(btree,current );
return true;
}
//递归求高度
int R_Height(BTreeNode *node)
{
if(node == NULL)
return false;
int L_H = R_Height(node->Lchild);
int R_H = R_Height(node->Rchild);
return (L_H > R_H ? L_H+1 : R_H+1);
}
//树的高度
int BTree_Height(BTree *btree)
{
if(btree == NULL)
return false;
int ret = R_Height(btree->root);
return ret;
}
//递归求树的度
int R_Degree(BTreeNode *node)
{
if(node == NULL)
return 0;
int degree = 0;
if(node->Lchild != NULL)
degree++;
if(node->Rchild != NULL)
degree++;
if(degree == 1)
{
int L_D = R_Degree(node->Lchild);
if(L_D == 2)
return 2;
int R_D = R_Degree(node->Rchild);
if(R_D == 2)
return 2;
}
return degree;
}
//树的度
int BTree_Degree(BTree *btree)
{
if(btree == NULL)
{
return false;
}
int R_D = R_Degree(btree->root);
return R_D;
}
//结点的清除
int BTree_Clear(BTree *btree)
{
if(btree == NULL)
{
return false;
}
Delete(btree,0,0);
btree->root = NULL;
return true;
}
//树的销毁
int Destroy(BTree **btree)
{
if(btree == NULL)
return false;
BTree_Clear(*btree);
free(*btree);
*btree = NULL;
return true;
}
//前序遍历
void PRE (BTreeNode *node)
{
if(node == NULL)
return;
printf("%c ",node->data);
PRE (node->Lchild);
PRE (node->Rchild);
return;
}
//中序遍历
void MID (BTreeNode *node)
{
if(node == NULL)
return;
MID (node->Lchild);
printf("%c ",node->data);
MID (node->Rchild);
return;
}
//后序遍历
void LAST (BTreeNode *node)
{
if(node == NULL)
return;
LAST (node->Lchild);
LAST (node->Rchild);
printf("%c ",node->data);
return;
}
//递归打印树
void R_Display(BTreeNode *node, int gap)
{
int i;
if(node == NULL)
{
for(i = 0;i < gap;i++)
{
printf("-");
}
printf("\n");
return;
}
for(i = 0;i < gap;i++)
{
printf("-");
}
printf("%c\n",node->data);
if(node->Lchild != NULL || node->Rchild != NULL )
{
R_Display(node->Lchild, gap+4);
R_Display(node->Rchild, gap+4);
}
}
void Display(BTree *btree)
{
if(btree == NULL)
return ;
R_Display(btree->root,0);
}
//主函数main.c
#include <stdio.h>
#include "BTree.h"
int main()
{
BTree *btree = create_btree();
if(btree == NULL)
{
printf("创建失败\n");
}
else
{
printf("创建成功\n");
}
insert_Btree(btree,'a',0,0,0);
insert_Btree(btree,'b',0,1,0);
insert_Btree(btree,'f',1,1,0);
insert_Btree(btree,'c',0,2,0);
insert_Btree(btree,'d',0,2,0);
insert_Btree(btree,'e',4,3,0);
insert_Btree(btree,'g',2,2,0);
insert_Btree(btree,'h',12,5,0);
Display(btree);
printf("删除后*****************\n");
Delete(btree,1,1);
printf("树的高度:");
printf("%d\n",BTree_Height(btree));
printf("树的度:");
printf("%d\n",BTree_Degree(btree));
BTree_Clear(btree);
Destroy(&btree);
Display(btree);
printf("前序遍历: \n");
PRE(btree->root);
printf("\n");
printf("中序遍历: \n");
MID(btree->root);
printf("\n");
printf("后序遍历: \n");
LAST(btree->root);
printf("\n");
return 0;
}
数据结构之二杈树的实现
最新推荐文章于 2024-11-02 20:41:42 发布