题目链接
AVL-维基百科
/* Node is defined as :
typedef struct node
{
int val;
struct node* left;
struct node* right;
int ht;
} node; */
int Height(node *T)
{
if (!T) return -1; //In this question,NULL'height is -1
return T->ht;
}
int Max(int a, int b)
{
return a > b ? a : b;
}
node *SingleLeft(node *k1) //单旋(RR)
{
node *k2;
k2 = k1->right;
k1->right = k2->left;
k2->left = k1;
k1->ht = Max(Height(k1->left), Height(k1->right)) + 1;
k2->ht = Max(Height(k2->left), Height(k2->right)) + 1;
return k2;
}
node *SingleRight(node *k1) //单旋(LL)
{
node *k2;
k2 = k1->left;
k1->left = k2->right;
k2->right = k1;
k1->ht = Max(Height(k1->left), Height(k1->right)) + 1;
k2->ht = Max(Height(k2->left), Height(k2->right)) + 1;
return k2;
}
node *LeftRight(node *k1) //双旋(LR)
{
k1->left = SingleLeft(k1->left);
return SingleRight(k1);
}
node *RightLeft(node *k1) //双旋(RL)
{
k1->right = SingleRight(k1->right);
return SingleLeft(k1);
}
int BF(node *p)
{
return Height(p->left) - Height(p->right);
}
node *insert(node *root, int val)
{
if (!root)
{
root = (node *)malloc(sizeof(node));
root->val = val;
root->left = root->right = NULL;
root->ht = 0; //Leaf's height is 0
}
else
{
if (root->val < val)
{
root->right = insert(root->right, val);
root->ht = Max(Height(root->left), Height(root->right)) + 1;
if (BF(root) == -2)
{
if (val < root->right->val) //RL
{
root = RightLeft(root);
}
if (val > root->right->val) //RR
{
root = SingleLeft(root);
}
}
}
if (root->val > val)
{
root->left = insert(root->left, val);
root->ht = Max(Height(root->left), Height(root->right)) + 1;
if (BF(root) == 2)
{
if (val > root->left->val) //LR
{
root = LeftRight(root);
}
if (val < root->left->val) //LL
{
root = SingleRight(root);
}
}
}
}
return root;
}
完整版
#include<stdio.h> //Inserting of AVL tree
#include<stdlib.h>
#include<string.h>
typedef struct node
{
int val;
struct node *left;
struct node *right;
int ht; //Height
}node;
int Height(node *T)
{
if (!T) return 0;
return T->ht;
}
int Max(int a, int b)
{
return a > b ? a : b;
}
node *SingleLeft(node *k1) //单旋(RR)
{
node *k2;
k2 = k1->right;
k1->right = k2->left;
k2->left = k1;
k1->ht = Max(Height(k1->left), Height(k1->right)) + 1;
k2->ht = Max(Height(k2->left), Height(k2->right)) + 1;
return k2;
}
node *SingleRight(node *k1) //单旋(LL)
{
node *k2;
k2 = k1->left;
k1->left = k2->right;
k2->right = k1;
k1->ht = Max(Height(k1->left), Height(k1->right)) + 1;
k2->ht = Max(Height(k2->left), Height(k2->right)) + 1;
return k2;
}
node *LeftRight(node *k1) //双旋(LR)
{
k1->left = SingleLeft(k1->left);
return SingleRight(k1);
}
node *RightLeft(node *k1) //双旋(RL)
{
k1->right = SingleRight(k1->right);
return SingleLeft(k1);
}
int BF(node *p)
{
return (Height(p->left) - Height(p->right));
}
node *insert(node *root, int val)
{
if (!root)
{
root = (node *)malloc(sizeof(node));
root->val = val;
root->left = root->right = NULL;
root->ht = 1;
}
else
{
if (root->val < val)
{
root->right = insert(root->right, val);
root->ht = Max(Height(root->left), Height(root->right)) + 1;
if (BF(root) == -2)
{
if (val < root->right->val) //RL
{
root = RightLeft(root);
}
if (val > root->right->val) //RR
{
root = SingleLeft(root);
}
}
}
if (root->val > val)
{
root->left = insert(root->left, val);
root->ht = Max(Height(root->left), Height(root->right)) + 1;
if (BF(root) == 2)
{
if (val > root->left->val) //LR
{
root = LeftRight(root);
}
if (val < root->left->val) //LL
{
root = SingleRight(root);
}
}
}
}
return root;
}
node *Create()
{
node *T;
int val;
T = NULL;
scanf("%d", &val);
while (val != -1)
{
T = insert(T, val);
scanf("%d", &val);
}
return T;
}
void Inorder(node *T)
{
if (T)
{
Inorder(T->left);
printf("%d ", T->val);
printf("%d ", BF(T));
Inorder(T->right);
}
}
void Preorder(node *T)
{
if (T)
{
printf("%d ", T->val);
printf("%d ", BF(T));
Preorder(T->left);
Preorder(T->right);
}
}
int main()
{
node *T;
T = Create(); //test
Inorder(T);
printf("\n");
Preorder(T);
printf("\n");
return 0;
}