AVL

AVL Tree

AVL Tree使用高度差作为平衡因子,他要求兄弟的高度差的绝对值不超过1 ## code
avl Tree代码
treeview raw
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
#pragma once
#include "../memery_management/memery_pool.h"
#include "search_tree.h"

namespace data_structure {
template <class T>
class avl_tree : public search_tree<T> {
struct node {
node *ls, *rs;
int hight;
T key;
};
memery_pool<node> pool;
node* root = nullptr;

void copy_self(node*& rt, node* cp) {
if (cp == nullptr) return;
rt = pool.get();
rt->key = cp->key;
rt->hight = cp->hight;
copy_self(rt->ls, cp->ls);
copy_self(rt->rs, cp->rs);
}
void delete_self(node* rt) {
if (rt == nullptr) return;
delete_self(rt->ls);
delete_self(rt->rs);
pool.erase(rt);
}

node* newnode(const T& w) {
node* res = pool.get();
res->ls = res->rs = nullptr;
res->hight = 1;
res->key = w;
return res;
}
void rotate(node*& rt, int l) {
node* cur = rt;
if (l) {
rt = rt->ls;
cur->ls = rt->rs;
rt->rs = cur;
} else {
rt = rt->rs;
cur->rs = rt->ls;
rt->ls = cur;
}
}
// static int max(int a, int b) { return a > b ? a : b; }
inline int getlh(node*& rt) { return rt->ls == nullptr ? 0 : rt->ls->hight; }
inline int getrh(node*& rt) { return rt->rs == nullptr ? 0 : rt->rs->hight; }
inline int getdis(node*& rt) { return getlh(rt) - getrh(rt); }
inline int max(int a, int b) { return a < b ? b : a; }
inline void pushup(node*& rt) { rt->hight = max(getlh(rt), getrh(rt)) + 1; }
void maintain(node*& rt) {
if (rt == nullptr) return;
if (getdis(rt) == 2) {
if (getdis(rt->ls) < 0) rotate(rt->ls, 0);
rotate(rt, 1);
} else if (getdis(rt) == -2) { // 这里不能写if
if (getdis(rt->rs) > 0) rotate(rt->rs, 1);
rotate(rt, 0);
}
if (rt->ls != nullptr) pushup(rt->ls);
if (rt->rs != nullptr) pushup(rt->rs);
pushup(rt);
}
void insert(node*& rt, const T& w) {
if (rt == nullptr) {
rt = newnode(w);
} else if (w < rt->key) {
insert(rt->ls, w);
} else if (rt->key < w) {
insert(rt->rs, w);
}
maintain(rt);
}
node*& search(node*& rt, const T& w) {
if (rt == nullptr)
return rt;
else if (w < rt->key)
return search(rt->ls, w);
else if (rt->key < w)
return search(rt->rs, w);
else
return rt;
}
void erase(node*& rt, const T& w) {
if (rt == nullptr) {
return;
} else if (w < rt->key) {
erase(rt->ls, w);
} else if (rt->key < w) {
erase(rt->rs, w);
} else {
node* cur = rt;
if (rt->ls == nullptr) {
rt = rt->rs;
pool.erase(cur);
} else if (rt->rs == nullptr) {
rt = rt->ls;
pool.erase(cur);
} else {
cur = cur->rs;
while (cur->ls != nullptr) cur = cur->ls;
rt->key = cur->key;
erase(rt->rs, cur->key);
}
}
maintain(rt);
}
void preorder(node*& rt, void (*f)(const T&)) {
if (rt == nullptr) return;
f(rt->key);
preorder(rt->ls, f);
preorder(rt->rs, f);
}
void midorder(node*& rt, void (*f)(const T&)) {
if (rt == nullptr) return;
midorder(rt->ls, f);
f(rt->key);
midorder(rt->rs, f);
assert(abs(getdis(rt)) <= 1);
}

public:
// 构造函数和析构函数
avl_tree() { root = nullptr; }
avl_tree(const avl_tree<T>& rhs) { copy_self(root, rhs.root); }
avl_tree<T> operator=(const avl_tree<T>& rhs) {
delete_self(root);
copy_self(root, rhs.root);
return *this;
}
~avl_tree() { delete_self(root); }

//普通操作
void insert(const T& w) { insert(root, w); }
node*& search(const T& w) { return search(root, w); }
void erase(const T& w) { erase(root, w); }
void preorder(void (*f)(const T&)) { preorder(root, f); }
void midorder(void (*f)(const T&)) { midorder(root, f); }
};
} // namespace data_structure