理想平衡
左右子树高度一样
平衡因子:左数高度-右数高度
适度平衡
ACL树
左右子树高度差小于2
如何判断平衡:-1,0,1
ACL树的规定是:左右子树的
最小不平衡子树
当在ACL树中插入节点的时候,会破坏平衡,这个时候需要调整
调整类型大致分为三种:
LL RR LR RL
LL(左左型):
右旋
RR型(右右型):
左旋
LR型:
先左旋,再右旋
RL型:
先右旋,再左旋
INSERT:
空树状态下直接创建节点
数据已经存在,不做任何处理(不存在重复数据)
如果数据<根节点数据,插入左子树, 数据>根节点数据,插入右子树
如果插入数据导致不平衡,调整平衡
DELETE:
如果是空树,直接返回
如果找到了数据,判断当前节点的孩子,如果是但分支,直接抬上去,如果是双分支
递归查找
判断是否需要调整平衡
这里给出C++实现AVL树的代码:
#pragma once
template <typename T>
class AVL {
public:
AVL();
AVL(T tValue);
void Insert(T tValue);
void Delete(T tValue);
void Show();
private:
struct Node {
T data;
Node* lChild;
Node* rChild;
int Height;
Node(T value) : data(value), lChild(nullptr), rChild(nullptr), Height(1) {}
};
Node* m_TreeRoot;
int Height(Node* pNode);
int BalanceFactor(Node* pNode);
void UpdateHeight(Node* pNode);
Node* LLRotation(Node* pNode);
Node* RRRotation(Node* pNode);
Node* RLRotation(Node* pNode);
Node* LRRotation(Node* pNode);
Node* Insert(Node* pNode, T tValue);
Node* FindMin(Node* pNode);
Node* Delete(Node* pNode, T tValue);
void Show(Node* pNode);
};
template<typename T>
AVL<T>::AVL() : m_TreeRoot(nullptr) {}
template<typename T>
AVL<T>::AVL(T tValue) {
m_TreeRoot = new Node(tValue);
}
template<typename T>
void AVL<T>::Insert(T tValue) {
m_TreeRoot = Insert(m_TreeRoot, tValue);
}
template<typename T>
typename AVL<T>::Node* AVL<T>::Insert(Node* pNode, T tValue) {
if (pNode == nullptr) {
return new Node(tValue);
}
if (tValue < pNode->data) {
pNode->lChild = Insert(pNode->lChild, tValue);
}
else if (tValue > pNode->data) {
pNode->rChild = Insert(pNode->rChild, tValue);
}
else {
std::cout << "已经存在该数据啦 -_- " << std::endl;
return pNode;
}
UpdateHeight(pNode);
int balance = BalanceFactor(pNode);
if (balance > 1) {
if (tValue < pNode->lChild->data) {
// LL型,需要做处理
return LLRotation(pNode);
}
else {
// LR型,需要做处理
return LRRotation(pNode);
}
}
else if (balance < -1) {
if (tValue > pNode->rChild->data) {
// RR型,需要做处理
return RRRotation(pNode);
}
else {
// RL型,需要做处理
return RLRotation(pNode);
}
}
return pNode;
}
template<typename T>
void AVL<T>::Delete(T tValue) {
m_TreeRoot = Delete(m_TreeRoot, tValue);
}
template<typename T>
typename AVL<T>::Node* AVL<T>::Delete(Node* pNode, T tValue) {
if (pNode == nullptr) {
return nullptr;
}
if (tValue < pNode->data) {
pNode->lChild = Delete(pNode->lChild, tValue);
}
else if (tValue > pNode->data) {
pNode->rChild = Delete(pNode->rChild, tValue);
}
else {
// Node found
if (pNode->lChild == nullptr) {
Node* temp = pNode->rChild;
delete pNode;
return temp;
}
else if (pNode->rChild == nullptr) {
Node* temp = pNode->lChild;
delete pNode;
return temp;
}
else {
// Node has two children
Node* minRightNode = FindMin(pNode->rChild);
pNode->data = minRightNode->data;
pNode->rChild = Delete(pNode->rChild, minRightNode->data);
}
}
UpdateHeight(pNode);
int balance = BalanceFactor(pNode);
if (balance > 1) {
if (BalanceFactor(pNode->lChild) >= 0) {
// LL型,需要做处理
return LLRotation(pNode);
}
else {
// LR型,需要做处理
return LRRotation(pNode);
}
}
else if (balance < -1) {
if (BalanceFactor(pNode->rChild) <= 0) {
// RR型,需要做处理
return RRRotation(pNode);
}
else {
// RL型,需要做处理
return RLRotation(pNode);
}
}
return pNode;
}
template<typename T>
typename AVL<T>::Node* AVL<T>::FindMin(Node* pNode) {
while (pNode->lChild != nullptr) {
pNode = pNode->lChild;
}
return pNode;
}
template<typename T>
void AVL<T>::Show() {
std::cout << "AVL Tree:" << std::endl;
if (m_TreeRoot == nullptr) {
std::cout << "Tree is empty." << std::endl;
}
else {
std::cout << "Data\tHeight" << std::endl;
Show(m_TreeRoot);
}
}
template<typename T>
void AVL<T>::Show(Node* pNode) {
if (pNode == nullptr) {
return;
}
std::cout << pNode->data << "\t" << pNode->Height << std::endl;
Show(pNode->lChild);
Show(pNode->rChild);
}
template<typename T>
int AVL<T>::Height(Node* pNode) {
if (pNode == nullptr) {
return 0;
}
return pNode->Height;
}
template<typename T>
int AVL<T>::BalanceFactor(Node* pNode) {
if (pNode == nullptr) {
return 0;
}
return Height(pNode->lChild) - Height(pNode->rChild);
}
template<typename T>
void AVL<T>::UpdateHeight(Node* pNode) {
if (pNode == nullptr) {
return;
}
pNode->Height = std::max(Height(pNode->lChild), Height(pNode->rChild)) + 1;
}
template<typename T>
typename AVL<T>::Node* AVL<T>::LLRotation(Node* pNode) {
Node* newRoot = pNode->lChild;
pNode->lChild = newRoot->rChild;
newRoot->rChild = pNode;
UpdateHeight(pNode);
UpdateHeight(newRoot);
return newRoot;
}
template<typename T>
typename AVL<T>::Node* AVL<T>::RRRotation(Node* pNode) {
Node* newRoot = pNode->rChild;
pNode->rChild = newRoot->lChild;
newRoot->lChild = pNode;
UpdateHeight(pNode);
UpdateHeight(newRoot);
return newRoot;
}
template<typename T>
typename AVL<T>::Node* AVL<T>::RLRotation(Node* pNode) {
pNode->rChild = LLRotation(pNode->rChild);
return RRRotation(pNode);
}
template<typename T>
typename AVL<T>::Node* AVL<T>::LRRotation(Node* pNode) {
pNode->lChild = RRRotation(pNode->lChild);
return LLRotation(pNode);
}
测试主函数:
main.cpp:
#include <iostream>
#include "AVL_Tree.h"
#include <algorithm>
int main() {
AVL<int> avl(25);
for (size_t i = 10; i < 20; i++) {
avl.Insert(i);
}
avl.Show();
avl.Delete(14);
avl.Show();
return 0;
}
运行效果截图:
如果发现文章中有错误,还请大家指出来,我会非常虚心地学习,我们一起进步!!!