-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7-6 Root of AVL Tree.cpp
105 lines (101 loc) · 2.1 KB
/
7-6 Root of AVL Tree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include<stdio.h>
#include<stdlib.h>
typedef struct AVLNode *Position ;
typedef Position AVLTree ;
typedef struct AVLNode{
int data;
AVLTree left;
AVLTree right;
int height;
};
int GetHeight(Position T);
AVLTree SingleRightRotation(AVLTree A);
AVLTree SingleLeftRotation(AVLTree A);
AVLTree Insert(AVLTree T,int x);
AVLTree DoubleRightLeftRotation(AVLTree A);
AVLTree DoubleLeftRightRotation(AVLTree A);
int Max(int a,int b);
int GetHeight(Position T)
{
if(T==NULL)return -1;
else return T->height;
}
int Max(int a,int b)
{
return a>b?a:b;
}
AVLTree Insert(AVLTree T,int x)
{
if(T==NULL)
{
T=(AVLTree)malloc(sizeof(struct AVLNode));
T->data=x;
T->height=1;
T->left=NULL;
T->right=NULL;
}
else if(x<T->data)
{
T->left=Insert(T->left,x);
if(GetHeight(T->left)-GetHeight(T->right)==2)
if(x<T->left->data)
T=SingleLeftRotation(T);
else
T=DoubleLeftRightRotation(T);
}
else if(x>T->data)
{
T->right=Insert(T->right,x);
if(GetHeight(T->left)-GetHeight(T->right)==-2)
if(x>T->right->data)
T=SingleRightRotation(T);
else
T=DoubleRightLeftRotation(T);
}
T->height=Max(GetHeight(T->left),GetHeight(T->right))+1;
return T;
}
AVLTree SingleLeftRotation(AVLTree A)
{
AVLTree B=A->left;
A->left=B->right;
//B->right=A;
A=B->right;
A->height=Max(GetHeight(A->left),GetHeight(A->right))+1;
B->height=Max(GetHeight(B->left),A->height)+1;
return B;
}
AVLTree SingleRightRotation(AVLTree A)
{
AVLTree B=A->right;
A->right=B->left;
//B->left=A;
A=B->left;
A->height=Max(GetHeight(A->left),GetHeight(A->right))+1;
B->height=Max(GetHeight(B->right),A->height)+1;
return B;
}
AVLTree DoubleLeftRightRotation(AVLTree A)
{
A->left=SingleRightRotation(A->left);
return SingleLeftRotation(A);
}
AVLTree DoubleRightLeftRotation(AVLTree A)
{
A->right=SingleLeftRotation(A->right);
return SingleRightRotation(A);
}
int main()
{
int n;
int num;
AVLTree T=NULL;
scanf("%d",&n);
while(n)
{
scanf("%d",&num);
T=Insert(T,num);
n--;
}
if(T!=NULL)printf("%d",T->data);
}