有序表之AVL树的c++实现

AVL树的平衡性判断是:左树和右树的高度之差不超过1

它并不像SB树和红黑树那样,限制比较宽,AVL树的限制是非常严格的

无论是SB、红黑、AVL,它们的平衡判断是不一样的,其他的操作几乎都是一样的

都是利用左旋和右旋来恢复平衡

而增删改查操作都是建立在BST平衡二叉排序树的基础上

而不平衡有四种情况:LL、LR、RR、RL

注意:当一棵树同时是LL、LR的情况时,必须优先选择LL对应情况的平衡策略,否则选择LR的化,最终的树还是不平衡;对于RR、RL也是同理

接下来是代码实现:

#include<iostream>
#include<unordered_map>
#include<vector>
using namespace std;
//实现AVL树
template<class T>
class Less {
public:
	bool operator()(const T& o1, const T& o2) {
		return o1 < o2;
	}
};
template<class K,class V>
class AVLNode {
public:
	K key;//本质还是一棵BST,需要有索引来进行比较
	V value;//装的是你真正想存入的东西
	int h;//平衡因子
	AVLNode* left;
	AVLNode* right;
	AVLNode(K k, V v) {
		key = k;
		value = v;
		h = 0;
		left = nullptr;
		right = nullptr;
	}
};
template<class K,class V/*,class comparator = Less<K>*/>//有序表,用户需要提供比较器来对索引进行比较
class AVLTreeMap {
private:
	using AVLNode = AVLNode<K,V>*;
	int size;
	AVLNode root;
	//comparator com;
public:
	AVLTreeMap() {
		size = 0;
		root = nullptr;
	}
	AVLNode LeftRotate(AVLNode cur) {
		AVLNode right = cur->right;
		cur->right = right->left;
		right->left = cur;
		//由于right和cur的孩子都有可能变化了,故它们的高度也有可能变化
		cur->h = max((cur->left != nullptr ? cur->left->h : 0),
			(cur->right != nullptr ? cur->right->h : 0)) + 1;
		right->h = max((right->left != nullptr ? right->left->h : 0),
			(right->right != nullptr ? right->right->h : 0)) + 1;
		return right;
	}
	AVLNode RightRotate(AVLNode cur) {
		AVLNode left = cur->left;
		cur->left = left->right;
		left->right = cur;
		cur->h = max((cur->left != nullptr ? cur->left->h : 0),
			(cur->right != nullptr ? cur->right->h : 0)) + 1;
		left->h = max((left->left != nullptr ? left->left->h : 0),
			(left->right != nullptr ? left->right->h : 0)) + 1;
		return left;
	}
	AVLNode maintain(AVLNode cur) {
		if (cur == nullptr) {
			return cur;
		}
		int leftheight = cur->left != nullptr ? cur->left->h : 0;
		int rightheight = cur->right != nullptr ? cur->right->h : 0;
		//如果两边高度之差的绝对值大于1,就说明不平衡,就要去四种不平衡情况中进行判断
		if (abs(leftheight - rightheight) > 1) {
			if (leftheight > rightheight) {
				int Leftleftheight = cur->left != nullptr && cur->left->left != nullptr ?
					cur->left->left->h : 0;
				int Leftrightheight = cur->left != nullptr && cur->left->right != nullptr ?
					cur->left->right->h : 0;
				//如果左树高度≥右树,等于情况下说明是LL和LR同时成立,此刻只有LL的右旋能把树调成平衡
				if (Leftleftheight >= Leftrightheight) {
					cur = RightRotate(cur);
				}
				else {
					cur->left = LeftRotate(cur->left);
					cur = RightRotate(cur);
				}
			}
			else {
				int Rightleftheight = cur->right != nullptr && cur->right->left != nullptr ?
					cur->right->left->h : 0;
				int Rightrightheight = cur->right != nullptr && cur->right->right != nullptr ?
					cur->right->right->h : 0;
				//和上面类似,也是RR的左旋优先级更高
				if (Rightrightheight >= Rightleftheight) {
					cur = LeftRotate(cur);
				}
				else {
					cur->right = RightRotate(cur->right);
					cur = LeftRotate(cur);
				}
			}
		}
		return cur;
	}
	AVLNode add(AVLNode cur, K key, V value) {
		//函数功能为返回添加完结点的根节点,并且这棵树已经调整好了平衡
		if (cur == nullptr) {
			return new AVLNode<K, V>(key, value);
		}
		if (com(key, cur->key)) {
			cur->left = add(cur->left, key, value);
		}
		
		else {
			cur->right = add(cur->right, key, value);
		}
		cur->h = max((cur->left != nullptr ? cur->left->h : 0),
			(cur->right != nullptr ? cur->right->h : 0)) + 1;
		return maintain(cur);
	}
	AVLNode Delete(AVLNode cur, K key) {
		//删除指定结点,并返回调整后的根结点
		if (com(key, cur->key)) {
			cur->left = Delete(cur->left, key);
		}
		else {
			cur->right = Delete(cur->right, key);
		}
		if (cur->key == key) {
			if (cur->left == nullptr && cur->right == nullptr) {
				cur = nullptr;
			}
			else if (cur->left == nullptr && cur->right != nullptr) {
				cur = cur->right;
			}
			else if (cur->left != nullptr && cur->right == nullptr) {
				cur = cur->left;
			}
			else {
				AVLNode des = cur->right;
				while (des->left != nullptr) {
					des = des->left;
				}
				//原本我们需要将des和cur进行替换,然后再删除des,接着从des的父结点开始往上去判断每个结点
				//是否需要进行平衡性调整
				//这里是直接将des断链
				cur->right = Delete(cur->right, des->key);
				des->left = cur->left;
				des->right = cur->right;
				cur = des;
			}
		}
		if (cur != nullptr) {
			//删除完结点后,cur的孩子进行了变化,故cur的高度也变化了
			cur->h = max((cur->left != nullptr ? cur->left->h : 0),
				(cur->right != nullptr ? cur->right->h:0)) + 1;
		}
		//当cur有两个孩子的时候,为什么要直接将des断链,这是为了155行的代码复用,这样就可以直接从cur开始找了
		//而不用写非递归,这个是递归函数
		return maintain(cur);
	}
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AVL树是一种自平衡二叉搜索树,它的每个节点存储一个键值对,且每个节点的左子树和右子树的高度差不超过1。这种平衡特性使得AVL树在查找、插入和删除操作方面都有很好的性能表现。 下面是一个简单的AVL树C++实现: ```c++ #include <iostream> using namespace std; // AVL树节点 struct Node { int key; int height; Node *left; Node *right; Node(int k) : key(k), height(1), left(NULL), right(NULL) {} }; // 获取节点高度 int height(Node *node) { if (node == NULL) { return 0; } return node->height; } // 获取节点平衡因子 int balanceFactor(Node *node) { if (node == NULL) { return 0; } return height(node->left) - height(node->right); } // 更新节点高度 void updateHeight(Node *node) { node->height = max(height(node->left), height(node->right)) + 1; } // 右旋操作 Node* rightRotate(Node *node) { Node *leftChild = node->left; Node *rightChild = leftChild->right; leftChild->right = node; node->left = rightChild; updateHeight(node); updateHeight(leftChild); return leftChild; } // 左旋操作 Node* leftRotate(Node *node) { Node *rightChild = node->right; Node *leftChild = rightChild->left; rightChild->left = node; node->right = leftChild; updateHeight(node); updateHeight(rightChild); return rightChild; } // 插入节点 Node* insert(Node *node, int key) { if (node == NULL) { return new Node(key); } if (key < node->key) { node->left = insert(node->left, key); } else if (key > node->key) { node->right = insert(node->right, key); } else { return node; } updateHeight(node); int bf = balanceFactor(node); if (bf > 1) { if (balanceFactor(node->left) >= 0) { return rightRotate(node); } else { node->left = leftRotate(node->left); return rightRotate(node); } } else if (bf < -1) { if (balanceFactor(node->right) <= 0) { return leftRotate(node); } else { node->right = rightRotate(node->right); return leftRotate(node); } } return node; } // 中序遍历AVL树 void inOrder(Node *node) { if (node == NULL) { return; } inOrder(node->left); cout << node->key << " "; inOrder(node->right); } int main() { Node *root = NULL; root = insert(root, 10); root = insert(root, 20); root = insert(root, 30); root = insert(root, 40); root = insert(root, 50); root = insert(root, 25); inOrder(root); cout << endl; return 0; } ``` 在上面的实现中,我们使用了递归插入节点,并在插入节点后更新了节点的高度和平衡因子。当节点的平衡因子大于1或小于-1时,我们进行相应的旋转操作来保持树的平衡。最后,我们在main函数中插入一些节点,并进行中序遍历来检查树是否正确构建。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值