AVL树

本文详细介绍了AVL树的四种旋转操作:左旋、右旋、左右旋和右左旋,以及如何通过这些旋转保持AVL树的平衡。在插入节点的过程中,通过判断子树的高度差来决定进行哪种旋转,确保树的高度平衡,从而实现高效的查找、插入和删除操作。
#include <iostream>

#define MAX 2; 

using namespace std;

struct treenode{
	int key;
	int high;
	treenode* left;
	treenode* right;
}; 
class autoTree{
public:

	int maxH(int a,int b);
	treenode* Lrotate(treenode* node);//向左转 ,即转到左下角 RR
	treenode* Rrotate(treenode* node);//向右转 LL
	treenode* RL(treenode* node);//RL
	treenode* LR(treenode* node);//LR
	treenode* insertnode(treenode* node,int key);
	int printH(treenode* root);//输出高度 
}; 


int autoTree::maxH(int a,int b){
	if(a>b)return a;
	else return b;
}
treenode* autoTree::Lrotate(treenode* node){//RR 
	treenode* temp=node->right;
	node->right=temp->left;
	temp->left=node;
	node->high=max(node->left->high,node->right->high)+1;
	temp->high=max(temp->left->high,temp->right->high)+1;
	return temp;
}
treenode* autoTree::Rrotate(treenode* node){//LL
	treenode* temp=node->left;
	node->left=temp->right;
	temp->right=node;
	node->high=max(node->left->high,node->right->high)+1;
	temp->high=max(temp->left->high,temp->right->high)+1;
	return temp;
} 
treenode* autoTree::RL(treenode* node){
	node->right=Rrotate(node->right);
	return Lrotate(node);
}
treenode* autoTree::LR(treenode* node){
	node->left=Lrotate(node->left);
	return Rrotate(node);
}
treenode* autoTree::insertnode(treenode* node,int key){
	if(!node){
	
		//node=initTree(key);
		node=new treenode();
		node->high=0;
		node->key=key;
		node->left=node->right=NULL;
		//	cout<<"succeed1"<<endl;
	}
	else if(key>node->key){
		node->right=insertnode(node->right,key);
		if(node->right->high-node->left->high>1){
			if(key>node->right->key)node=Lrotate(node);
			else{
				node=RL(node);
				//node->right=Rrotate(node->right);
			}
		}
	}
	else if(key<node->key){
		node->left=insertnode(node->left,key);
		if(node->left->high-node->right->high>1){
			if(key<node->left->key)node=Rrotate(node);
			else node=LR(node);
		}
	}
	node->high=maxH(node->left->high,node->right->high)+1;
	return node;
}
int autoTree::printH(treenode* root){
	cout<<root->high;
}

 

AVL是一种自平衡的二叉查找,它确保了的高度始终保持在对数级别,从而保证了查找、插入和删除操作的时间复杂度为O(log n)。这种数据结构以它的发明者G.M. Adelson-Velsky和E.M. Landis的名字命名[^1]。 ### AVL的定义 - **平衡因子**:每个节点都有一个平衡因子,它是该节点左子的高度减去右子的高度。对于AVL来说,所有节点的平衡因子只能是-1, 0或1。 - **空**:如果T是空,则它自然是一个AVL。 - **非空**:若T不是空,则其左右子TL和TR都必须是AVL,并且对于任意节点,|HL - HR| ≤ 1(其中HL和HR分别表示左子和右子的高度)。 ### AVL的操作 当进行插入或删除操作时,可能会破坏AVL的平衡性,这时需要通过旋转来重新恢复平衡: - **单旋转**: - 左单旋(Single Rotate with Left)用于处理左孩子的左子过高。 - 右单旋(Single Rotate with Right)用于处理右孩子的右子过高。 - **双旋转**: - 左右双旋(Double Rotate with Left)用于处理左孩子的右子过高的情况。 - 右左双旋(Double Rotate with Right)用于处理右孩子的左子过高的情况。 这些旋转操作可以保持AVL的性质不变,并且能够快速地调整的结构以维持平衡。 ### AVL的实现 下面是一个简单的C语言代码示例,展示了如何定义AVL的节点以及实现基本的插入操作。这个例子中包含了必要的函数声明和一些核心逻辑。 ```c #include <stdio.h> #include <stdlib.h> // 定义AVL节点结构体 typedef struct AvlNode { int element; struct AvlNode *left; struct AvlNode *right; int height; // 节点的高度 } *AvlTree, *Position; // 获取两个整数中的较大者 int max(int a, int b) { return (a > b) ? a : b; } // 计算给定节点的高度 static int Height(AvlTree T) { if (T == NULL) return -1; // 空节点高度为-1 else return T->height; } // 单旋转 - 左左情况 AvlTree singlerotatewithLeft(AvlTree K2) { Position K1 = K2->left; K2->left = K1->right; K1->right = K2; // 更新节点高度 K2->height = max(Height(K2->left), Height(K2->right)) + 1; K1->height = max(Height(K1->left), K2->height) + 1; return K1; // 新根 } // 单旋转 - 右右情况 AvlTree singlerotatewithRight(AvlTree K2) { Position K1 = K2->right; K2->right = K1->left; K1->left = K2; // 更新节点高度 K2->height = max(Height(K2->left), Height(K2->right)) + 1; K1->height = max(Height(K1->right), K2->height) + 1; return K1; // 新根 } // 双旋转 - 左右情况 AvlTree doublerotatewithLeft(AvlTree K3) { K3->left = singlerotatewithRight(K3->left); return singlerotatewithLeft(K3); } // 双旋转 - 右左情况 AvlTree doublerotatewithRight(AvlTree K3) { K3->right = singlerotatewithLeft(K3->right); return singlerotatewithRight(K3); } // 插入新元素到AVLAvlTree Insert(int x, AvlTree T) { if (T == NULL) { // 如果为空,创建新节点 T = (AvlTree)malloc(sizeof(struct AvlNode)); if (T == NULL) printf("Out of space!!!"); else { T->element = x; T->left = T->right = NULL; T->height = 0; // 新叶节点高度为0 } } else if (x < T->element) { // 向左子插入 T->left = Insert(x, T->left); // 检查并修复平衡 if (Height(T->left) - Height(T->right) == 2) { if (x < T->left->element) T = singlerotatewithLeft(T); // 左左旋转 else T = doublerotatewithLeft(T); // 左右旋转 } } else if (x > T->element) { // 向右子插入 T->right = Insert(x, T->right); // 检查并修复平衡 if (Height(T->right) - Height(T->left) == 2) { if (x > T->right->element) T = singlerotatewithRight(T); // 右右旋转 else T = doublerotatewithRight(T); // 右左旋转 } } // 更新高度 T->height = max(Height(T->left), Height(T->right)) + 1; return T; } ``` 上述代码提供了AVL的基本框架,包括节点定义、插入操作及必要的旋转方法。实际应用中可能还需要添加更多的功能,如删除节点、查找特定值等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值