AVL树的删除操作同这篇文章http://blog.csdn.net/bdss58/article/details/41788243里的插入操作相似,具体实现步骤参考这篇文章。
两个步骤:
1)按照BST树的删除操作方法,删除AVL树中的摸个节点
2)“再平衡”
“再平衡”的详细介绍请看http://blog.csdn.net/bdss58/article/details/41788243
下面是C语言实现:
/*
* AVL tree deletion
*
* */
#include <stdio.h>
#include <stdlib.h>
// structure of tree node
struct node
{
int value;
struct node* left;
struct node* right;
int height;
};
// utility function to get node's height
int getHeight(struct node* n)
{
if(n==NULL)
return 0;
return n->height;
}
// get the bigger one between two number
int max(int a,int b)
{
return a>b?a:b;
}
// helper function allocate new node with given value
struct node* newNode(int value)
{
struct node* newnode=(struct node*)malloc(sizeof(struct node));
newnode->height=1;
newnode->left=NULL;
newnode->right=NULL;
newnode->value=value;
return newnode;
}
struct node* rightRotate(struct node* n)
{
struct node* nleft=n->left;
struct node* nleftright=nleft->right;
// rotate
nleft->right=n;
n->left=nleftright;
n->height=max(getHeight(n->left),getHeight(n->right))+1;
nleft->height=max(getHeight(nleft->left),getHeight(nleft->right))+1;
return nleft;
}
struct node* leftRotate(struct node* n)
{
struct node* nright=n->right;
struct node* nrightleft=nright->left;
// rotate
nright->left=n;
n->right=nrightleft;
n->height=max(getHeight(n->left),getHeight(n->right))+1;
nright->height=max(getHeight(nright->left),getHeight(nright->right))+1;
return nright;
}
// get balance factor of node n
int getBalance(struct node* n)
{
if (n==NULL)
return 0;
return getHeight(n->left)-getHeight(n->right);
}
// insert
struct node* insert(struct node* n,int value)
{
if(n==NULL)
return newNode(value);
if(n->value<value)
n->right=insert(n->right,value);
else
n->left=insert(n->left,value);
n->height=max(getHeight(n->left),getHeight(n->right))+1;
int balance=getBalance(n);
// left left case
if(balance>1 && value<n->left->value)
{
return rightRotate(n);
}
// left right case
if(balance>1 && value>n->left->value)
{
n->left=leftRotate(n->left);
return rightRotate(n);
}
// right right case
if(balance <-1 && value>n->right->value)
{
return leftRotate(n);
}
// right left case
if(balance <-1 && value<n->right->value)
{
n->right=rightRotate(n->right);
return leftRotate(n);
}
}
// given a binary search tree ,find the minum node
struct node* minNode(struct node* n)
{
struct node* current=node;
while(current->left!=NULL)
{
current=current->left;
}
return current;
}
struct node* deleteNode(struct node* root,int value)
{
if(root==NULL)
return root;
if(value<root->value)
{
deleteNode(root->left,value);
}
else if(value>root->value)
{
deleteNode(root->right,value);
}
else
{
if(root->left==NULL || root->right=NULL)
{
struct node* temp=root->left?root->left:root->right;
if(temp==NULL)
{
temp=root;
root=NULL;
}
else
{
*root=*temp;
}
free(temp);
}
else
{
struct node* temp=minNode(root->right);
root->value=temp->value;
root->right=deleteNode(root->right,temp->value);
}
}
if (root == NULL)
return root;
// STEP 2: UPDATE HEIGHT OF THE CURRENT NODE
root->height = max(height(root->left), height(root->right)) + 1;
// STEP 3: GET THE BALANCE FACTOR OF THIS NODE (to check whether
// this node became unbalanced)
int balance = getBalance(root);
// If this node becomes unbalanced, then there are 4 cases
// Left Left Case
if (balance > 1 && getBalance(root->left) >= 0)
return rightRotate(root);
// Left Right Case
if (balance > 1 && getBalance(root->left) < 0)
{
root->left = leftRotate(root->left);
return rightRotate(root);
}
// Right Right Case
if (balance < -1 && getBalance(root->right) <= 0)
return leftRotate(root);
// Right Left Case
if (balance < -1 && getBalance(root->right) > 0)
{
root->right = rightRotate(root->right);
return leftRotate(root);
}
return root;
}
int main(void)
{
printf("Hello World!\n");
return 0;
}