-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavl_tree.cpp
218 lines (192 loc) · 4.09 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
#include<bits/stdc++.h>
using namespace std;
struct Node {
int key, val;
int height, cnt;
Node *l, *r;
Node() {}
Node(int k, int v) : key(k), val(v), height(1), cnt(1), l(NULL), r(NULL) {}
};
typedef Node* pnode;
inline int getHeight(pnode root) {
return (root) ? root->height : 0;
}
inline void updateHeight(pnode root) {
if (root) {
root->height = max(getHeight(root->l), getHeight(root->r)) + 1;
}
}
inline int getCnt(pnode root) {
return (root) ? root->cnt : 0;
}
inline void updateCnt(pnode root) {
if (root) {
root->cnt = getCnt(root->l) + getCnt(root->r) + 1;
}
}
inline pnode rRotate(pnode y) {
pnode x = y->l;
pnode t = x->r;
x->r = y;
y->l = t;
updateHeight(y);
updateHeight(x);
updateCnt(y);
updateCnt(x);
return x;
}
inline pnode lRotate(pnode y) {
pnode x = y->r;
pnode t = x->l;
x->l = y;
y->r = t;
updateHeight(y);
updateHeight(x);
updateCnt(y);
updateCnt(x);
return x;
}
inline pnode smallest(pnode root) {
if (!root) {
return root;
}
while (root->l) {
root = root->l;
}
return root;
}
inline int getBalFactor(pnode root) {
if (root) {
return getHeight(root->l) - getHeight(root->r);
}
return 0;
}
// insert a key value pair. here duplicate not allow.
pnode insertNode(pnode root, int key, int val) {
if (!root) {
root = new Node(key, val);
return root;
}
if (key < root->key) {
root->l = insertNode(root->l, key, val);
} else if (key > root->key) {
root->r = insertNode(root->r, key, val);
}
updateHeight(root);
updateCnt(root);
int balFactor = getBalFactor(root);
// l - l case.
if (balFactor > 1 && key < root->l->key) {
return rRotate(root);
}
// r - r case.
if (balFactor < -1 && key > root->r->key) {
return lRotate(root);
}
// l - r case.
if (balFactor > 1 && key > root->l->key) {
root->l = lRotate(root->l);
return rRotate(root);
}
// r - l case.
if (balFactor < -1 && key < root->r->key) {
root->r = rRotate(root->r);
return lRotate(root);
}
return root;
}
pnode deleteNode(pnode root, int key) {
if (!root) {
return root;
}
if (key < root->key) {
root->l = deleteNode(root->l, key);
} else if (key > root->key) {
root->r = deleteNode(root->r, key);
} else {
if (!root->l || !root->r) {
pnode x = root->l ? root->l : root->r;
if (x) {
*root = *x;
} else {
x = root;
root = NULL;
}
delete(x);
} else { // l and r both exist.
pnode x = smallest(root->r);
root->key = x->key;
root->val = x->val;
deleteNode(root->r, x->key);
}
}
if (!root) { // tree containing only one node.
return root;
}
updateHeight(root);
updateCnt(root);
int balFactor = getBalFactor(root);
// l - l case.
if (balFactor > 1 && key < root->l->key) {
return rRotate(root);
}
// r - r case.
if (balFactor < -1 && key > root->r->key) {
return lRotate(root);
}
// l - r case.
if (balFactor > 1 && key > root->l->key) {
root->l = lRotate(root->l);
return rRotate(root);
}
// r - l case.
if (balFactor < -1 && key < root->r->key) {
root->r = rRotate(root->r);
return lRotate(root);
}
return root;
}
inline int getKth(pnode root, int k) {
if (root == NULL) {
return -1;
}
int root_rank = getCnt(root->l) + 1;
if (root_rank == k) {
return root->key;
}
if (root_rank < k) {
return getKth(root->r, k - root_rank);
} else {
return getKth(root->l, k);
}
}
vector <pair <int, int>> ans;
inline void walk(pnode root) {
if (root) {
walk(root->l);
//printf("(%d %d) ", root->key, root->val);
ans.push_back(make_pair(root->key, root->cnt));
walk(root->r);
}
}
int main() {
pnode root = NULL;
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int x;
scanf("%d", &x);
root = insertNode(root, x, -1);
}
walk(root);
for (int i = 0; i < (int)ans.size(); i++) {
printf("%d %d\n", ans[i].first, ans[i].second);
}
int q;
scanf("%d", &q);
for (int i = 0; i < n; i++) {
int k;
scanf("%d", &k);
printf("%d\n", getKth(root, k));
}
}