AVL树的创建和返回AVL树的根节点

三个文件,注释已经很清楚了:

main.cpp

#include"tree.h"

int main(void)
{
	bitt A;
	int n, v;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> v;
		A.insert(A.root, v);
	}
	cout<<A.get()<<endl;
	system("pause");
	return 0;
}

head.h

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

tree.h

#pragma once
#include"head.h"

struct node
{
	int v, height;//v为结点权值,height为子树深度
	node *lchild, *rchild;
};

class bitt
{
public:
	node *root;
	bitt() { root = NULL; }
	~bitt(){}
	node* newNode(int v);//生成一个新节点,v为结点权值
	int getHeight(node *root);//获取以root为根节点的子树当前的height(深度值)
	void updateHeight(node *root);//更新结点的height(深度值)
	int getBalanceFactor(node *root);//计算结点root的平衡因子
	void L(node* &root);//左旋算法
	void R(node* &root);//右旋算法
	void insert(node* &root,int v);//插入权值结点,权值为v
	node* create(int data[], int n);//AVL树的创建
	int get();//取出值
	node* flag();
	node* getRoot();
};

node* bitt::newNode(int v)
{
	node* Node = new node;//申请一个node型变量的地址空间
	Node->v = v;//结点权值为v
	Node->height = 1;//结点高度初始为1
	Node->lchild = Node->rchild = NULL;//初始状态下没有左右孩子结点
	return Node;//返回新建结点的地址
}

node* bitt::flag()
{
	node* Node = new node;
	Node->v = 0;
	Node->height = 0;
	Node->lchild = Node->rchild = NULL;
	return Node;
}

int bitt::getHeight(node *root)
{
	if (root == NULL)return 0;//空结点高度为0
	return root->height;
}

void bitt::updateHeight(node* root)
{
	root->height = max(getHeight(root->lchild), getHeight(root->rchild)) + 1;
	//返回左右子树最大者再加1(1是根节点的深度)
}

int bitt::getBalanceFactor(node* root)
{
	return getHeight(root->lchild) - getHeight(root->rchild);
	//左子树深度减去右子树深度
}

void bitt::L(node*&root)
{
	//root指向A,temp指向B,具体见示意图
	node* temp = root->rchild;
	root->rchild = temp->lchild;
	temp->lchild = root;
	updateHeight(root);//更新A的高度
	updateHeight(temp);//更新B的高度
	root = temp;
}

void bitt::R(node*&root)
{
	//类似左旋,只是形状恰好相反
	node* temp = root->lchild;
	root->lchild = temp->rchild;
	temp->rchild = root;
	updateHeight(root);
	updateHeight(temp);
	root = temp;
}

void bitt::insert(node *&root,int v)
{
	if (root == NULL)//到达空结点
	{
		root = newNode(v);
		return;
	}
	if (v < root->v)//v比根结点权值小
	{
		insert(root->lchild,v);//插入左子树
		updateHeight(root);//更新树高
		if (getBalanceFactor(root) == 2)
		{
			if (getBalanceFactor(root->lchild) == 1)//LL
				R(root);
			else if (getBalanceFactor(root->lchild) == -1)//LR
			{
				L(root->lchild);
				R(root);
			}
		}
	}
	else//v比根结点权值大
	{
		insert(root->rchild, v);//插入右子树
		updateHeight(root);//更新树高
		if (getBalanceFactor(root) == -2)
		{
			if (getBalanceFactor(root->rchild) == -1)//RR
				L(root);
			else if (getBalanceFactor(root->rchild) == 1)//RL
			{
				R(root->rchild);
				L(root);
			}
		}
	}
}

node* bitt::create(int data[], int n)
{
	node* root = NULL;//建立空树根节点
	for (int i = 0; i < n; i++)
		insert(root, data[i]);//插入
	return root;//返回根节点
}

int bitt::get()
{	
	if (root != NULL)
		return root->v;
	else
		return NULL;
}

node* bitt::getRoot()
{
	return root;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TIM33470348

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值