-
Notifications
You must be signed in to change notification settings - Fork 0
/
AVL_Tree.cpp
285 lines (281 loc) · 4.68 KB
/
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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include <iostream>
using namespace std;
template <typename T>
struct AVLTreeNode
{
AVLTreeNode<T>* parent;
AVLTreeNode<T>* left;
AVLTreeNode<T>* right;
T value;
int bf;
AVLTreeNode(const T&aimvalue=T())
:left(nullptr)
,right(nullptr)
,parent(nullptr)
,value(aimvalue)
,bf(0)
{}
};
template <typename T>
class AVLTree
{
typedef AVLTreeNode<T> Node;
public:
Node* root;
AVLTree() :root(nullptr) {}
~AVLTree() { Destroy(root); };
void Destroy(Node* &root);
void RotateLeft(Node* par);
void RotateRight(Node* par);
void RotateLR(Node* par);
void RotateRL(Node* par);
bool Insert(const T& aimvalue);
void Inorder(Node* root);
int Height(Node* root);
bool IsAVLTree(Node* root);
};
template<typename T>
void AVLTree<T>::Destroy(Node* &root)
{
if (root)
{
Destroy(root->left);
Destroy(root->right);
delete root;
root = nullptr;
}
}
template<typename T>
void AVLTree<T>::RotateLeft(Node* par)
{
Node* subR = par->right;
Node* subRL = subR->left;
Node* ppar = par->parent;
par->right = subRL;
if (subRL)
{
subRL->parent = par;
}
subR->left = par;
par->parent = subR;
subR->parent = ppar;
if (ppar == nullptr)
{
root = subR;
}
else
{
if (ppar->left == par)
{
ppar->left = subR;
}
else
{
ppar->right = subR;
}
}
par->bf = 0;
subR->bf = 0;
}
template<typename T>
void AVLTree<T>::RotateRight(Node* par)
{
Node* subL = par->left;
Node* subLR = par->right;
Node* ppar = par->parent;
par->left = subLR;
if (subLR)
{
subLR->parent = par;
}
subL->right = par;
par->parent = subL;
subL->parent = ppar;
if (ppar == nullptr)
{
root = subL;
}
else
{
if (ppar->left == par)
{
ppar->left = subL;
}
else
{
ppar->right = subL;
}
}
par->bf = 0;
subL->bf = 0;
}
template<typename T>
void AVLTree<T>::RotateLR(Node* par)
{
Node* subL = par->left;
Node* subLR = subL->right;
int BF = subLR->bf;//subLR的平衡因子
RotateLeft(subL);
RotateRight(par);
if (BF == -1)
{
par->bf = 1;
}
else
{
par->bf = -1;
}
}
template<typename T>
void AVLTree<T>::RotateRL(Node* par)
{
Node* subR = par->right;
Node* subRL = par->left;
int BF = subRL->bf;
RotateRight(subR);
RotateLeft(par);
if (BF == -1)
{
subR->bf = 1;
}
else
{
subR->bf = -1;
}
}
template <typename T>
bool AVLTree<T>::Insert(const T& aimvalue)
{
if (root == nullptr)
{
root = new Node(aimvalue);
return true;
}
Node* cur = root;
Node* par = nullptr;
while (cur)
{
par = cur;
if (aimvalue < cur->value)
{
cur = cur->left;
}
else if (aimvalue > cur->value)
{
cur = cur->right;
}
else
{
return false;
}
}
cur = new Node(aimvalue);
if (aimvalue < par->value)
{
par->left = cur;
}
else
{
par->right = cur;
}
cur->parent = par;
while (par)
{
if (cur == par->left)
{
par->bf--;
}
else
{
par->bf++;
}
if (par->bf == 0)
{
break;
}
else if (par->bf == 1 || par->bf == -1)
{
cur = par;
par = cur->parent;
}
else//可能需要双旋调整
{
if (par->bf == 2)
{
cur->bf == 1 ? RotateLeft(par) : RotateRL(par);
}
else
{
cur->bf == -1 ? RotateRight(par) : RotateLR(par);
}
break;
}
}
return true;
}
template <typename T>
void AVLTree<T>::Inorder(Node* root)
{
if (root)
{
Inorder(root->left);
cout << root->value<<" ";
Inorder(root->right);
}
}
template<typename T>
int AVLTree<T>::Height(Node *root)
{
if (root == nullptr)
{
return 0;
}
int leftHeight = Height(root->left);
int rightHeight = Height(root->right);
return leftHeight > rightHeight ? (leftHeight + 1) : (rightHeight + 1);
}
template <typename T>
bool AVLTree<T>::IsAVLTree(Node *root)
{
if (root == nullptr)
{
return true;
}
int leftHeight = Height(root->left);
int rightHeight = Height(root->right);
if (abs(rightHeight - leftHeight) > 1 || rightHeight - leftHeight != root->bf)
{
cout << "Wrong!" << endl << "结点值:" << root->value << "结点平衡因子:" << root->bf << endl;
return false;
}
return IsAVLTree(root->left) && IsAVLTree(root->right);
}
int main()
{
int n;
cout << "请输入元素个数" << endl;
cin >> n;
int* arr = new int[n];
cout << "请输入元素" << endl;
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
cout << "Constructing..."<<endl;
AVLTree<int> avl;
for (int i = 0; i < n; i++)
{
avl.Insert(arr[i]);
avl.Inorder(avl.root);
cout << endl;
}
if (avl.IsAVLTree(avl.root))
{
cout << "AVL Construction Success" << endl;
}
else
{
cout << "AVL Construction Failed" << endl;
}
return 0;
}