数据结构-二叉搜索树

1.二叉搜索树的基本操作——自己准备校招用的——不喜勿喷哦

本人将,重复的节点,只保存了数量。但是输出的时候却没有输出所有的节点,想要输出所有的节点,自己加个for循环即可哦

#include <iostream>
#include <queue>
using namespace std;
struct jian{
    int key;
    int num;
    jian *lc;
    jian *rc;
};
queue<jian*> q;
queue<jian*> p;
/**
* 创建树
*/
jian *create(jian *root,int x){
    if(root==NULL){
        root=new jian;
        root->key=x;
        root->num=1;
        root->lc=NULL;
        root->rc=NULL;
    }else{
        if(root->key==x){
            root->num++;
        }else if(root->key<x){
            root->rc=create(root->rc,x);
        }else{
            root->lc=create(root->lc,x);
        }
    }
    return root;
}
/**
* 前序遍历
*/
void xianxu(jian *root)
{
    if(root!=NULL){
        cout<<root->key<<" ";
        xianxu(root->lc);
        xianxu(root->rc);
    }
}
/**
*   中序遍历
*/
void zhongxu(jian *root)
{
    if(root!=NULL){

        zhongxu(root->lc);
        cout<<root->key<<" ";
        zhongxu(root->rc);
    }
}
/**
*   后序遍历
*/
void houxu(jian *root)
{
    if(root!=NULL){

        houxu(root->lc);
        houxu(root->rc);
        cout<<root->key<<" ";
    }
}
/**
*   层次遍历,两个队列,一个是按照层次取值,找出左右子树,放入队列,删掉队列首,
*   一个是,存放取出的节点
*/
void cengcibianli(jian *root){
    if(q.empty()){
        q.push(root);
    }

    while(!q.empty()){
        jian *tree=q.front();
        if(tree->lc!=NULL){
            q.push(tree->lc);
        }
        if(tree->rc!=NULL){
            q.push(tree->rc);
        }
        p.push(tree);
        q.pop();
    }

}
/**
*   在二叉搜索树中,最大的值一定是在左子树中的
*   当节点的左子树为空的话,那么这个点就是最小值的节点,自己画个图看看
*/
jian *findMin(jian *root){
    if(root->lc==NULL)
    {
        cout<<root->key<<endl;
        return root;
    }
    if(root->lc!=NULL){
        findMin(root->lc);
    }
}
/**
*在二叉搜索树中,最大的值一定是在右子树中的,
*当结节的右子树为空的话,那么这个节点就是最大值的节点
*/
jian *findMax(jian *root){
    if(root->rc==NULL)
    {
        cout<<"所在的树的最大值是:"<<root->key<<endl;
        return root;
    }
    if(root->rc!=NULL){
        findMax(root->rc);
    }
}
/**
* 删除最小的那个值
*/
jian *removeMin(jian *root){
    if(root==NULL){
        return NULL;
    }
    if(root->lc==NULL){
            /**
            * 如果左子树是空的那么,返回右子树的地址,纵然是null也无所谓
            */
        jian* rightRc=root->rc;
        //delete root;
        return rightRc;//返回的地址,被上一个节点连接
    }
    root->lc=removeMin(root->lc);
    return root;
}
jian *removeMax(jian *root){
    if(root==NULL){
        return NULL;
    }
    //删除最大的,肯定是在右子树旁边找,删除某一个最大值,只要再把子树移到上边就行
    //树的结构不会发生变化
    //当key所在的节点没有右孩子的时候,那么就可以,将左孩子一上去,
    //递归函数的返回值是非常重要的
    //删除最小值同理
    if(root->rc==NULL){
        jian *leftLc=root->lc;
        //delete root;
        return leftLc;
    }
    root->rc=removeMax(root->rc);
    return root;
}
jian* removeKey(jian *root,int key)
{
    if(root==NULL){
        cout<<"不存在该节点"<<endl;
        return NULL;
    }
    /**
    * 先找出对应key的节点的地址
    */
    if(root->key>key){
        root->lc=removeKey(root->lc,key);
        return root;
    }else if(root->key<key){
        root->rc=removeKey(root->rc,key);
        return root;
    }else{ 
        //key==root->key
        /**
        * 找到节点对应的地址
        *  1.如果左子树为空的树,说明是个最小值,删除节点,将子树移上去结构不变
        */
        if(root->lc==NULL){
            jian *rightRc=root->rc;
            delete root;
            return rightRc;
        }
         /**
        * 找到节点对应的地址
        *  1.如果右子树为空的树,说明是个最大值,删除节点,将子树移上去结构不变
        */
        if(root->rc==NULL){
            jian *leftLc=root->lc;
            delete root;
            return leftLc;
        }
        //左右子树都不为空的时候,
        /**
        *   左右子树都不为空的时候
        *   1.以当前节点的右子树的节点值为根,寻找右子树的最小值,作为替换,key所对应的节点
        *   2.返回的地址的右子树连接key对应节点的右子树删除最小值的新树
        *   3.左子树不变
        *   4其实寻找替换节点还可以用左子树的对应的最大值
        */
        jian *tree=findMin(root->rc);//寻找右子树中最小的值,当节点
        tree->rc=removeMin(root->rc);//
        tree->lc=root->lc;
        return tree;
    }
}
int main()
{
    int i;
    //int a[5]={5,2,4,8,6};
    int a[7]={10,7,4,9,12,13,11};
    jian *root;
    root=NULL;
    for(i=0;i<7;i++){
        root=create(root,a[i]);
    }
    //xianxu
    cout<<"前序遍历"<<endl;
    xianxu(root);
     cout<<endl;
   cout<<"中序遍历,其实就是从小到大排序!"<<endl;
    zhongxu(root);
     cout<<endl;
    cout<<"后序遍历"<<endl;
    houxu(root);
    cout<<endl;

    cout<<"层次遍历"<<endl;
    cengcibianli(root);
    while(!p.empty()){
        jian *tree=p.front();
        cout<<p.front()->key<<" ";
        p.pop();
    }
    cout<<endl;
    cout<<"删除最小值"<<endl;
    /*
    * 最小值是左子树中,最左边的值
    */
    findMin(root);
    cout<<"删除最大值"<<endl;
    /**
    *最大值是右子树中最右边的值
    */
    findMax(root);
    cout<<"删除二叉搜索树中的某个节点"<<endl;

    cout<<"先查找某一个节点"<<endl;
    //jian* tree;
    //tree=searchValue(2);
    cout<<"zainane"<<endl;
    int searchKey=12;
    root=removeKey(root,searchKey);

    cengcibianli(root);
    while(!p.empty()){
        jian *tree=p.front();
        cout<<p.front()->key<<" ";
        p.pop();
    }

}















