-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathAVLTree
More file actions
314 lines (306 loc) · 10.6 KB
/
AVLTree
File metadata and controls
314 lines (306 loc) · 10.6 KB
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#pragma once
#include<iostream>
#include<stdlib.h>
#include<vector>
using namespace std;
//节点定义
template<class K,class V>
struct AVLTreeNode
{
pair<K,V> _val;//节点,键值对
int _bf;//平衡因子
AVLTreeNode<K,V>* _left;//左子树
AVLTreeNode<K,V>* _right;//右子树
AVLTreeNode<K,V>* _parent;//双亲节点
AVLTreeNode(const pair<K,V>& val)
:_val(val),_bf(0)
,_left(nullptr),_right(nullptr),_parent(nullptr)
{}
};
template<class K,class V>
class AVLTree
{
typedef AVLTreeNode<K,V> Node;
private:
Node* root = nullptr;//根节点
public:
//构造函数
//析构函数
//插入--返回一个键值对,键值对的第一个值是插入节点的指针第二个值是bool值表示是否插入成功
pair<Node*,bool> insert(const pair<K,V>& val)
{ //1.找到该节点并插入
Node* newNode = new Node(val);
//如果是一颗空树,新插入节点就为跟节点
if(root == nullptr)
{
root = newNode;
return make_pair(root,true);
}
//找到插入位置
Node* parent = nullptr;//当前节点的父节点
Node* cur = root;
while(cur)
{
if((cur->_val).first < (newNode->_val).first)
{
//左走
parent = cur;
cur = cur->left;
}
else if((cur->_val).first > (newNode->_val).first)
{
//右走
parent = cur;
cur = cur->right;
}
else
{
//找到了,不需要插入,直接返回
return make_pair(cur,false);
}
}
//插入节点
if((parent->_val).first > (newNode->_val).first)
{
//插入到parent的左孩子节点处 parent->right = newNode;
newNode->_parent = parent;
}
//2.调整平衡因子
/*基本思路
* 从该节点的父节点开始调整,如果父节点的平衡因子变成了0,则停止调整
* 如果该节点的父节点的平衡因子不是0,则继续向上调整,直到某个节点的
* 平衡因子变成0或者调整到了根节点
* 调整平衡因子的策略是,如果插入的节点是该节点的左孩子节点则平衡因子-1
* 如果是该节点的右孩子节点,则平衡因子+1
*/
cur = newNode;
while(parent)
{
if(cur == parent->left)
{
//parent的平衡因子-1
parent->_bf--;
}
else
{
//parent的平衡因子+1
parent->_bf++;
}
//判断parent的平衡因子是否为0
if(parent->_bf == 0)
{
//调整完成
break;
}
else if(parent->bf == -1 || parent->bf == 1)
{ //继续向上调整
cur = parent;
parent = parent->_parent;
}
else
{
//已经不再是一颗AVL树,需要进行旋转
if(parent->_bf == -2)
{
if(parent->_left->_bf == -1)
{
//新插入的节点是较高的左子树的左孩子节点---右单旋
RotateR(parent);
}
else
{
//左右单旋
RotateLR(parent);
}
}
else if(parent->_bf == 2)
{
if(parent->_right->_bf == 1)
{
//左单旋
RotateL(parent);
}
else
{
//右左单旋
RotateRL(parent);
}
}
}
} return make_pair(cur,false);
}
//右单旋
void RotateR(Node* parent)
{
Node* parentL = parent->_left;//parent的左孩节点
Node* subR = parentL->_right;//parent的左孩子节点的右孩子节点
Node* parentParent = parent->parent;
//parent的左孩子节点指向parentL的右孩子节点
parentL = subR;
if(subR)
subR->_parent = parent;
//parent变成parentL的右孩子节点
subR = parent;
//parent的父节点变成parent的左孩子节点
parent->_parent = parentL;
//parent是根节点
if(parent == root)
{
root = parentL;
root->_parent = nullptr;
}
else
{
if(parent == parentParent->_left)
{
parentParent->_left = parentL;
parentL->_parent = parentParent;
}
else
{
parentParent->_right = parentL;
parentL->_parent = parentParent;
} }
parent->_bf = parentL->_bf = 0;
}
//左单旋
void RotateL(Node* parent)
{
Node* parentR = parent->_right;//parent的右孩子节点
Node* subRL = parentR->_left;//parent的右孩子的左孩子节点
Node* parentParent = parent->_parent;
//parent的右孩子节点指向parent的右孩子节点的左孩子节点
parentR = subRL;
if(subRL)
subRL->_parent = parentR;
//parent的右孩子节点的左孩子节点指向parent节点
subRL = parent;
//parent的父节点指向parent的右孩子节点
parent->_parent = parentR;
if(parent == root)
{
root = parentR;
root->_parent = nullptr;
}
else
{
if(parentParent->_left == parent)
{
parentParent->_left = parent;
}
else
{
parentParent->_right = parent;
} parent->_parent = parentParent;
}
parent->_bf = parentR->_bf = 0;
}
//左右单旋
void RotateLR(Node* parent)
{
Node* parentL = parent->_left;
Node* subLR = parentL->_right;
int subLRBf = subLR->_bf;
//左孩子节点进行左单旋
RotateL(parent->_left);
RotateR(parent);
//调整平衡因子
if(subLRBf == 1)
{
parent->_bf = subLR->_bf = 0;
parentL->_bf = -1;
}
else if(subLRBf == -1)
{
subLR->_bf = parentL->_bf = 0;
parent->_bf = -1;
}
else
{
parent->_bf = parentL->_bf = subLR->_bf = 0;
}
}
//右左单旋
void RotateRL(Node* parent)
{
Node* parentR = parent->_right;
Node* subRL = parentR->_left;
int subRLBf = subRL->_bf;
//该节点的右孩子进行右旋转
RotateR(parent->_right);
//该节点进行左旋转
RotateL(parent);
//调整平衡因子
if(subRLBf == 1)
{
parentR->_bf = subRL->_bf = 0;
parent->_bf = -1;
}
else if(subRLBf == -1)
{
parentR->_bf = subRL->_bf = 0;
parent->_bf = 1;
}
else
{
parentR->_bf = subRL->_bf = parent->_bf = 0;
}
}
//中序遍历AVL树
static void BSTreeInOrder(Node* node,vector<pair<K,V>>& inOrder)
{
//inOrder是输出型参数,将遍历结果保存到该数组中
if(node == nullptr)
return;
BSTreeInOrder(node->_left,inOrder);
inOrder.push_back(node->_val);
BSTreeInOrder(node->_right,inOrder);
}
bool isBSTree()
{
vector<pair<K,V>> inOrder; BSTreeInOrder(root,inOrder);
if(inOrder.empty())
return true;
//遍历,检查是否有序
pair<K,V> tmp = inOrder[0];
for(int i = 1;i < inOrder.size();i++)
{
if(tmp.first < inOrder[i].first)
{
tmp = inOrder[i];
}
else
return false;
}
}
//二叉树的高度
static int BSTreeHeight(Node* node)
{
if(node == nullptr)
return 0;
int left = BSTreeHeight(node->_left);
int right = BSTreeHeight(node->_right);
return left>right?(left+1):(right+1);
}
//判断是否平衡
static bool _isBalance(Node* root)
{
//求左右子树的高度
int left = BSTreeHeight(root->_left);
int right = BSTreeHeight(root->_right);
if(abs(left-right) <= 1)
{
return _isBalance(root->_left) && _isBalance(root->_right); }
else
return false;
}
//验证AVL树
bool isBalance()
{
if(root == nullptr)
return true;
if(isBSTree() && _isBalance())
return true;
return false;
}
}