AVL树的简单实现

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
class AVLNode
{
public:
	int data;
	int height;
	AVLNode *lchid;
	AVLNode *rchid;
public:
	AVLNode(int data): data(data),height(1),lchid(0),rchid(0){}
};
class AVL
{
public:
	AVLNode *root;
public:
	AVL(){
		root = NULL;
	}
	~AVL(){
		delete root;
	}
	int height(AVLNode* root)
	{
		if(root)
			return root->height;
		return 0;
	}
	AVLNode *findmaxNode(AVLNode * root)//寻找书中最大节点
	{
		if(root->rchid)
			root=root->rchid;
		return root;
	}
	AVLNode *findminNode(AVLNode *root)//寻找最小节点
	{
		if(root->lchid)
			root = root ->lchid;
		return root;
	}
	//右旋

    // 对节点y进行向右旋转操作,返回旋转后新的根节点x
    //        y                              x
    //       / \                           /   \
    //      x   T4     向右旋转 (y)        z     y
    //     / \       - - - - - - - ->    / \   / \
    //    z   T3                       T1  T2 T3 T4
    //   / \
    // T1   T2
    AVLNode *llRotate(AVLNode *p)
	{
		AVLNode *temp = p->lchid;
		p->lchid = temp ->rchid;
		temp->rchid =p;
		// 节点高度 由该节点的子树 唯一决定,所以只有字数发生变化时才会更新高度
		temp->height =max(height(temp->lchid),height(temp->rchid))+1;
		p->height =max(height(p->lchid),height(p->rchid))+1;
		return temp;
	}
	//左旋
	// 对节点y进行向左旋转操作,返回旋转后新的根节点x
    //    y                             x
    //  /  \                          /   \
    // T1   x      向左旋转 (y)       y     z
    //     / \   - - - - - - - ->   / \   / \
    //   T2  z                     T1 T2 T3 T4
    //      / \
    //     T3 T4
	AVLNode *rrRotate(AVLNode *p)
	{
		AVLNode *temp = p->rchid;
		p->rchid = temp ->lchid;
		temp->lchid = p;
		temp->height = max(height(temp->lchid),height(temp->rchid))+1;
		p->height = max(height(p->lchid),height(p->rchid))+1;
		return temp;
	}
	//lr
	AVLNode *lrRotate(AVLNode *p)
	{
		AVLNode *temp = rrRotate(p->lchid);
		return llRotate(p);
	}
	//rl
	AVLNode *rlRotate(AVLNode *p)
	{
		AVLNode *temp = llRotate(p->rchid);
		return rrRotate(p);
	}
	//插入新节点 并且保持平衡
	void insert(int data,AVLNode*& root)
	{
		if(!root)
			root = new AVLNode(data);
		else
		{
			if(data<root->data)
			{
				insert(data,root->lchid);
				if(height(root->lchid)-height(root->rchid)==2)
				{
					if(data<root->lchid->data)
						root=llRotate(root);
					else
						root=lrRotate(root);
				}
			}
			else if(data>root->data)
			{
			    insert(data,root->rchid);
				if(height(root->rchid)-height(root->lchid)==2)
                {
                if(data > root->lchid->data)
					root= rrRotate(root);
				else
					root=rlRotate(root);
                }
			}
			else
				cout<<"存在该值"<<endl;
		}

	}
	//删除
	void del(int data,AVLNode *& root)
	{
		if(data<root->data)
		{
			del(data,root->lchid);
			if(height(root->rchid)-height(root->lchid)==2)
			{
				AVLNode *temp=root->rchid;
				if(height(temp->lchid)>height(temp->rchid))
					root=rlRotate(root);
				else
				{
					root=rrRotate(root);
				}
			}
		}
		else if(data>root->data)
		{
			del(data,root->rchid);
			if(height(root->lchid)-height(root->rchid)==2)
			{
				AVLNode *temp=root->lchid;
				if(height(temp->lchid)>height(temp->rchid))
					root=llRotate(root);
				else
				{
					root=lrRotate(root);
				}
			}
		}
		else
		{
			if(root->lchid && root->rchid)
			{
				if(height(root->lchid)>height(root->rchid))
				{
					AVLNode *maxNode =findmaxNode(root->lchid);
					root->data = maxNode->data;
					del(maxNode->data,root->lchid);
				}
				else
				{
					AVLNode *minNode =findminNode(root->rchid);
					root->data = minNode->data;
					del(minNode->data,root->rchid);
				}
			}
			else
			{
				if(root->lchid)
				{
					root->data=root->lchid->data;
					root->lchid=NULL;
				}
				else if(root->rchid)
				{
					root->data = root->rchid->data;
					root->rchid =NULL;
				}
				else
				{
					root = NULL;
				}
			}
		}
	}
	void inorder(AVLNode *root)
	{
		if (root)
		{
			inorder(root->lchid);
			cout<<root->data<<endl;
			inorder(root->rchid);
		}
	}


};
int main()
{
	AVL tree;
	tree.insert(5,tree.root);
	tree.insert(15,tree.root);
	tree.insert(25,tree.root);
	tree.insert(35,tree.root);
	tree.insert(45,tree.root);
	tree.insert(55,tree.root);
	tree.insert(45,tree.root);
	tree.del(55,tree.root);
	tree.inorder(tree.root);
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值