• C++ BinarySercahTree recursion version


    目录

    FindR()

    InsertR()

    push​编辑

    链接


    for循环版本的:C++ BinarySercahTree for version-CSDN博客

    Inorder()在c++ BinarySerschTree for verison写了。

    还是按照那种嵌套的方式来写递归。

    现在来写查找

    FindR()

    1. bool FindR()
    2. {
    3. return _FindR(_root);
    4. }

    然后_FindR()函数写递归具体实现:

    假设要找13,就让13和root比,key大root就往右,key小就往左,找到空就不找了,找到了

    1. bool _FindR(Node* root,const K& key)
    2. {
    3. while(cur)
    4. {
    5. Node* cur = root;
    6. if (root == nullptr)return false;
    7. if (key > cur->_key) return _FindR(cur->_right, key);
    8. else if (key < cur->_key) return _FindR(cur->_left, key);
    9. return true;
    10. }
    11. }

    假设要找12,那找到空都找不到,那就返回false

    1. bool _FindR(Node* root,const K& key)
    2. {
    3. while (cur)
    4. {
    5. Node* cur = root;
    6. if (root == nullptr)return false;
    7. if (key > cur->_key) return _FindR(cur->_right, key);
    8. else if (key < cur->_key) return _FindR(cur->_left, key);
    9. return true;
    10. return true
    11. }
    12. return false;
    13. }

    InsertR()

    1. bool InsertR()
    2. {
    3. return _Insert(root,key);
    4. }
    1. bool _InsertR(Node* _root,const K& key)
    2. {
    3. if (key > root->_key) return _InsertR(root->_right, key);
    4. else if (key < root->_key) return _InsertR(root->_left, key);
    5. else return false;//相等不让插入
    6. }

    push

    在root为空的时候插入

    	if (root == nullptr) { root = new Node(key); return true; }

    链接

    还可以用快慢指针的方式链接。

    也可以用下面这种方式:引用

    bool _InsertR(Node*& _root,const  K& key)

    画图解析:

    要插入9,root最后会走到空节点:

    root又是一个引用,是10节点的别名:

     root = new Node(key);

    把key值给给root就是给root->letf

    这样就链起来了:

    测试:

    EraserR

    基本架构

    1. bool EraserR(const K& key)
    2. {
    3. return _EraserR(_root, key);
    4. }
    1. bool _EraserR(Node* root,const K& key)
    2. {
    3. if (root == nullptr) return false;
    4. if (key > root->_key) return _EraserR(root->_right, key);
    5. else if (key < root->_key) return _EraserR(root->_left, key);
    6. else
    7. {
    8. //否则就是找到了
    9. //开始删除
    10. }
    11. }

    下面有3种情况要处理

    左为空,右为空,左右都不为空

    先看左为空的情况:

    假设我们要删除10,10比8小,root往右走,走到10,找到了,root->left==nullptr

    然后删除10,再把8和14链起来

    1. bool _EraserR(Node*& root,const K& key)
    2. {
    3. if (root == nullptr) return false;
    4. if (key > root->_key) return _EraserR(root->_right, key);
    5. else if (key < root->_key) return _EraserR(root->_left, key);
    6. else
    7. {
    8. //否则就是找到了
    9. //开始删除
    10. Node* del = root;
    11. if (root->_left == nullptr)root= root->_right;
    12. delete del;
    13. }
    14. }

    这里仍然用到了引用:

    &root=root->right

    例如10是8的别名:

    然后 root= root->_right;

    root是root->right的别名,也就是10,10的right是14,把14给给8:

    把3给给1,就是把14给给8,就把8和14链起来了。

    再把10给删掉:

     右边为空也一样:

    1. if (root->_right == nullptr)
    2. {
    3. root = root->_left;
    4. delete del;
    5. }

    如果要删除的节点为root,比如要删除8:

  • 相关阅读:
    统计假设检验
    【Laravel系列7.8】广播系统
    魔众智能AI系统v2.1.0版本支持主流大模型(讯飞星火、文心一言、通义千问、腾讯混元、Azure、MiniMax、Gemini)
    【云原生之Docker实战】使用Docker部署calibre-web个人图书管理平台
    Mendeley教程(1)中如何输出中文参考文献
    「问题解决」java web项目打成jar包运行后工具类无法读取模板文件的解决方法
    机器学习(22)---信息熵、纯度、条件熵、信息增益
    理解 JMeter 聚合报告(Aggregate Report)
    STM32 USB虚拟串口
    [技术发展-22]:网络与通信技术的应用与发展快速概览-2- 通信技术
  • 原文地址:https://blog.csdn.net/m0_65143871/article/details/134032788