#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
typedef struct AVLTreeNode
{
int key; //数据
int height;
AVLTreeNode *left;
AVLTreeNode *right;
} Node, *AVLTree;
int Height(AVLTree tree);
void inorder_avltree(AVLTree tree);
void Search(AVLTree tree,int data);
void print_avltree(AVLTree tree, int data, int direction);
Node* avltree_search(AVLTree x, int data);
Node* avltree_maximum(AVLTree tree);
Node* avltree_insert(AVLTree tree, int data);
Node* avltree_delete(AVLTree tree, int data);
Node* avltree_create_node(int data, Node *left, Node* right);
void destroy_avltree(AVLTree tree);// 销毁AVL树
int main()
{
int i;
int data;
AVLTree root=NULL;
data=3;
cout<<“插入 “<<data<<endl;
root = avltree_insert(root,data);
data=5;
cout<<“插入 “<<data<<endl;
root = avltree_insert(root,data);
data=2;
cout<<“插入 “<<data<<endl;
root = avltree_insert(root,data);
i = 3;
cout<<“删除节点: “<<i<<endl;
root = avltree_delete(root, i);
print_avltree(root, root->key, 0);
data=20;
cout<<“插入 “<<data<<endl;
root = avltree_insert(root,data);
int flag=1 ;
cout<<“查找 “<<flag<<endl;
Search(root,flag);
data=10;
cout<<“插入 “<<data<<endl;
root = avltree_insert(root,data);
i=5;
cout<<“删除节点: “<<i<<endl;
root = avltree_delete(root, i);
print_avltree(root, root->key, 0);
cout<<“中序遍历: “<<endl;
inorder_avltree(root);
cout<<endl;
cout<<“树的详细信息: “<<endl;
print_avltree(root, root->key, 0);
destroy_avltree(root);
}
int Height(AVLTree tree)
{
if(tree==NULL)
return -1;
else
return tree->height;
}
void Search(AVLTree tree,int data)
{
while(tree!=NULL)
{
if(tree->key==data)
{
cout<<data<<“在AVL树中.”<<endl;
return;
}
else if(data<tree->key)
tree=tree->left;
else
tree=tree->right;
}
cout<<data<<“不在AVL树中.”<<endl;
}
void inorder_avltree(AVLTree tree) //中序遍历AVL树
{
if(tree != NULL)
{
inorder_avltree(tree->left);
cout<<tree->key<<” “;
inorder_avltree(tree->right);
}
}
Node* avltree_search(AVLTree x, int data) //递归查找节点
{
if (x==NULL || x->key==data)
return x;
if (data < x->key)
return avltree_search(x->left,data);
else
return avltree_search(x->right,data);
}
Node* avltree_maximum(AVLTree tree) //最大结点
{
if (tree == NULL)
return NULL;
while(tree->right != NULL)
tree = tree->right;
return tree;
}
Node* avltree_minimum(AVLTree tree) //最小结点
{
if (tree == NULL)
return NULL;
while(tree->left != NULL)
tree = tree->left;
return tree;
}
Node* left_left_rotation(AVLTree k2) //LL旋转
{
AVLTree k1;
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;
}
Node* right_right_rotation(AVLTree k1) //RR旋转
{
AVLTree k2;
k2 = k1->right;
k1->right = k2->left;
k2->left = k1;
k1->height = max(Height(k1->left), Height(k1->right)) + 1;
k2->height = max(Height(k2->right), k1->height) + 1;
return k2;
}
Node* left_right_rotation(AVLTree k3) //LR旋转
{
k3->left = right_right_rotation(k3->left);
return left_left_rotation(k3);
}
Node* right_left_rotation(AVLTree k1) //RL旋转
{
k1->right = left_left_rotation(k1->right);
return right_right_rotation(k1);
}
Node* avltree_create_node(int data, Node *left, Node* right)
{
Node* p;
if ((p = (Node *)malloc(sizeof(Node))) == NULL)
return NULL;
p->key = data;
p->height = 0;
p->left = left;
p->right = right;
return p;
}
Node* avltree_insert(AVLTree tree, int data)//插入结点
{
if (tree == NULL)
{
tree = avltree_create_node(data, NULL, NULL);// 新建节点
}
else if (data < tree->key) // 应该将key插入到”tree的左子树”的情况
{
tree->left = avltree_insert(tree->left, data);
if (Height(tree->left) – Height(tree->right) == 2)
{
if (data < tree->left->key)
tree = left_left_rotation(tree);
else
tree = left_right_rotation(tree);
}
}
else if (data > tree->key) // 应该将key插入到”tree的右子树”的情况
{
tree->right = avltree_insert(tree->right, data);
if (Height(tree->right) – Height(tree->left) == 2)
{
if (data > tree->right->key)
tree = right_right_rotation(tree);
else
tree = right_left_rotation(tree);
}
}
else
{
cout<<“添加失败:不允许添加相同的节点!”;
}
tree->height = max(Height(tree->left),Height(tree->right)) + 1;
return tree;
}
Node* delete_node(AVLTree tree, Node *z)//删除结点
{
if (tree==NULL || z==NULL)
return NULL;
if (z->key < tree->key) // 待删除的节点在”tree的左子树”中
{
tree->left = delete_node(tree->left, z);
if (Height(tree->right) – Height(tree->left) == 2)
{
Node *r = tree->right;
if (Height(r->left) > Height(r->right))
tree = right_left_rotation(tree);
else
tree = right_right_rotation(tree);
}
}
else if (z->key > tree->key)// 待删除的节点在”tree的右子树”中
{
tree->right = delete_node(tree->right, z);
if (Height(tree->left) – Height(tree->right) == 2)
{
Node *l = tree->left;
if (Height(l->right) > Height(l->left))
tree = left_right_rotation(tree);
else
tree = left_left_rotation(tree);
}
}
else
{
if ((tree->left) && (tree->right))
{
if (Height(tree->left) > Height(tree->right))
{
// 删除左边的节点
Node *max = avltree_maximum(tree->left);
tree->key = max->key;
tree->left = delete_node(tree->left, max);
}
else
{
//删除右边的节点
Node *min = avltree_minimum(tree->right);
tree->key = min->key;
tree->right = delete_node(tree->right, min);
}
}
else
{
Node *tmp = tree;
tree = tree->left ? tree->left : tree->right;
free(tmp);
}
}
return tree;
}
Node* avltree_delete(AVLTree tree, int data)
{
Node *z;
if ((z = avltree_search(tree,data)) != NULL)
tree = delete_node(tree, z);
return tree;
}
void destroy_avltree(AVLTree tree)//销毁AVL树
{
if (tree==NULL)
return ;
if (tree->left != NULL)
destroy_avltree(tree->left);
if (tree->right != NULL)
destroy_avltree(tree->right);
free(tree);
}
void print_avltree(AVLTree tree, int data, int direction)//输出AVL树
{
if(tree != NULL)
{
if(direction==0) // tree是根节点
cout<<” “<<tree->key<<” is root”<<endl;//, , key);
else // tree是分支节点
printf(“%2d is %2d’s %6s child\n”, tree->key, data, direction==1?”right” : “left”);
print_avltree(tree->left, tree->key, -1);
print_avltree(tree->right,tree->key, 1);
}
}