-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathbst_soln(1).cpp
More file actions
201 lines (178 loc) · 5.35 KB
/
Copy pathbst_soln(1).cpp
File metadata and controls
201 lines (178 loc) · 5.35 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
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <algorithm>
#include <assert.h>
using namespace std;
struct Node {
int key;
int size;
Node *left;
Node *right;
Node (int k) { key = k; size = 1; left = right = NULL; }
};
void fix_size(Node *T)
{
T->size = 1;
if (T->left) T->size += T->left->size;
if (T->right) T->size += T->right->size;
}
// insert key k into tree T, returning a pointer to the resulting tree
Node *insert(Node *T, int k)
{
if (T == NULL) return new Node(k);
if (k < T->key) T->left = insert(T->left, k);
else T->right = insert(T->right, k);
fix_size(T);
return T;
}
// prints out the inorder traversal of T (i.e., the contents of T in sorted order)
void print_inorder(Node *T)
{
if (T == NULL) return;
print_inorder(T->left);
cout << T->key << "\n";
print_inorder(T->right);
}
// return a pointer to the node with key k in tree T, or NULL if it doesn't exist
Node *find(Node *T, int k)
{
if (T == NULL) return NULL;
if (k == T->key) return T;
if (k < T->key) return find(T->left, k);
else return find(T->right, k);
}
// return pointer to node of rank r (with r'th largest key; e.g. r=0 is the minimum)
Node *select(Node *T, int r)
{
assert(T!=NULL && r>=0 && r<T->size);
int rank_of_root = T->left ? T->left->size : 0;
if (r == rank_of_root) return T;
if (r < rank_of_root) return select(T->left, r);
else return select(T->right, r - rank_of_root - 1);
}
// Join trees L and R (with L containing keys all <= the keys in R)
// Return a pointer to the joined tree.
Node *join(Node *L, Node *R)
{
// choose either the root of L or the root of R to be the root of the joined tree
// (where we choose with probabilities proportional to the sizes of L and R)
if (L == NULL) return R;
if (R == NULL) return L;
int random = rand() % (L->size + R->size);
if (random < L->size) {
// L stays root
L->right = join(L->right, R);
fix_size(L);
return L;
} else {
// R stays root
R->left = join(L, R->left);
fix_size(R);
return R;
}
}
// remove key k from T, returning a pointer to the resulting tree.
// it is required that k be present in T
Node *remove(Node *T, int k)
{
assert(T != NULL);
if (k == T->key) {
Node *to_delete = T;
T = join(T->left, T->right);
delete to_delete;
return T;
}
if (k < T->key) T->left = remove(T->left, k);
else T->right = remove(T->right, k);
fix_size(T);
return T;
}
// Split tree T on key k into tree L (containing keys <= k) and a tree R (containing keys > k)
void split(Node *T, int k, Node **L, Node **R)
{
if (T == NULL) {
*L = NULL;
*R = NULL;
return;
}
if (k < T->key) {
// recursively split left subtree
split(T->left, k, L, &T->left);
*R = T;
} else {
split(T->right, k, &T->right, R);
*L = T;
}
fix_size(T);
}
// insert key k into the tree T, returning a pointer to the resulting tree
Node *insert_random(Node *T, int k)
{
// If k is the Nth node inserted into T, then:
// with probability 1/N, insert k at the root of T
// otherwise, insert_random k recursively left or right of the root of T
if (T == NULL) return new Node(k);
if (rand() % (T->size + 1) == 0) {
// insert at root
Node *new_root = new Node(k);
split(T, k, &new_root->left, &new_root->right);
fix_size(new_root);
return new_root;
}
if (k < T->key) T->left = insert_random(T->left, k);
else T->right = insert_random(T->right, k);
fix_size(T);
return T;
}
int main(void)
{
int A[10];
// put 0..9 into A[0..9] in random order
for (int i=0; i<10; i++) A[i] = i;
for (int i=9; i>0; i--) swap(A[i], A[rand()%i]);
// insert contents of A into a BST
Node *T = NULL;
for (int i=0; i<10; i++) T = insert(T, A[i]);
// print contents of BST (should be 0..9 in sorted order)
print_inorder(T);
// test select
for (int i=0; i<10; i++) {
Node *result = select(T, i);
if (!result || result->key != i) cout << "Select test failed for select(" << i << ")!\n";
}
// test find: Elements 0..9 should be found; 10 should not be found
for (int i=0; i<11; i++)
if (find(T,i)) cout << i << " found\n";
else cout << i << " not found\n";
// test split
Node *L, *R;
split(T, 4, &L, &R);
cout << "Contents of left tree after split (should be 0..4):\n";
print_inorder(L);
cout << "Left tree size " << L->size << "\n";
cout << "Contents of right tree after split (should be 5..9):\n";
print_inorder(R);
cout << "Right tree size " << R->size << "\n";
// test join
T = join(L, R);
cout << "Contents of re-joined tree (should be 0..9)\n";
print_inorder(T);
cout << "Tree size " << T->size << "\n";
// test remove
for (int i=0; i<10; i++) A[i] = i;
for (int i=9; i>0; i--) swap(A[i], A[rand()%i]);
for (int i=0; i<10; i++) {
T = remove(T, A[i]);
cout << "Contents of tree after removing " << A[i] << ":\n";
print_inorder(T);
if (T != NULL)
cout << "Tree size " << T->size << "\n";
}
// test insert_random
cout << "Inserting 10 million elements in order; this should be very fast...\n";
for (int i=0; i<10000000; i++) T = insert_random(T, i);
cout << "Tree size " << T->size << "\n";
cout << "Done\n";
return 0;
}