/*queue<jian*> q;
queue<jian*> p;
jian* create(jian *root,int x){
    if(root==NULL){
            //这里创建的二叉搜索树,重复元素,用个数代替
        root=new jian;
        root->key=x;
        root->lc=NULL;
        root->rc=NULL;
        root=new jian;
        root->key=x;
        root->num=1;
        root->lc=NULL;
        root->rc=NULL;
    }

    else
    {
         if(x>root->key)
        {
            root->rc=create(root->rc,x);
        }
        else
        {
            root->lc=create(root->lc,x);
        }
        if(x>root->key){
            root->rc=create(root->rc,x);
        }else if(x<root->key){
            root->lc=create(root->lc,x);
        }else{
            root->num++;
        }

    }
    return root;
}
void bianli(jian *root){
    if(q.empty()){
        q.push(root);
    }
    jian *tree=q.front();
    if(tree->lc!=NULL){
        q.push(tree->lc);
    }
    if(tree->rc!=NULL){
        q.push(tree->rc);
    }
    p.push(tree);
    q.pop();
}

int main()
{
    jian* root;
    int x,i;
    int a[5]={5,4,6,2,8};
    for(i=0;i<5;i++)
    {
        root=create(root,a[i]);
    }


    //bfs层次遍历一下
   // bianli(root);
    while(!p.empty()){
        jian *tree=p.front();
        cout<<p.front()->key<<" "<<endl;
        p.pop();
    }
}*/

航拍图像多类别实例分割数据集 一、基础信息 • 数据集名称:航拍图像多类别实例分割数据集 • 图片数量: 训练集:1283张图片 验证集:416张图片 总计:1699张航拍图片 • 训练集:1283张图片 • 验证集:416张图片 • 总计:1699张航拍图片 • 分类类别: 桥梁(Bridge) 田径场(GroundTrackField) 港口(Harbor) 直升机(Helicopter) 大型车辆(LargeVehicle) 环岛(Roundabout) 小型车辆(SmallVehicle) 足球场(Soccerballfield) 游泳池(Swimmingpool) 棒球场(baseballdiamond) 篮球场(basketballcourt) 飞机(plane) 船只(ship) 储罐(storagetank) 网球场(tennis_court) • 桥梁(Bridge) • 田径场(GroundTrackField) • 港口(Harbor) • 直升机(Helicopter) • 大型车辆(LargeVehicle) • 环岛(Roundabout) • 小型车辆(SmallVehicle) • 足球场(Soccerballfield) • 游泳池(Swimmingpool) • 棒球场(baseballdiamond) • 篮球场(basketballcourt) • 飞机(plane) • 船只(ship) • 储罐(storagetank) • 网球场(tennis_court) • 标注格式:YOLO格式,包含实例分割的多边形坐标,适用于实例分割任务。 • 数据格式:航拍图像数据。 二、适用场景 • 航拍图像分析系统开发:数据集支持实例分割任务,帮助构建能够自动识别和分割航拍图像中各种物体的AI模型,用于地理信息系统、环境监测等。 • 城市
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值