AVL树(算法导论)

#include <iostream>
#include <algorithm>
using namespace std;

struct node // 节点
{
	node *left;
	node *right;
	node *parent;
	int data;
	int h; // 树高 h >= 2 时候就得调整平衡
	
	node() : left(nullptr), right(nullptr), parent(nullptr), data(0), h(1){} 
	node(int k) : left(nullptr), right(nullptr), parent(nullptr), data(k), h(1){}
};

class AVL 
{
public:
	AVL() : root(nullptr){} 
public:
	int height(node *x); // 求树的高度
	void print(node *x); // 中序遍历输出
	node *left_Rotate(node *x); // 左旋
	node *right_Rotate(node *x); // 右旋
	node *AVL_insert(node *x, node *z); // 插入
	node *root;  //指向根节点的指针
};

int AVL::height(node *x) 
{
	if (x == nullptr)
	{
		return 0;
	}
	return x->h;
}

node* AVL::left_Rotate(node *x)  //    4          3
{								//	    \	->   /
	node *y = x->right;        //        3      4
	x->right = y->left; 
	if (y->left != nullptr)
	{
		y->left->parent = x;
	}
	y->parent = x->parent;
	if (x->parent == nullptr)
	{
		root = y;
	}
	else if (x == x->parent->left)
	{
		x->parent->left = y;
	}
	else
	{
		x->parent->right = y;
	}
	y->left = x;
	x->parent = y;

	x->h = max(height(x->left), height(x->right)) + 1;
	y->h = max(height(y->left), height(y->right)) + 1;

	return y;
}
 
node* AVL::right_Rotate(node *x)     //   3     4
{								    //	 /  ->   \ 
	node *y = x->left;             //   4         3
	x->left = y->right;
	if (y->right != nullptr)
	{
		y->right->parent = x;
	}
	y->parent = x->parent;
	if (x->parent == nullptr)
	{
		root = y;
	}
	else if (x == x->parent->right)
	{
		x->parent->right = y;
	}
	else
	{
		x->parent->left = y;
	}
	y->right = x;
	x->parent = y;

	x->h = max(height(x->left), height(x->right)) + 1; // 更新树的高度
	y->h = max(height(y->left), height(y->right)) + 1;

	return y;
}

node* AVL::AVL_insert(node *x, node *z)
{
	if (x == nullptr)  //  只有空树时候执行 
	{
		root = z;
		return x;
	}

	if (z->data < x->data) 
	{		
		if (x->left)
		{
			z = AVL_insert(x->left, z);
		}
		x->left = z;
		z->parent = x;

		node *y = x->right;
		int hl = z->h; int hr = height(y);

		if (hl - hr >= 2)
		{
			node *a = z->left; node *b = z->right;
			if (a > b) // LL
			{		
				return right_Rotate(x);
			}
			else if (a < b) // LR
			{
				z = left_Rotate(z);
				return right_Rotate(x);
			}
		}
		x->h = max(hl, hr) + 1;
		return x;
	}

	if (z->data > x->data)
		{
		if (x->right)
			z = AVL_insert(x->right, z);
		x->right = z;
		z->parent = x;

		node *y = x->left;
		int hl = height(y); int hr = z->h;

		if (hr - hl >= 2)
		{
	    	node *a = z->left;
			node *b = z->right;
			if (a > b)
			{
				z = right_Rotate(z);
				return left_Rotate(x);
			}
				else if (a < b)
			{
				return left_Rotate(x);
			}
		}
		x->h = max(hl, hr) + 1;
		return x;
	}
}

void AVL::print(node *x)
{
	if (x == nullptr)
	{
		return;
	}
	print(x->left);
	cout << x->data << "  ";
	print(x->right);
}

int main()
{
	AVL tree;
	
	for (int i = 0; i < 6; ++i)
	{
		tree.AVL_insert(tree.root, new node(i+1));
	}

	tree.print(tree.root);


	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值