C++数据结构之AVL树

AVL树是最先发明的自平衡二叉查找树。AVL树得名于它的发明者 G.M. Adelson-Velsky 和 E.M. Landis,他们在 1962 年的论文 "An algorithm for the organization of information" 中发表了它。 在AVL树中,查找、删除和插入在最坏情况下的时间复杂度都是O(logn),增加、删除等操作可能需要对树进行一次或者多次旋转来使树重新平衡。我们在这里主要讨论AVL树的旋转问题。 首先是右旋:如图

右旋第二种情况:


左旋:

1:

2:

双旋:

左右双旋1:

左右双旋2:

右左双旋1:

右左双旋2:

AVL树的难点在于平衡树时进行的旋转操作,大家可以多多画图帮助理解,只要理解透彻,代码就很好完成。AVL树是最早的自平衡二叉树,相比于后来出现的平衡二叉树(红黑树,treap,splay树)而言,它现在应用较少,但研究AVL树对于了解后面出现的常用平衡二叉树具有重要意义。

源码如下:

#include<iostream>
#include<assert.h>
using namespace std;

template<class K,class V>
struct AVLTreeNode
{
	K _key;
	V _value;

	AVLTreeNode<K, V>* _left;
	AVLTreeNode<K, V>* _right;
	AVLTreeNode<K, V>* _parent;
	
	int _bf;

	AVLTreeNode(const K& key, const V& value)
		: _key(key)
		, _value(value)
		, _left(NULL)
		, _right(NULL)
		, _parent(NULL)
		, _bf(0)
	{}
};
template<class K,class V>

class AVLTree
{
	typedef AVLTreeNode<K, V> Node;
public:
	AVLTree()
		:_root(NULL)
	{}
	bool Insert(const K& key,const V& value)
	{
		if (_root == NULL)
		{
			_root = new Node(key,value);
			return true;
		}
		Node* cur = _root;
		Node* parent= NULL;
		while (cur)
		{
			if (cur->_key < key)
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (cur->_key > key)
			{
				parent = cur;
				cur = cur->_left;
			}
			else
			{
				return false;
			}

		}

		cur = new Node(key, value);
		if (parent->_key < key)
		{
			parent->_right = cur;
			cur->_parent = parent;

		}
		else
		{
			parent->_left = cur;
			cur->_parent = parent;
		}
		while (parent)
		{
			if (cur->_bf == 0 && cur->_left != NULL && cur->_right != NULL)
			{
				break;
			}
			if (parent->_left == cur)
			{

				parent->_bf--;
			}
			else
			{
				parent->_bf++;
			}
			if (parent->_bf == 2 && parent->_bf == -2)
			{
				break;
			}
			cur = parent;
			parent = cur->_parent;
		}
		if (parent == NULL)
		{
			return true;
		}
		//旋转调整
		if (parent->_bf == 2)
		{
			if (cur->_bf == 1)
			{
				RotateL(parent);
			}
			else if (cur->_bf == -1)
			{
				RotateRL(parent);
			}
		}
		else
		{
			if (cur->_bf == 1)
			{
				RotateLR(parent);
			}
			else if (cur->_bf == -1)
			{
				RotateR(parent);
			}
		}


	}
	bool IsAVLTree()
	{
		int Height = 0;
		return _IsAVLTree(_root, Height);
	}
	void Inorder()
	{
		_Inorder(_root);
		cout << endl;

	}
protected:

	void RotateR(Node* parent)
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;
		parent->_left = subLR;
		if (subLR)
		{
			subLR->_parent = parent;
		}
		Node* ppNode = parent->_parent;

		subL->_right = parent;
		parent->_parent = subL;

		if (ppNode)
		{
			if (ppNode->_left == parent)
			{
				ppNode->_left = subL;
			}
			else
			{
				ppNode->_right = subL;
				subL->_parent = ppNode;
			}
		}
		else
		{
			subL->_parent = NULL;
			_root = subL;
		}
		subL->_bf = parent->_bf = 0;

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

		if (subRL)
		{
			//subRL = subRL->_parent;
			subRL->_parent = parent;
		}
		Node* ppNode = parent->_parent;

		subR->_left = parent;
		parent->_parent = subR;

		if (ppNode)
		{
			if (ppNode == parent)
			{
				parent->_left = subR;
			}
			else
			{
				ppNode->_right = subR;
				subR->_parent = ppNode;

			}

		}
		else
		{
			subR->_parent = NULL;
			_root = subR;
		}
		subR->_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 == 0)
		{
			subR->_bf = parent->_bf = 0;
		}
		else if (bf == 1)
		{
			subR->_bf = 0;
			parent->_bf = -1;
		}
		else
		{
			subR->_bf = 1;
			parent->_bf = 0;
		}
		subR->_bf = 0;
	}
	void RotateLR(Node* parent)
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;
		int bf = subLR->_bf;

		RotateL(parent->_left);
		RotateR(parent);

		if (bf == 0)
		{
			parent->_bf = subL->_bf = 0;
			subLR->_bf = 0; 
		}
		else if (bf == -1)
		{
			subL->_bf = 0;
			parent->_bf = 1;
			subLR->_bf = 0;
		}
		else
		{
			subL->_bf = 1;
			parent->_bf = 0;
			subLR->_bf = 0;
		}
	}
	int _Height(Node* root)
	{
		if (root == NULL)
		{
			return 0;
		}
		int left = _Height(root->_left) + 1;
		int right = _Height(root->_right) + 1;

		return left > right ? left : right;
	}
	bool _IsAVLTree(Node* root, int& Height)
	{
		if (root == NULL);
		{
			int Height = 0;
			return true;
		}
		int LeftHeight = 0;
		if (_IsAVLTree(root->_left, LeftHeight) == false)
		{
			return false;
		}
		int RightHeight = 0;
		if (_IsAVLTree(root->_right, RightHeight) == false)
		{
			return false;
		}

		Height = LeftHeight > RightHeight ? LeftHeight + 1 : RightHeight + 1;
		return abs(LeftHeight - RightHeight) < 2;


	}
	void _Inorder(Node* root)
	{
		if (root == NULL)
		{
			return;
		}
		_Inorder(root->_left);
		cout << root->_key << ""<<endl;
		_Inorder(root->_right);
		
		
	}
protected:
	Node* _root;
};


void test()
{
	AVLTree<int, int> tree;
	int a[9] = { 16, 3, 7, 11, 9, 26, 18, 14, 15 };
	
	for (size_t i = 0; i < 9; ++i)
	{
		tree.Insert(a[i], i);
	}
	tree.Inorder();
	cout << "IsAVLTree?" << tree.IsAVLTree() << "" << endl;





}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值