AVL树理解

简介

我们知道,AVL树也是平衡树中的一种,是自带平衡条件的二叉树,始终都在维护树的高度,保持着树的高度为logN,同时把插入、查找、删除一个结点的时间复杂度的最好和最坏情况都维持在O(logN);增加和删除需要通过一次或者多次的树旋转来重新平衡这棵树。

其中规定每个结点的左子树和右子树的高度最多差1,也就是说任何结点的两个子树的最大高度为1。

性质

平衡树最重要的地方在于旋转,其中分为单旋转和双旋转,其中单旋转和双旋转又各分为两种子形式,分别为左单旋转,右单旋转,左右双旋转,右左双旋转;在左左和右右这样的单旋转的情况下,只需要进行一次旋转操作,但是在左右或者右左这样的双旋转的情况下,则需要进行二次旋转操作。

如图:

  • 1,4为单旋转,1为左左,4为右右
  • 2,3为双旋转,2为左右,3为右左

再看下面一张图:

  • 左右(父亲结点在左边,子结点在右边):子结点先要经过一次左旋,变成左左的情况,再对父亲结点进行一次右旋,即可达到平衡;
  • 右左(父亲结点在右边,子结点在左边):同理,先对子结点进行一次右旋,变成右右的情况,再对父亲结点进行一次左旋,即可达到平衡。

单旋转

这是一个左左的情况,因为是对称的,所以两者可以通过左右旋转相互到达;

可以发现的是,k2为失去平衡的树的根结点,k1为旋转后重新平衡的树的根结点,k1结点的右结点经过旋转连接到了k2的左结点上,此时,k2的左结点连接到k1的右结点,而k2则连到k1的右结点上面;

双旋转

上面是左右的情况,但是右左的情况虽然不是对称的, 但是情况是类似的;

我们先看子结点的部分,也就是以k1为父结点的部分,此时k1为失去平衡的树的根结点,k2为进行旋转以后平衡树的根结点,经过左旋转以后变成了以k2为父结点,k1为左子结点的部分,此时树变成了k3为失去平衡的树根结点,k2变成经过旋转以后平衡树的根结点,所以在旋转时,可以把k1看作是一个整体,把k2的右子结点连接到k3的左子结点上面,再将k3当作一个整体连接到k2的右子结点上面。

实现

TreeNode.hpp

//
// TreeNode.hpp
// AVL
//
// Created by George on 16/10/7.
// Copyright © 2016年 George. All rights reserved.
// #ifndef TreeNode_hpp
#define TreeNode_hpp namespace AVL { template<class T>
class TreeNode {
public:
TreeNode(T value, int height = 0) : value(value), height(height), left(nullptr), right(nullptr){};
T value;
int height;
TreeNode* left;
TreeNode* right;
}; template<class T>
class AVLTree {
public:
AVLTree() = default;
AVLTree(TreeNode<T>* ptr) : _root(ptr){};
~AVLTree();
void Insert(T value);
TreeNode<T>* find(T value);
void Delete(T value);
void Traversal(); private:
TreeNode<T> * _root;
void insertNode(TreeNode<T>* &root, T value);
TreeNode<T>* find(TreeNode<T>* root, T value);
void deleteNode(TreeNode<T>* &root, T value);
void traversal(TreeNode<T>* root);
int height(TreeNode<T>* node) const; void singleRotateWithLeftChild(TreeNode<T>* &node);
void singleRotateWithRightChild(TreeNode<T>* &node);
void doubleRotateWithLeftRightChild(TreeNode<T>* &node);
void doubleRotateWithRightLeftChild(TreeNode<T>* &node);
}; void insertTest();
void deleteTest();
void searchTest();
void traversalTest();
} #endif /* TreeNode_hpp */

TreeNode.cpp

