1. 概述
在AVL樹中任何節(jié)點(diǎn)的兩個子樹的高度最大差別為一,所以它也被稱為高度平衡樹。AVL樹查找、插入和刪除在平均和最壞情況下都是O(log n),增加和刪除可能需要通過一次或多次樹旋轉(zhuǎn)來重新平衡這個樹。本文介紹了AVL樹的設(shè)計思想和基本操作。
2. 基本術(shù)語
有四種種情況可能導(dǎo)致二叉查找樹不平衡,分別為:
- LL:插入一個新節(jié)點(diǎn)到根節(jié)點(diǎn)的左子樹的左子樹,導(dǎo)致根節(jié)點(diǎn)的平衡因子由1變?yōu)?
- RR:插入一個新節(jié)點(diǎn)到根節(jié)點(diǎn)的右子樹的右子樹,導(dǎo)致根節(jié)點(diǎn)的平衡因子由-1變?yōu)?2
- LR:插入一個新節(jié)點(diǎn)到根節(jié)點(diǎn)的左子樹的右子樹,導(dǎo)致根節(jié)點(diǎn)的平衡因子由1變?yōu)?
- RL:插入一個新節(jié)點(diǎn)到根節(jié)點(diǎn)的右子樹的左子樹,導(dǎo)致根節(jié)點(diǎn)的平衡因子由-1變?yōu)?2
針對四種種情況可能導(dǎo)致的不平衡,可以通過旋轉(zhuǎn)使之變平衡。有兩種基本的旋轉(zhuǎn):
(1)左旋轉(zhuǎn):將根節(jié)點(diǎn)旋轉(zhuǎn)到(根節(jié)點(diǎn)的)右孩子的左孩子位置
(2)右旋轉(zhuǎn):將根節(jié)點(diǎn)旋轉(zhuǎn)到(根節(jié)點(diǎn)的)左孩子的右孩子位置
3. AVL樹的旋轉(zhuǎn)操作
AVL樹的基本操作是旋轉(zhuǎn),有四種旋轉(zhuǎn)方式,分別為:
左旋轉(zhuǎn),右旋轉(zhuǎn),左右旋轉(zhuǎn)(先左后右),右左旋轉(zhuǎn)(先右后左).
實(shí)際上,這四種旋轉(zhuǎn)操作兩兩對稱,因而也可以說成兩類旋轉(zhuǎn)操作。
LL情況
LL情況需要右旋解決,如下圖所示:
T1, T2, T3 and T4 are subtrees.
z y
/ \ / \
y T4 Right Rotate (z) x z
/ \ - - - - - - - - -> / \ / \
x T3 T1 T2 T3 T4
/ \
T1 T2
Node_t RightRotate(Node_t a) {
b = a->left;
a->left = b->right;//將根節(jié)點(diǎn)指向左子樹的右節(jié)點(diǎn)
b->right = a; //左子樹的右節(jié)點(diǎn)指向根節(jié)點(diǎn)
//重新調(diào)整高度
a->height = Max(Height(a->left), Height(a->right));
b->height = Max(Height(b->left), Height(b->right));
return b;
}
RR情況
RR情況需要左旋解決,如下圖所示:
z y
/ \ / \
T1 y Left Rotate(z) z x
/ \ - - - - - - - -> / \ / \
T2 x T1 T2 T3 T4
/ \
T3 T4
代碼為:
Node_t LeftRotate(Node_t a) {
b = a->right;
a->right = b->left; //先將根的右子樹指向右子樹的左節(jié)點(diǎn)
b->left = a;//再將的根的左子樹指向根
//重新計算高度
a->height = Max(Height(a->left), Height(a->right));
b->height = Max(Height(b->left), Height(b->right));
return b;
}
LR情況
LR情況需要左右(先B左旋轉(zhuǎn),后A右旋轉(zhuǎn))旋解決:
z z x
/ \ / \ / \
y T4 Left Rotate (y) x T4 Right Rotate(z) y z
/ \ - - - - - - - - -> / \ - - - - - - - -> / \ / \
T1 x y T3 T1 T2 T3 T4
/ \ / \
T2 T3 T1 T2
代碼為:
Node_t LeftRightRotate(Node_t a) {
a->left = LeftRotate(a->left);
return RightRotate(a);
}
RL情況
RL情況需要右左旋解決(先B右旋轉(zhuǎn),后A左旋轉(zhuǎn)):
z z x
/ \ / \ / \
T1 y Right Rotate (y) T1 x Left Rotate(z) z y
/ \ - - - - - - - - -> / \ - - - - - - - -> / \ / \
x T4 T2 y T1 T2 T3 T4
/ \ / \
T2 T3 T3 T4
代碼為:
Node_t RightLeftRotate(Node_t a) {
a->right = RightRotate(a->right);
return LeftRotate(a);
}
#include<stdio.h>
#include<stdlib.h>
// An AVL tree node
struct node
{
int key;
struct node *left;
struct node *right;
int height;
};
// A utility function to get maximum of two integers
int max(int a, int b);
// A utility function to get height of the tree
int height(struct node *N)
{
if (N == NULL)
return 0;
return N->height;
}
// A utility function to get maximum of two integers
int max(int a, int b)
{
return (a > b)? a : b;
}
/* Helper function that allocates a new node with the given key and
NULL left and right pointers. */
struct node* newNode(int key)
{
struct node* node = (struct node*)
malloc(sizeof(struct node));
node->key = key;
node->left = NULL;
node->right = NULL;
node->height = 1; // new node is initially added at leaf
return(node);
}
// A utility function to right rotate subtree rooted with y
// See the diagram given above.
struct node *rightRotate(struct node *y)
{
struct node *x = y->left;
struct node *T2 = x->right;
// Perform rotation
x->right = y;
y->left = T2;
// Update heights
y->height = max(height(y->left), height(y->right))+1;
x->height = max(height(x->left), height(x->right))+1;
// Return new root
return x;
}
// A utility function to left rotate subtree rooted with x
// See the diagram given above.
struct node *leftRotate(struct node *x)
{
struct node *y = x->right;
struct node *T2 = y->left;
// Perform rotation
y->left = x;
x->right = T2;
// Update heights
x->height = max(height(x->left), height(x->right))+1;
y->height = max(height(y->left), height(y->right))+1;
// Return new root
return y;
}
// Get Balance factor of node N
int getBalance(struct node *N)
{
if (N == NULL)
return 0;
return height(N->left) - height(N->right);
}
struct node* insert(struct node* node, int key)
{
/* 1. Perform the normal BST rotation */
if (node == NULL)
return(newNode(key));
if (key < node->key)
node->left = insert(node->left, key);
else
node->right = insert(node->right, key);
/* 2. Update height of this ancestor node */
node->height = max(height(node->left), height(node->right)) + 1;
/* 3. Get the balance factor of this ancestor node to check whether
this node became unbalanced */
int balance = getBalance(node);
// If this node becomes unbalanced, then there are 4 cases
// Left Left Case
if (balance > 1 && key < node->left->key)
return rightRotate(node);
// Right Right Case
if (balance < -1 && key > node->right->key)
return leftRotate(node);
// Left Right Case
if (balance > 1 && key > node->left->key)
{
node->left = leftRotate(node->left);
return rightRotate(node);
}
// Right Left Case
if (balance < -1 && key < node->right->key)
{
node->right = rightRotate(node->right);
return leftRotate(node);
}
/* return the (unchanged) node pointer */
return node;
}
// A utility function to print preorder traversal of the tree.
// The function also prints height of every node
void preOrder(struct node *root)
{
if(root != NULL)
{
printf("%d ", root->key);
preOrder(root->left);
preOrder(root->right);
}
}
/* Drier program to test above function*/
int main()
{
struct node *root = NULL;
/* Constructing tree given in the above figure */
root = insert(root, 10);
root = insert(root, 20);
root = insert(root, 30);
root = insert(root, 40);
root = insert(root, 50);
root = insert(root, 25);
/* The constructed AVL Tree would be
30
/ \
20 40
/ \ \
10 25 50
*/
printf("Pre order traversal of the constructed AVL tree is \n");
preOrder(root);
return 0;
}