当前位置: 首页 > news >正文

AVL树的理解和实现[C++]

文章目录

  • AVL树
    • AVL树的规则或原理
  • AVL树的实现
    • 1.节点的定义
    • 2.功能和接口等的实现
      • 默认构造函数,析构函数
      • 拷贝构造函数
      • 插入
      • 搜索
      • 打印函数
      • 检查是否为平衡树,检查平衡因子
      • 旋转

AVL树

AVL树,全称Adelson-Velsky和Landis树,是一种自平衡的二叉搜索树。它于1962年由苏联科学家Adelson-Velsky和Landis首次提出。AVL树具有以下特点:树中任一节点的左右子树高度差不超过1,因此AVL树是一种严格平衡的二叉搜索树。在AVL树上进行查找、插入和删除操作的时间复杂度均为O(log n),大大提高了搜索效率。

AVL树的规则或原理

当向二叉搜索树中插入新结点后,如果能保证每个结点的左右子树高度之差的绝对值不超过1(需要对树中的结点进行调整),即可降低树的高度,从而减少平均搜索长度。

一棵AVL树或者是空树,或者是具有以下性质的二叉搜索树:

  • 它的左右子树都是AVL树
  • 左右子树高度之差(简称平衡因子)的绝对值不超过1(-1/0/1)
    在这里插入图片描述如果一棵二叉搜索树是高度平衡的,它就是AVL树。如果它有n个结点,其高度可保持在
    O ( l o g 2 n ) O(log_2 n) O(log2n),搜索时间复杂度O( l o g 2 n log_2 n log2n)。

AVL树的实现

1.节点的定义

首先,我们定义AVL树的节点结构

template<class K, class V>
struct AVLTreeNode
{pair<K, V> _kv;//值 AVLTreeNode<K, V>* _left;//该节点的左孩子AVLTreeNode<K, V>* _right;//该节点的右孩子AVLTreeNode<K, V>* _parent;//该节点的父节点int _bf; // balance factor(平衡因子)AVLTreeNode(const pair<K, V>& kv):_kv(kv), _left(nullptr), _right(nullptr), _parent(nullptr), _bf(0){}
};

2.功能和接口等的实现

默认构造函数,析构函数

template<class K, class V>
class AVLTree
{typedef AVLTreeNode<K, V> Node;
public://默认构造函数,用来初始化AVL树,将根节点置空来表示树是空的AVLTree() = default;//析构函数~AVLTree(){Destroy(_root);_root = nullptr;}
private:void Destroy(Node* root){if (root == nullptr)return;Destroy(root->_left);Destroy(root->_right);delete root;}
private:Node* _root = nullptr;
};

拷贝构造函数

template<class K, class V>
class AVLTree
{//拷贝构造AVLTree(const AVLTree<K, V>& t){_root = Copy(t._root);}private://用递归来进行赋值AVL树的节点Node* Copy(Node* root){if (root == nullptr)return nullptr;Node* newRoot = new Node(root->_key, root->_value);newRoot->_left = Copy(root->_left);newRoot->_right = Copy(root->_right);return newRoot;}private:Node* _root = nullptr;
};

插入