//
// TreeNode.cpp
// AVL
//
// Created by George on 16/10/7.
// Copyright © 2016年 George. All rights reserved.
// #include "TreeNode.hpp" #include <string>
#include <iostream> namespace AVL { template<class T>
void LogNode(TreeNode<T> *node) {
std::cout << "value :" << node->value << ", height :" << node->height << std::endl;
} std::string GetInputString(const std::string content) {
std::cout << content << " :";
std::string input;
std::cin >> input;
return input;
} /*------------------------------AVLTree-------------------------------*/
template<class T>
int AVLTree<T>::height(TreeNode<T> *node) const {
return node == nullptr ? -1 : node->height;
} template<class T>
void AVLTree<T>::singleRotateWithLeftChild(TreeNode<T>* &node) {
TreeNode<T>* lNode = node->left;
node->left = lNode->right;
lNode->right = node; node->height = std::max(height(node->left), height(node->right)) + 1;
lNode->height = std::max(height(lNode->left), height(lNode->right)) + 1; node = lNode;
} template<class T>
void AVLTree<T>::singleRotateWithRightChild(TreeNode<T>* &node) {
TreeNode<T>* rNode = node->right;
node->right = rNode->left;
rNode->left = node; node->height = std::max(height(node->left), height(node->right)) + 1;
rNode->height = std::max(height(rNode->left), height(rNode->right)) + 1; node = rNode;
} template<class T>
void AVLTree<T>::doubleRotateWithLeftRightChild(TreeNode<T>* &node) {
singleRotateWithRightChild(node->left);
singleRotateWithLeftChild(node);
} template<class T>
void AVLTree<T>::doubleRotateWithRightLeftChild(TreeNode<T>* &node) {
singleRotateWithLeftChild(node->right);
singleRotateWithRightChild(node);
} template<class T>
void AVLTree<T>::insertNode(TreeNode<T>* &root, T value) {
if (!root)
root = new TreeNode<T>(value); if (value < root->value) {
insertNode(root->left, value); if (height(root->left) - height(root->right) == 2) {
if (value < root->left->value)
singleRotateWithLeftChild(root);
else
doubleRotateWithLeftRightChild(root);
}
}
else if (value > root->value) {
insertNode(root->right, value); if (height(root->right) - height(root->left) == 2) {
if (value > root->right->value)
singleRotateWithRightChild(root);
else
doubleRotateWithRightLeftChild(root);
}
} root->height = std::max(height(root->left), height(root->right)) + 1;
} template<class T>
TreeNode<T>* AVLTree<T>::find(TreeNode<T>* root, T value) {
if (root == nullptr)
return nullptr; if (value < root->value)
return find(root->left, value);
else if (value > root->value)
return find(root->right, value);
else
return root; } template<class T>
void AVLTree<T>::deleteNode(TreeNode<T> *&root, T value) {
if (root == nullptr) return ; if (value < root->value) {
deleteNode(root->left, value); if (height(root->right) - height(root->left) == 2) {
// 判断是否存在右边子结点的左子结点
if (root->right->left && height(root->right->left) > height(root->right->right))
doubleRotateWithRightLeftChild(root);
else
singleRotateWithRightChild(root);
}
}
else if (value > root->value) {
deleteNode(root->right, value); if (height(root->left) - height(root->right) == 2) {
// 判断是否存在左边子结点的右子结点
if (root->left->right && height(root->left->right) > height(root->left->left))
doubleRotateWithLeftRightChild(root);
else
singleRotateWithLeftChild(root);
}
}
else {
// 存在两个子结点
if (root->left && root->right) {
TreeNode<T>* ptr = root->right; // 找到右子树中最小的结点
while (ptr->left != nullptr) {
ptr = ptr->left;
} // 替换值
root->value = ptr->value;
// 删除右子树中最小值的结点
deleteNode(root->right, ptr->value); if (height(root->left) - height(root->right) == 2) {
if (root->left->right && height(root->left->right) > height(root->left->left))
doubleRotateWithLeftRightChild(root);
else
singleRotateWithLeftChild(root);
}
}
else { // 存在一个结点或没有结点
TreeNode<T>* ptr = root;
if (root->left == nullptr)
root = root->right;
else if (root->right == nullptr)
root = root->left; delete ptr;
ptr = nullptr;
}
} if (root) {
root->height = std::max(height(root->left), height(root->right)) + 1;
}
} template<class T>
void AVLTree<T>::traversal(TreeNode<T>* root) {
if (!root) return;
traversal(root->left);
LogNode(root);
traversal(root->right);
} template<class T>
void AVLTree<T>::Insert(T value) {
insertNode(_root, value);
} template<class T>
TreeNode<T>* AVLTree<T>::find(T value) {
return find(_root, value);
} template<class T>
void AVLTree<T>::Delete(T value) {
deleteNode(_root, value);
} template<class T>
void AVLTree<T>::Traversal() {
traversal(_root);
} /*------------------------------Test-------------------------------*/
static AVLTree<int>* tree = new AVLTree<int>(); void insertTest() {
for (int i = 1; i <= 10 ; i++) {
tree->Insert(i);
}
} void deleteTest() {
std::string input = GetInputString("Please input the delete value");
int value = atoi(input.c_str());
tree->Delete(value);
} void searchTest() {
std::string input = GetInputString("Please input the search value");
int value = atoi(input.c_str());
TreeNode<int>* node = tree->find(value);
LogNode(node);
} void traversalTest() {
tree->Traversal();
} }

main.cpp

//
// main.cpp
// AVL
//
// Created by George on 16/10/7.
// Copyright © 2016年 George. All rights reserved.
// #include <iostream>
#include "TreeNode.hpp" int main(int argc, const char * argv[]) {
// insert code here... AVL::insertTest(); AVL::traversalTest(); AVL::deleteTest(); AVL::traversalTest(); AVL::searchTest(); return 0;
}

运行结果

应用

因为旋转消耗的时间较多,所以适用于插入删除次数较少的,比如windows对进程地址空间的管理就用到了AVL。

