算法与数据结构系列之[二分搜索树-中]

上篇介绍了二分搜索树的概念和基本操作,这篇贴出二分搜索树C语言实现代码。

BinarySearchTree.c

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

typedef struct BinarySearchNode{
    int e;
    struct BinarySearchNode *left,*right;
}BinarySearchNode,*BinarySearchTree;

int size = 0;  //维护二分搜索树的元素个数

BinarySearchTree root;
//添加元素
BinarySearchTree add(BinarySearchTree bst,int e){
    if(!bst){
        size++;
        bst = malloc(sizeof(BinarySearchNode));
        bst->e = e;
        bst->left = bst->right = NULL;
        return bst;
    }

    if(e < bst->e)
       bst->left = add(bst->left,e);
    else if (e > bst->e)
       bst->right = add(bst->right,e);
    return bst;
}

void Insert(int e){
    root = add(root,e);
}
//二分搜索树的元素个数
int GetBSTSize(){
    return size;
}

//判断二分搜索树是否为空
int IsBSTEmpty(){
    return size == 0;
}

//二分搜索树的前序遍历
void PreOrderBST(BinarySearchTree bst){
    if(bst == NULL)
        return;
    printf("%d ",bst->e);
    PreOrderBST(bst->left);
    PreOrderBST(bst->right);
}

void PreSearch(){
    PreOrderBST(root);
}

//二分搜索树的中序遍历
void InOrderBST(BinarySearchTree bst){
    if(bst == NULL)
        return;
    InOrderBST(bst->left);
    printf("%d ",bst->e);
    InOrderBST(bst->right);
}

void InSearch(){
    InOrderBST(root);
}

//二分搜索树的后序遍历
void PostOrderdBST(BinarySearchTree bst){
    if(bst == NULL)
        return;
    PostOrderdBST(bst->left);
    PostOrderdBST(bst->right);
    printf("%d ",bst->e);
}

void PostSearch(){
    PostOrderdBST(root);
}

//二分搜索树的查找
int FindBST(BinarySearchTree bst,int e){
    if(bst == NULL)
        return 0;
    if(e == bst->e)
        return 1;
    else if(e < bst->e)
        return FindBST(bst->left,e);
    else
        return FindBST(bst->right,e);
}

int  Contains(int e){
    return FindBST(root,e);
}

//查找二分搜索树的最下元素
BinarySearchTree MinimumBST(BinarySearchTree bst){
    if(size == 0){
        printf("这是一棵空树");
        exit(0);
    }
    if(bst->left == NULL)
        return bst;
    return MinimumBST(bst->left);
}

int Minimum(){
    return MinimumBST(root)->e;
}

//查找二分搜索树的最大元素
BinarySearchTree MaxmumBST(BinarySearchTree bst){
    if(size == 0){
        printf("这是一棵空树");
        exit(0);
    }
    if(bst->right == NULL)
        return bst;
    return MaxmumBST(bst->right);
}

int Maxmum(){
    return MaxmumBST(root)->e;
}

//删除二分搜索树中的最小元素
BinarySearchTree RemoveMinBST(BinarySearchTree bst){
    if(bst->left == NULL){ //只有右子节点,没有左子节点的情况
        BinarySearchTree rightNode = bst->right;
        bst->right = NULL;
        size--;
        return rightNode;
    }
    bst->left =  RemoveMinBST(bst->left);
    return bst;
}

int RemoveMin(){
    int min = Minimum();
    root = RemoveMinBST(root);
    return min;
}

//删除二分搜索树的最大元素
BinarySearchTree RemoveMaxBST(BinarySearchTree bst){
    if(bst->right == NULL){ //只有左子节点没有右子节点的情况
        BinarySearchTree leftNode = bst->left;
        bst->left = NULL;
        size--;
        return leftNode;
    }

    bst->right = RemoveMaxBST(bst->right);
    return bst;
}

int RemoveMaxmum(){
    int max = Maxmum();
    root = RemoveMaxBST(root);
    return max;
}

//删除二分搜索树的任意元素
BinarySearchTree RemoveBST(BinarySearchTree bst,int e){
    if(bst == NULL)
        return NULL;
    if(e < bst->e){
        bst->left = RemoveBST(bst->left,e);
        return bst;
    }
    else if(e > bst->e){
        bst->right = RemoveBST(bst->right,e);
        return bst;
    }
    else{ //e == node.e
        //待删除节点的左子树为空,右子树不为空
        if(bst->left == NULL){ //只有右子节点,没有左子节点的情况
            BinarySearchTree rightNode = bst->right;
            bst->right = NULL;
            size--;
            return rightNode;
        }

        //待删除节点的右子树为空,左子树不为空
        if(bst->right == NULL){ //只有左子节点没有右子节点的情况
            BinarySearchTree leftNode = bst->left;
            bst->left = NULL;
            size--;
            return leftNode;
        }

        //待删除节点的左右子树都不为空
        BinarySearchTree successor = MinimumBST(bst->right); //successor  待删除节点的后继,也就是待删除节点右子树的最小节点
        successor->right = RemoveMinBST(bst->right);
        successor->left = bst->left;
        bst->right = bst->left = NULL;
        return successor;
    }
}

void Remove(int e){
   root =  RemoveBST(root,e);
}

BinarySearchTree.h

typedef struct BinarySearchNode{
    int e;
    int size;
    struct BinarySearchNode *left,*right;
}BinarySearchNode,*BinarySearchTree;
//添加元素
void add(BinarySearchTree *bst,int e);
void Insert(int e);
//二分搜索树的元素个数
int GetBSTSize();
//判断二分搜索树是否为空
int IsBSTEmpty();
//二分搜索树的前序遍历
void PreSearch();
//二分搜索树的中序遍历
void InSearch();
//二分搜索树的后序遍历
void PostSearch();
//二分搜索树的查找
int  Contains(int e);
//查找二分搜索树的最下元素
int Minimum();
//查找二分搜索树的最大元素
int Maxmum();
//删除二分搜索树中的最小元素
int RemoveMin();
//删除二分搜索树的最大元素
int RemoveMaxmum();
//删除二分搜索树的任意元素
void Remove(int e);

mian.c

int num[10] = {62,58,88,46,77,99,30,50,92,31};


for (int i = 0; i <10 ; ++i) {
  Insert(num[i]);
}
//前、中、后序遍历
PreSearch();
printf("\n");
InSearch();
printf("\n");
PostSearch();
printf("\n");
printf("%d",GetBSTSize());
printf("\n");
//查找30这个元素是否在二分搜索树中
printf("%d",Contains(30));
printf("\n");
//输出最小元素
printf("%d",Minimum());
printf("\n");
//输出最大元素
printf("%d",Maxmum());
printf("\n");
//删除最小元素
RemoveMin();
InSearch();
printf("\n");
//删除二分搜树的最大元素
RemoveMaxmum();
InSearch();
printf("\n");
//删除二分搜索树的任意元素
Remove(77);
InSearch();
printf("\n");

执行结果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值