template<class K, class V>
class AVLTree
{	
private:bool Insert(const pair<K, V>& kv){if (_root == nullptr){_root = new Node(kv);return true;}Node* parent = nullptr;Node* cur = _root;while (cur){if (cur->_kv.first < kv.first){parent = cur;cur = cur->_right;}else if (cur->_kv.first > kv.first){parent = cur;cur = cur->_left;}else{return false;}}cur = new Node(kv);if (parent->_kv.first < kv.first){parent->_right = cur;}else{parent->_left = cur;}cur->_parent = parent;// 更新平衡因子while (parent){if (cur == parent->_left)parent->_bf--;elseparent->_bf++;if (parent->_bf == 0){break;}else if (parent->_bf == 1 || parent->_bf == -1){// 继续往上更新cur = parent;parent = parent->_parent;}else if (parent->_bf == 2 || parent->_bf == -2){// 不平衡了,旋转处理if (parent->_bf == 2 && cur->_bf == 1){RotateL(parent);}else if (parent->_bf == -2 && cur->_bf == -1){RotateR(parent);}else if (parent->_bf == 2 && cur->_bf == -1){RotateRL(parent);}else{RotateLR(parent);}break;}else{assert(false);}}return true;}private:Node* _root = nullptr;
};

搜索

template<class K, class V>
class AVLTree
{
private:Node* Find(const K& key){Node* cur = _root;while (cur){if (cur->_key < key){cur = cur->_right;}else if (cur->_key > key){cur = cur->_left;}else{return cur;}}return nullptr;}private:Node* _root = nullptr;
};

打印函数

template<class K, class V>
class AVLTree
{void InOrder(){_InOrder(_root);cout << endl;}	
private:void _InOrder(Node* root){if (root == nullptr){return;}_InOrder(root->_left);cout << root->_kv.first << ":" << root->_kv.second << endl;_InOrder(root->_right);}
private:Node* _root = nullptr;
};

检查是否为平衡树,检查平衡因子

template<class K, class V>
class AVLTree
{
bool IsBanlanceTree(){return _IsBanlanceTree(_root);}
private://计算平衡因子函数int _Height(Node* root){if (root == nullptr){return 0;}int leftHeight = _Height(root->_left);int rightHeight = _Height(root->_right);return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;}//判断是否为平衡树的函数bool _IsBanlanceTree(Node* root){//空树返回真if (nullptr == root){return true;}//计算root的平衡因子:即root左右子树高度差int leftHeight = _Height(root->_left);int rightHeight = _Height(root->_right);int diff = rightHeight - leftHeight;//如果计算出的平衡因子与root的平衡因子不相等//或,root平衡因子的绝对值超过一,则不是AVL树/*if (abs(diff) < 2 || root->_bf != diff){return false;}*/if (abs(diff) >= 2){cout << root->_kv.first << "高度差异常" << endl;return false;}if (root->_bf != diff){cout << root->_kv.first << "平衡因子异常" << endl;return false;}//root的左右都是AVL树那么概述一定是AVL树return _IsBanlanceTree(root->_left) && _IsBanlanceTree(root->_right);}
private:Node* _root = nullptr;
};

旋转

template<class K, class V>
class AVLTree
{void RotateL(Node* parent){Node* subR = parent->_right;Node* subRL = subR->_left;parent->_right = subRL;if (subRL)subRL->_parent = parent;Node* parentParent = parent->_parent;csubR->_left = parent;parent->_parent = subR;if (parentParent == nullptr){_root = subR;subR->_parent = nullptr;}else{if (parent == parentParent->_left){parentParent->_left = subR;}else{parentParent->_right = subR;}subR->_parent = parentParent;}parent->_bf = subR->_bf = 0;}void  RotateR(Node* parent){Node* subL = parent->_left;Node* subLR = subL->_right;parent->_left = subLR;if (subLR)subLR->_parent = parent;Node* parentParent = parent->_parent;subL->_right = parent;parent->_parent = subL;if (parentParent == nullptr){_root = subL;subL->_parent = nullptr;}else{if (parent == parentParent->_left){parentParent->_left = subL;}else{parentParent->_right = subL;}subL->_parent = parentParent;}parent->_bf = subL->_bf = 0;}void RotateRL(Node* parent){Node* subR = parent->_right;Node* subRL = subR->_left;int bf = subRL->_bf;RotateR(parent->_right);RotateL(parent);if (bf == 0){subR->_bf = 0;subRL->_bf = 0;parent->_bf = 0;}else if (bf == 1){subR->_bf = 0;subRL->_bf = 0;parent->_bf = -1;}else if (bf == -1){subR->_bf = 1;subRL->_bf = 0;parent->_bf = 0;}else{assert(false);}}void RotateLR(Node* parent){Node* subL = parent->_left;Node* subLR = subL->_right;int bf = subLR->_bf;RotateL(parent->_left);RotateR(parent);if (bf == 0){subL->_bf = 0;subLR->_bf = 0;parent->_bf = 0;}else if (bf == -1){subL->_bf = 0;subLR->_bf = 0;parent->_bf = 1;}}
private:Node* _root = nullptr;
};
http://www.lryc.cn/news/404734.html

相关文章:

  • 云计算遭遇的主要安全威胁
  • [MySQL]02 存储引擎与索引,锁机制,SQL优化
  • ld,GNU 链接器介绍以及命令行参数详解
  • [web]-反序列化-base64
  • 【医学影像】RK3588+FPGA:满足远程诊疗系统8K音视频编解码及高效传输需求
  • 昇思25天学习打卡营第16天|基于MindSpore通过GPT实现情感分类
  • 服务器借助笔记本热点WIFI上网
  • 开发实战中Git的常用操作
  • python调用chrome浏览器自动化如何选择元素
  • 深入理解JS中的排序
  • Kafka之存储设计
  • Python面试整理-Python中的函数定义和调用
  • HTTP协议、Wireshark抓包工具、json解析、天气爬虫
  • electron项目中实现视频下载保存到本地
  • 基于chrome插件的企业应用
  • unittest框架和pytest框架区别及示例
  • IDEA性能优化方法解决卡顿
  • Mysql集合转多行
  • MFC:只允许产生一个应用程序实例的具体实现
  • 深入理解TCP/IP协议中的三次握手
  • 【React】事件绑定、React组件、useState、基础样式
  • x264、x265、libaom 编码对比实验
  • c++网络编程实战——开发基于ftp协议的文件传输模块(二) 配置ftp服务与手动执行ftp命令
  • Sphinx 安装相关指令解释
  • npm下载包-更改默认缓存目录
  • PWM再理解(1)
  • CSPVD 智慧工地安全帽安全背心检测开发包
  • 给常用Docker命令起别名,提高效率
  • 基于深度学习的草莓成熟度实时检测系统(UI界面+YOLOv8/v7/v6/v5模型+完整代码与数据集)
  • 【devops】ttyd 一个web版本的shell工具 | web版本shell工具 | web shell