AVLTree模拟

#pragma once
using namespace std;
template<class K,class V>
struct AVLTreeNode
{
	AVLTreeNode<K, V>* _left;
	AVLTreeNode<K, V>* _right;
	AVLTreeNode<K, V>* _parent;

	int _bf;//balance factor平衡因子
	pair<K,V> _kv;//元素
	AVLTreeNode(const pair<K, V>& kv)
		:_left(nullptr)
		, _right(nullptr)
		, _parent(nullptr)
		, _kv(kv)
		, _bf(0)
	{}
};

template<class K,class V>
class AVLTree
{
	typedef AVLTreeNode<K, V> Node;
public:
	AVLTree() : _root(nullptr) // 显式初始化 _root 为 nullptr  
	{
		// 构造函数的其他代码(如果有的话)  
	}
	bool insert(const pair<K, V>& kv)
	{
		//1.先按搜索树进行插入
		if (_root == NULL)
		{
			_root = new Node(kv);
			return true;
		}
		Node* parent = nullptr;
		Node* cur = _root;
		while (cur) {
			if (cur->_kv.first > kv.first)
			{
				parent = cur;
				cur = cur->_left;
			}
			else if (cur->_kv.first < kv.first)
			{
				parent = cur;
				cur = cur->_right;
			}
			else {
				return false;
			}
		}
		cur = new Node(kv);
		if (parent->_kv.first < kv.first)
		{
			parent->_right = cur;
			cur->_parent = parent;
		}
		else {
			parent->_left = cur;
			cur->_parent = parent;
		}
		//2.更新平衡因子
		while (parent) {
			if (cur == parent->_right)
				parent->_bf++;
			else
				parent->_bf--;
			if (parent->_bf == 0)
			{
				//说明parent所在的子树高度不变,更新结束
				break;
			}
			else if (parent->_bf == 1 || parent->_bf == -1) {
				//说明parent所在的子树高度变了,继续往上更新
				cur = parent;
				parent = parent->_parent;
			}
			else if (parent->_bf == 2 || parent->_bf == -2) {
				//进行旋转
				if (parent->_bf == 2) {
					if (cur->_bf == 1) {
						RotateL(parent);
					}
					else if (cur->_bf == -1) {
						RotateRL(parent);
					}

				}
				else if (parent->_bf == -2) {
					if (cur->_bf == -1) {
						RotateR(parent);
					}
					else if (cur->_bf == 1) {
						RotateLR(parent);
					}
				}
				break;
			}
		}
		return true;
	}

	void RotateL(Node* parent)
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;

		parent->_right = subRL;
		if (subRL)
			subRL->_parent = parent;
		subR->_left = parent;
		Node* ppNode = parent->_parent;
		parent->_parent = subR;
		//原来parent是这颗树的根,现在subR是跟
		//parent为根的树只是整棵树中的子树,改变连接关系,那么subR要顶替他的位置
		if (_root == parent)
		{
			_root = subR;
			subR->_parent = nullptr;
		}
		else
		{
			if (ppNode->_left == parent)
				ppNode->_left = subR;
			else
				ppNode->_right = subR;

			subR ->_parent = ppNode;
		}
		parent->_bf = subR->_bf = 0;
	}
	void RotateR(Node* parent)
	{
		Node* subL = parent->_left;
		Node* subRL = subL->_right;
		Node* Nodepp = parent->_parent;
		parent->_left = subRL;
		if (subRL != NULL)
		{
			subRL->_parent = parent;
		}
		subL->_right = parent;
		parent->_parent = subL;
		
		if (parent == _root) {
			subL->_parent = nullptr;
			_root = subL;
		}
		else {
			if (Nodepp->_left == parent) {
				Nodepp->_left = subL;
			}
			else {
				Nodepp->_right = subL;
			}
			subL->_parent = Nodepp;
		}
		subL->_bf = parent->_bf = 0;

	}
	void RotateRL(Node* parent) {
		Node* subR = parent->_right;
		Node* subRL = subR->_left;
		int bf = subRL->_bf;
		RotateR(subR);
		RotateL(parent);
		if (bf == -1) {
			parent->_bf = 0;
			subR->_bf = 1;
			subRL->_bf = 0;
		}
		else if (bf == 1) {
			subR->_bf = 0;
			parent->_bf = -1;
			subRL->_bf = 0;
		}
		else if (bf == 0) {
			subR->_bf = 0;
			parent->_bf = 0;
			subRL->_bf = 0;
		}
	}
	void RotateLR(Node* parent) {
		Node* subL = parent->_left;
		Node* subLR = subL->_right;
		int bf = subLR->_bf;
		RotateL(subL);
		RotateR(parent);
		if (bf == 1)
		{
			parent->_bf = 0;
			subL->_bf = -1;
			subLR->_bf = 0;
		}
		else if (bf == -1) {
			parent->_bf = 1;
			subL->_bf = 0;
			subLR->_bf = 0;
		}
		else if (bf == 0) {
			parent->_bf = 0;
			subL->_bf = 0;
			subLR->_bf = 0;
		}
	}

	void _InOrder(Node* root)
	{
		if (root == nullptr) {
			return;
		}
		_InOrder(root->_left);
		cout << root->_kv.first << ":" << root->_kv.second << ":" << root->_bf << endl;
		if(root->_parent)
			cout<<"parent"<<root->_parent->_kv.first << endl;
		if (root->_left)
			cout <<"left"<< root->_left->_kv.first << ":";
		if(root->_right)
			cout<<"right"<< root->_right->_kv.first << endl;
		_InOrder(root->_right);
	}
	void InOrder()
	{
		_InOrder(_root);
		cout << endl;
	}
	
	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 _IsBalance(Node* root) {
		if (root == nullptr)
			return true;
		int leftHeight = Height(root->_left);
		int rightHeight = Height(root->_right);
		return abs(leftHeight - rightHeight) < 2
			&& _IsBalance(root->_left)
			&& _IsBalance(root->_right);
	}
	bool IsBalance() {
		return _IsBalance(_root);
	}
private:
	Node* _root;
};
void TestAVLTree() {
	int a[] = { 4, 2, 6, 1, 3, 5, 15, 7, 16,14 };
	AVLTree<int, int> t;
	for (auto e : a) {
		t.insert(make_pair(e, e));
	}
	t.InOrder();
	cout << t.IsBalance() << endl;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值