-
Notifications
You must be signed in to change notification settings - Fork 0
/
1066.cpp
205 lines (194 loc) · 2.7 KB
/
1066.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// 参考《大话数据结构》中AVL的基本操作
// 查阅了网上其他人的代码,没发现除了建立AVL树之外的解法
#include <iostream>
using namespace std;
typedef struct node
{
int data;
int bf; // 节点平衡因子
struct node *lchild, *rchild;
}node;
#define LH 1
#define EH 0
#define RH -1
void R_Rotate(node *&T)
{
node *L = T->lchild;
T->lchild = L->rchild;
L->rchild = T;
T = L;
}
void L_Rotate(node *&T)
{
node *R = T->rchild;
T->rchild = R->lchild;
R->lchild = T;
T = R;
}
void LeftBalance(node *&T)
{
node *L, *Lr;
L = T->lchild;
switch(L->bf)
{
// 新节点插入在T的左孩子的左子树上
case LH:
T->bf = L->bf = EH;
R_Rotate(T);
break;
// 新节点插入在T的左孩子的右子树上
case RH:
Lr = L->rchild;
switch(Lr->bf)
{
case LH:
T->bf = RH;
L->bf = EH;
break;
case EH:
T->bf = L->bf = EH;
break;
case RH:
T->bf = EH;
L->bf = LH;
break;
}
Lr->bf = EH;
L_Rotate(T->lchild);
R_Rotate(T);
}
}
void RightBalance(node *&T)
{
node *R, *Rl;
R = T->rchild;
switch (R->bf)
{
// 插在T右孩子的右节点上
case RH:
T->bf = R->bf = EH;
L_Rotate(T);
break;
// 插在T右孩子的左节点上
case LH:
Rl = R->lchild;
switch (Rl->bf)
{
case LH:
T->bf = EH;
R->bf = RH;
break;
case EH:
T->bf = EH;
R->bf = EH;
break;
case RH:
T->bf = LH;
R->bf = EH;
break;
}
Rl->bf = EH;
R_Rotate(T->rchild);
L_Rotate(T);
}
}
bool insertAVL(node *&T, int key, bool &taller)
{
if (NULL == T)
{
T = new node;
T->lchild = T->rchild = NULL;
T->data = key;
T->bf = EH;
taller = true;
}
else
{
if (key == T->data)
{
taller = false;
return false;
}
if (key < T->data)
{
if (!insertAVL(T->lchild, key, taller))
{
return false;
}
if (taller)
{
switch(T->bf)
{
case LH:
LeftBalance(T);
taller = false;
break;
case EH:
T->bf = LH;
taller = true;
break;
case RH:
T->bf = EH;
taller = false;
break;
}
}
}
else
{
if (!insertAVL(T->rchild, key, taller))
{
return false;
}
if (taller)
{
switch(T->bf)
{
case LH:
T->bf = EH;
taller = false;
break;
case EH:
T->bf = RH;
taller = true;
break;
case RH:
RightBalance(T);
taller = false;
break;
}
}
}
}
return true;
}
void destoryAVL(node *&T)
{
if (NULL != T->lchild)
{
destoryAVL(T->lchild);
}
if (NULL != T->rchild)
{
destoryAVL(T->rchild);
}
delete T;
}
int main()
{
int N;
int tmp;
node *bitree = NULL;
bool flag;
scanf("%d", &N);
for (int i = 0; i != N; ++i)
{
scanf("%d", &tmp);
insertAVL(bitree, tmp, flag);
}
printf("%d\n", bitree->data);
destoryAVL(bitree);
bitree = NULL;
system("pause");
return 0;
}