二叉搜索树 | 中序遍历 | 插入 | 删除 | 查找 | 输出

在这里插入图片描述

#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;

typedef struct bstnode{
    int key;
    struct bstnode *lChild,*rChild;         //递归定义结构体
}BSTnode;


typedef struct{                              //指向第一个结点的指针root
    BSTnode *root;
}BSTtree;

void Create(BSTtree *t){
    t->root = NULL;
}

BSTnode* CreateNode(int root){                //初始化
    BSTnode *p = (BSTnode *)malloc(sizeof(BSTnode));
    if(NULL == p)
        return NULL;
    p->key = root;
    p->lChild = p->rChild = NULL;
    return p;
}

int Insert(BSTtree *t, int x){                //往二叉搜索树中插入结点x
    BSTnode *p = t->root,*q,*r;
    while(p){
        q = p;
        if(x < p->key)
            p = p->lChild;
        else if (x > p->key)
            p = p->rChild;
        else
            return false;
    }
    r = CreateNode(x);
    if(!t->root)
        t->root = r;
    else if(x < q->key)
        q->lChild = r;
    else q->rChild = r;
    return true;

}

int Search(BSTtree *t, int x){              //搜索树中是否存在x结点
    BSTnode *p = t->root, *q;
    if(t->root){
        while(p){
            if(x < p->key)
                p = p->lChild;
            else if(x > p->key)
                p = p->rChild;
            else{
                cout<<"存在!"<<endl;
                return 1;
            }
        }
    }
    cout<<"不存在!"<<endl;
    return 0;
}

int Delete(BSTnode *t,int x){           //删除二叉搜索树中的x结点
    if(t==NULL){
        return 0;
    }
    BSTnode *p = t, *prev;
    while( p && p->key!=x ){
        prev = p;
        if( x < p->key )
            p = p->lChild;
        else
            p = p->rChild;
    }
    if(p == NULL)
        return 0;

    if(p->lChild && p->rChild){             //对于结点含两棵非空子树
        BSTnode *min = p->rChild;           //将p的左子树中最大元素移动到p 或 p的右子树最小元素移动到p
        prev = p;
        while(min->lChild){
            prev = min;
            min = min->lChild;
        }
        p->key = min->key;
        if(prev->rChild == min)
            prev->rChild = min->rChild;
        else
            prev->lChild = min->rChild;
        free(min);
        min = NULL;
    }
    else{                                   //对于结点只有一棵子树或者结点是叶子的情况
        if(prev->lChild == p)                //存在一棵非空子树,则将这棵非空子树的根作为所删除结点的孩子
            prev->lChild = p->lChild ? p->lChild:p->rChild;     //a?b:c   如果a为真,则表达式值为b,如果a为假,则表达式值为c
        else if(prev->rChild == p)
            prev->rChild = p->lChild ? p->lChild:p->rChild;
        free(p);                            //叶子结点直接删除
        p = NULL;
    }
    return 1;
}

void Print(BSTnode *t){                     //中序遍历输出二叉搜索树,形成递增有序序列
    if(t==NULL)
        return;
    Print(t->lChild);
    cout<<t->key<<" ";
    Print(t->rChild);
}

int main(){
    BSTtree T;
    int record[10];                         //记录下所有生成的随机数
    int i,random;
    Create(&T);

    srand(time(NULL));      //生成随机数,插入到二叉搜索树中
    for(i=0;i<10;i++){
        random = rand()%10+1;
        record[i] = random;
        Insert(&T,random);
    }

    cout<<"共生成随机数[ 若重复,则不添加到二叉搜索树中 ]:"<<endl;
    for(i=0;i<10;i++){
        cout<<record[i]<<" ";
    }
    cout<<endl<<"中序遍历该二叉搜索树:"<<endl;
    Print(T.root);
    cout<<endl<<"搜索一下该树中是否存在结点2!"<<endl;
    Search(&T,2);
    Delete(T.root,2);
    cout<<endl<<"尝试删除结点2后,对二叉搜索树进行中序遍历:"<<endl;
    Print(T.root);

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值