【数据结构】【二叉排序树BST】【代码实现】

二叉排序树的基本概念

二叉排序树(二叉搜索树、二叉查找树、BST Binary Sort Tree)一非空的BST具有以下性质:

  1. 如果左子树不空,则左子树上所有结点的值都小于根结点的值;
  2. 如果右子树不空,则右子树上所有结点的值都大与根结点的值;
  3. 左右子树也是二叉排序树;

在这里插入图片描述

二叉排序树的创建

左子树 < 根 < 右子树

  1. 相同序列创建的二叉排序树是惟一的;
  2. 同一集合创建的二叉排序树是不同的;
  3. 用二叉树的先序遍历序列创建的二叉树和原二叉树相同;

删除二叉排序树的节点

  1. 如果树只有根结点,并且删除的结点就是根结点。
  2. 如果待删除的结点是叶结点,直接删除,不会破坏二叉排序树的性质。
  3. 如果待删除的结点只有左子树或右子树,则让子树代替自己。
  4. 如果待删除的结点有左子树或右子树,让左子树最右侧结点代替自己,然后删除左子树最右侧结点。(也可让右子树最左侧的结点代替自己,让后删除右子树最左侧结点)。

二叉排序树的增删查改【代码实现】

#include<stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
#define ElemType int
#define  KeyType int
/* 二叉排序树的节点结构定义 */
typedef struct BiTNode
{
    int data;
    struct BiTNode *lchild, *rchild;
} BiTNode, *BiTree;
//二叉排序树查找算法
int SearchBST(BiTree T, KeyType key, BiTree f, BiTree* p) {
  if(T == NULL ){
      *p = f;
      return FALSE;
  }
  if(T->data == key){
      *p = T;
      return TRUE;
  }
    return key < T->data ? SearchBST(T->lchild,key,T,p): SearchBST(T->rchild,key,T,p);
}
int InsertBST(BiTree *T, ElemType e) {
    BiTree p =NULL;
    if(!SearchBST(*T,e,NULL,&p)){
        BiTree newNode = (BiTree) malloc(sizeof (BiTree));
        newNode->data = e;
        newNode->lchild = newNode->rchild = NULL;
        if(p == NULL){ //p为NULL说明该树为空,这插入结点为根节点
            *T  = newNode;
        }else if( e < (p)->data){
            p->lchild= newNode;
        }else
            p->rchild = newNode;
        return TRUE;
    }
    return FALSE;
}
//删除函数
int Delete(BiTree *p)
{
    BiTree s , t;
    if((*p)->lchild ==NULL && (*p)->rchild ==NULL){
       //free(*p);
        *p = NULL;
    }
    else if((*p)->lchild == NULL){
        t = *p;
        (*p)= (*p)->rchild;
        free(t);
    }
    else if((*p)->rchild == NULL){
        t = *p;
        (*p)=(*p) ->lchild;
        free(t);
    }else {
        t = (*p);
        s = (*p)->lchild;
        while (s->rchild != NULL) {
            t = s;
            s = s->rchild;
        }
        (*p)->data = s->data;
        if (t == *p) {
            t->lchild = s->lchild;
        }else {
            t->rchild = s->lchild;
        }
        free(s);
    }
        return TRUE;
}
int DeleteBST(BiTree *T, int key)
{
    if (!(*T)) {//不存在关键字等于key的数据元素
        return FALSE;
    }
    else
    {
        if (key == (*T)->data) {
            Delete(T);
            return TRUE;
        }
        else if (key < (*T)->data) {
            //使用递归的方式
            return DeleteBST(&(*T)->lchild, key);
        }
        else {
            return DeleteBST(&(*T)->rchild, key);
        }
    }
}
void order(BiTree t)//中序输出
{
    if (t == NULL) {
        return;
    }
    order(t->lchild);
    printf("%d ", t->data);
    order(t->rchild);
}
int main()
{
    int i;
    int a[5] = { 3,4,2,5,9 };
    BiTree T = NULL;
    for (i = 0; i < 5; i++) {
        InsertBST(&T, a[i]);
    }
    printf("中序遍历二叉排序树:\n");
    order(T);
    printf("\n");
    printf("删除3后,中序遍历二叉排序树:\n");
    DeleteBST(&T, 3);
    order(T);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jie3606

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值