算法与数据结构随笔-生活工作点滴

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

2019-07-10  本文已影响1人  秦老厮

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

1.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);
}

2.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);

3.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");

4.执行结果


执行结果 本人微信公众号,点关注,不迷路
上一篇下一篇

猜你喜欢

热点阅读