AVL树理解的更多相关文章

  1. AVL树的左旋右旋理解 (转)

    AVL树是最先发明的自平衡二叉查找树.在AVL树中任何节点的两个子树的高度最大差别为一,所以它也被称为高度平衡树.查找.插入和删除在平均和最坏情况下都是O(log n).增加和删除可能需要通过一次或多 ...

  2. AVL树的理解及自写AVL树

    AVL树是最先发明的自平衡二叉查找树.在AVL树中任何节点的两个子树的高度最大差别为一,所以它也被称为高度平衡树.查找.插入和删除在平均和最坏情况下都是O(log n).增加和删除可能需要通过一次或多 ...

  3. 以AVL树为例理解二叉树的旋转(Rotate)操作

    树旋转是在二叉树中的一种子树调整操作, 每一次旋转并不影响对该二叉树进行中序遍历的结果. 树旋转通常应用于需要调整树的局部平衡性的场合. 树旋转包括两个不同的方式, 分别是左旋转和右旋转. 两种旋转呈 ...

  4. 06-看图理解数据结构与算法系列(AVL树)

    AVL树 AVL树,也称平衡二叉搜索树,AVL是其发明者姓名简写.AVL树属于树的一种,而且它也是一棵二叉搜索树,不同的是他通过一定机制能保证二叉搜索树的平衡,平衡的二叉搜索树的查询效率更高. AVL ...

  5. 对于AVL树和红黑树的理解

    AVL又称(严格)高度平衡的二叉搜索树,也叫二叉查找树.平衡二叉树.window对进程地址空间的管理用到了AVL树. 红黑树是非严格平衡二叉树,统计性能要好于平衡二叉树.广泛的在C++的STL中,ma ...

  6. 深入理解索引和AVL树、B-树、B+树的关系

    目录 什么是索引 索引的分类 索引和AVL树.B-树.B+树的关系 AVL树.红黑树 B-树 B+树 SQL和NoSQL索引 什么是索引 索引时数据库的一种数据结构,数据库与索引的关系可以看作书籍和目 ...

  7. 算法与数据结构(十一) 平衡二叉树(AVL树)

    今天的博客是在上一篇博客的基础上进行的延伸.上一篇博客我们主要聊了二叉排序树,详情请戳<二叉排序树的查找.插入与删除>.本篇博客我们就在二叉排序树的基础上来聊聊平衡二叉树,也叫AVL树,A ...

  8. 数据结构图文解析之:AVL树详解及C++模板实现

    0. 数据结构图文解析系列 数据结构系列文章 数据结构图文解析之:数组.单链表.双链表介绍及C++模板实现 数据结构图文解析之:栈的简介及C++模板实现 数据结构图文解析之:队列详解与C++模板实现 ...

  9. PAT树_层序遍历叶节点、中序建树后序输出、AVL树的根、二叉树路径存在性判定、奇妙的完全二叉搜索树、最小堆路径、文件路由

    03-树1. List Leaves (25) Given a tree, you are supposed to list all the leaves in the order of top do ...

随机推荐

  1. pytorch--cnn的理解

    class Model(nn.Module): def __init__(self): super(Model, self).__init__() self.conv1 = nn.Conv2d(1, ...

  2. MVVM模式的 数据绑定

    数据绑定要达到的效果 数据绑定要达到什么效果呢,就是在界面中绑定了数据源之后,数据在界面上的修改能反映到绑定源,同时绑定源的修改也能反映到界面上.从界面反映到绑定的数据源是很容易理解的,因为在绑定过程 ...

  3. python3 切换工作文件夹

    python3 默认的工作文件夹在Python安装路径下.如下为查看工作文件夹路径: >>> import os >>> os.getcwd() 'D:\\Work ...

  4. 公司xenserver搭建的使用

    [root@xenserver ~]# ls -l /dev/disk/by-path/total 0lrwxrwxrwx 1 root root  9 Jan 19 16:33 pci-0000:0 ...

  5. python基础-类的继承

    继承:承创建的新类称为“子类”或“派生类”,被继承的类称为“基类”.“父类. 继承的过程,就是从一般到特殊的过程.要实现继承,可以通过“继承”(Inheritance)和“组合”(Compositio ...

  6. windows 下的一些常用命令提示符

    windows下dos命令窗口输入 netstat -ano即可查看端口使用情况, 如果要查看指定端口是否被占用 使用命令netstat -ano|findstr 端口号, 例如要查看8080端口号是 ...

  7. thinkphp5高亮当前页(仅针对个人项目记录,不做通用参考)

    <div class="navbg"> <ul class="menu"> <li> <a href="/& ...

  8. 使用Appium 测试微信小程序和微信公众号方法

    由于腾讯系QQ.微信等都是基于腾讯自研X5内核,不是google原生webview,需要打开TBS内核Inspector调试功能才能用Chrome浏览器查看页面元素,并实现Appium自动化测试微信小 ...

  9. gcc中C++一个特别的头文件

    今天在一段程序中看到这样一个很奇怪的语句: #include<bits/stdc++.h> 博主查了之后发现业界称其万能头文件,这个头文件包含了如下等头文件,几乎包含了所有的C++标准库头 ...

  10. 解决loadrunner 脚本和replaylog中的中文乱码问题

    解决loadrunner 脚本和replaylog中的中文乱码问题 解决这个问题必须认识到一个事实就是,loadrunner和测试服务器交换数据使用的是utf8格式,但是展现在replaylog中是使 ...