IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C++实现AVL树(只有插入操作) -> 正文阅读

[C++知识库]C++实现AVL树(只有插入操作)

作者:recommend-item-box type_download clearfix
// 树节点
struct AVLNode {
  int depth;
  AVLNode* left;
  AVLNode* right;
  int val;

  AVLNode(int val=0) {
    depth = 0;
    left = right = nullptr;
    this->val = val;
  }
};
class AVLTree {
  typedef AVLNode Node;
  typedef AVLNode *PNode;

  const int DEPTH_DIFFERENCE_LIMIT = 1;

  public:
    /**
     * 递归插入
    */
    bool insert(const int val, PNode &n) {
      if (nullptr == n) {
        n = new Node(val);
        balance(n);
        return true;
      }
      else if (val < n->val) {
        bool isSucc = insert(val, n->left);
        balance(n);
        return isSucc;
      }
      else if (val > n->val) {
        bool isSucc = insert(val, n->right);
        balance(n);
        return isSucc;
      }
      else
        // 说明树中已经有这个节点了
        return false;
    }

    /**
     * 获取深度
     */
    int get_depth(PNode node) {
      if (node == nullptr) {
        return 0;
      }

      return node->depth;
    }

    /***
     * 更新深度
     */
    void update_depth(PNode node) {
      if (node == nullptr) {
        return;
      }

      update_depth(node->left);
      update_depth(node->right);

      int l_depth = get_depth(node->left);
      int r_depth = get_depth(node->right);
      node->depth = max(l_depth, r_depth) + 1;
    }

    /**
    * 获取平衡因子
    */
    int get_balance(PNode n) {
      if (n == nullptr) {
        return 0;
      }

      return get_depth(n->left) - get_depth(n->right);
    }

    /**
     * 左旋
     */ 
    void l_rotate(PNode &n) {
      auto r = n->right;
      n->right = r->left; // 右子树的左子树右挂
      r->left = n;

      // 更新高度
      // update_depth(n);
      // update_depth(r);

      n = r;
    }

    /**
     * 右旋
     */ 
    void r_rotate(PNode &n) {
      auto l = n->left;
      n->left = l->right; // 左子树的右子树左挂
      l->right = n;

      n = l;
    }

    /**
     * 右左双旋
     */ 
    void rl_rotate(PNode &n) {
      r_rotate(n->right);
      l_rotate(n);
    }

    /**
     * 左右双旋
     */ 
    void lr_rotate(PNode &n) {
      l_rotate(n->left);
      r_rotate(n->right);
    }

    /**
     * 通过旋转节点来平衡高度
     */ 
    void balance(PNode &n) {
      if (nullptr == n) {
        return;
      }

      if ((get_depth(n->left) - get_depth(n->right)) > DEPTH_DIFFERENCE_LIMIT) { // 左子树高度失衡
        if (get_depth(n->left->left) >= get_depth(n->left->right)) {
          /**
           *      8           6
           *     /           / \
           *    6    =>     4   8
           *   /
           *  4          (右旋)
           * 
           */
          r_rotate(n);
        } else {
          /**
           *      8            8           7
           *     /            /           / \
           *    6     =>     7    =>     6   8
           *     \  (左旋)  /
           *      7        6      (右旋)
           * 
           */
          lr_rotate(n);
        }
      } else if ((get_depth(n->right) - get_depth(n->left)) > DEPTH_DIFFERENCE_LIMIT) { // 右子树失衡
        if (get_depth(n->right->right) >= get_depth(n->right->left)) {
          /**
           *    4                  6
           *     \                / \
           *      6     =>       4   8
           *       \
           *        8    (左旋)
           * 
           */
          l_rotate(n);
        } else {
          /**
           *    4             4              6
           *     \             \            / \
           *      8     =>      6    =>    4   8
           *     /    (右旋)     \     
           *    6                8     (左旋)
           * 
           */
          rl_rotate(n);
        }
      }

      // 更新高度
      update_depth(n);
    }

    /**
     * 获取根节点
     */
    PNode getRoot() {
      return root;
    }
  private:
    PNode root = nullptr;
};

节点高度更新的代码调用还有点问题

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-08-18 12:31:35  更:2021-08-18 12:33:31 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/20 13:41:28-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码