-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbst_sequences.cpp
346 lines (319 loc) · 9.48 KB
/
bst_sequences.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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#include <iostream>
#include <span> // for std::span<>
#include <algorithm> // for std::ranges::sort()
#include <vector> // for std::vector<>
#include <queue> // for std::queue<>
#include <initializer_list> // for std::initializer_list<>
#include <type_traits>
// Binary Tree Node holding data value of type T
template<typename T>
struct BTNode
{
T value { };
BTNode<T>* left { };
BTNode<T>* right { };
};
// NOTE: if we were to write 'const std::span<T>' here, then T will be deduced as 'const int' when calling createBST with const data
// if we write 'const std::span<const T>', the compiler deduces T as 'int'
template<typename T>
static BTNode<T>* createBST(const std::span<const T> array, std::size_t start, std::size_t end) noexcept
{
if(start >= end)
return nullptr;
std::size_t index = (end + start) / 2;
auto* node = new BTNode<T> { array[index] };
node->left = createBST(array, start, index);
node->right = createBST(array, index + 1, end);
return node;
}
template<template<typename> typename T, typename U>
concept VectorLike = requires(T<U>& array)
{
{ array.size() } -> std::integral;
{ array.data() } -> std::same_as<typename std::add_pointer<U>::type>;
};
template<typename T, template<typename> typename Vector> requires(VectorLike<Vector, T>)
static BTNode<T>* createBST(const Vector<T>& array) noexcept
{
return createBST(std::span { array.data(), array.size() }, 0, array.size());
}
template<typename T>
concept BTNodeType = requires(std::add_pointer_t<T> node)
{
{ node->left } -> std::convertible_to<std::add_pointer_t<T>>;
{ node->right } -> std::convertible_to<std::add_pointer_t<T>>;
};
template<BTNodeType Node>
static void _BTNodeGetCount(const Node* node, std::size_t& outCount) noexcept
{
if(!node) return;
++outCount;
_BTNodeGetCount(node->left, outCount);
_BTNodeGetCount(node->right, outCount);
}
template<BTNodeType Node>
static std::size_t BTNodeGetCount(const Node* node) noexcept
{
std::size_t count = 0;
_BTNodeGetCount(node, count);
return count;
}
template<typename T, typename Node>
concept BTNodeVisitor = requires(T visitor, Node* node)
{
{ visitor(node) } -> std::same_as<void>;
};
template<BTNodeType Node>
static void BTNodeInOrderTraverse(Node* node, BTNodeVisitor<Node> auto visitor)
{
if(!node)
return;
BTNodeInOrderTraverse(node->left, visitor);
visitor(node);
BTNodeInOrderTraverse(node->right, visitor);
}
template<typename T>
static void destroyBST(BTNode<T>* node) noexcept
{
if(!node)
return;
destroyBST(node->left);
destroyBST(node->right);
delete node;
}
template<typename T, template<typename> typename Iteratable>
static std::ostream& operator<<(std::ostream& stream, const Iteratable<T>& values) noexcept
{
stream << "{ ";
for(decltype(values.size()) i = 0; const auto& value : values)
{
stream << value;
if(++i < values.size())
stream << ", ";
}
stream << " }";
return stream;
}
template<typename T>
static std::ostream& operator<<(std::ostream& stream, BTNode<T>& bst) noexcept
{
stream << "\n";
stream << "Node Count: " << BTNodeGetCount(&bst) << "\n";
std::size_t level = 0;
std::queue<BTNode<T>*> queue;
queue.push(&bst);
while(!queue.empty())
{
stream << "[" << level << "]: ";
// Only pop out the nodes for this level
std::size_t count = queue.size();
// Print the level
while(count)
{
BTNode<T>* node = queue.front();
queue.pop();
stream << node->value;
--count;
if(count)
stream << ", ";
// Add the next level nodes in the back of the queue
if(node->left)
queue.push(node->left);
if(node->right)
queue.push(node->right);
}
if(!queue.empty())
stream << "\n";
++level;
}
return stream;
}
template<typename T>
static BTNode<T>* BTNodeGetRightMost(BTNode<T>* node) noexcept
{
if(node->right)
return BTNodeGetRightMost(node->right);
return node;
}
template<BTNodeType Node>
static std::size_t BTNodeGetHeight(Node* node) noexcept
{
if(!node) return 0;
return std::max(BTNodeGetHeight(node->left), BTNodeGetHeight(node->right)) + ((node->left || node->right) ? 1 : 0);
}
// WARN: This solution has a serious flaw, it just swaps the subtrees recursively! So it doesn't produce exhaustive list of
// possible sequences.
template<typename T, std::integral SizeType>
static void generateBSTSequences(BTNode<T>* node, SizeType& n, std::vector<T>& seq, std::vector<std::vector<T>>& seqs) noexcept
{
if(!node) return;
// The parent node must have been added first
seq.push_back(node->value);
auto cnt = seq.size();
// If this was the last node then we have just build a possible sequence
if(cnt == n)
{
// so add this another possible sequence
seqs.push_back(seq);
return;
}
// Recursively Permute the two subtrees as it doesn't matter all node of which subtree is added after the parent node.
generateBSTSequences(node->left, n, seq, seqs);
generateBSTSequences(node->right, n, seq, seqs);
// We need to permute the two subtrees only if the node (parent) has exactly two non-null children,
// This is to avoid a case where only one child is non-null in which case we would end up generating duplicates sequences.
if(node->left && node->right)
{
seq.resize(cnt);
generateBSTSequences(node->right, n, seq, seqs);
generateBSTSequences(node->left, n, seq, seqs);
}
}
template<BTNodeType Node>
static std::size_t BTNodeGetCount(Node* node) noexcept
{
std::size_t i = 0;
BTNodeInOrderTraverse(node, [&i]([[maybe_unused]] Node* _) noexcept
{
i += 1;
});
return i;
}
template<typename T>
static std::vector<std::vector<T>> generateBSTSequences(BTNode<T>* node) noexcept
{
std::vector<std::vector<T>> seqs;
std::vector<T> seq;
std::size_t n = BTNodeGetCount(node);
generateBSTSequences(node, n, seq, seqs);
// NRVO would kick-in here
return seqs;
}
template<typename T>
static void generateWeaves(const std::span<const T> s1, const std::span<const T> s2,
std::size_t index1, std::size_t index2,
std::vector<T>& buffer,
std::vector<std::vector<T>>& seqs)
{
if(buffer.size() >= (s1.size() + s2.size()))
{
seqs.push_back(buffer);
return;
}
for(auto i = index1; i < s1.size(); ++i)
{
buffer.push_back(s1[i]);
generateWeaves(s1, s2, i + 1, index2, buffer, seqs);
buffer.pop_back();
}
for(auto i = index2; i < s2.size(); ++i)
{
buffer.push_back(s2[i]);
generateWeaves(s1, s2, index1, i + 1, buffer, seqs);
buffer.pop_back();
}
}
template<typename T>
static void generateWeaves(const std::span<const T> s1, const std::span<const T> s2, std::vector<std::vector<T>>& seqs)
{
std::vector<T> buffer;
generateWeaves(s1, s2, 0, 0, buffer, seqs);
}
template<typename T>
static void push_front(std::vector<T>& v, const T& value)
{
v.insert(v.begin(), value);
}
template<typename T>
static std::vector<std::vector<T>> generateBSTSequences2(BTNode<T>* node) noexcept
{
// Always think theoretically, so if we have null node that means there is only one sequence which
// could lead to a null node (or an empty bst), it is the empty sequence.
if(!node)
{
std::vector<std::vector<T>> seqs;
seqs.push_back(std::vector<T>({ }));
return seqs;
}
std::vector<std::vector<T>> leftSeqs = generateBSTSequences2(node->left);
std::vector<std::vector<T>> rightSeqs = generateBSTSequences2(node->right);
// Otherwise weave the sequences in all possible pair combinations
std::vector<std::vector<T>> seqs;
for(const auto& seq1 : leftSeqs)
for(const auto& seq2 : rightSeqs)
generateWeaves(std::span { seq1 }, std::span { seq2 }, seqs);
// Parent node value is always added first in a valid BST
for(auto& seq : seqs)
push_front(seq, node->value);
return seqs;
}
struct Solution1
{
template<typename T>
std::vector<std::vector<T>> operator()(BTNode<T>* node) noexcept
{
std::cout << "Solution 1: \n";
return generateBSTSequences(node);
}
};
struct Solution2
{
template<typename T>
std::vector<std::vector<T>> operator()(BTNode<T>* node) noexcept
{
std::cout << "Solution 2: \n";
return generateBSTSequences2(node);
}
};
template<typename Sol, typename T>
static void runBSTSequences(BTNode<T>* bst) noexcept
{
std::cout << "Input Binary Tree: " << *bst << "\n";
std::vector<std::vector<T>> sequences = Sol { }(bst);
std::cout << "Sequence count: " << sequences.size() << "\n";
for(const auto& seq : sequences)
std::cout << seq << "\n";
}
template<typename T>
static void run(std::initializer_list<T> initValues) noexcept
{
std::cout << "Input: " << initValues << "\n";
std::vector<T> values (initValues);
std::ranges::sort(values, std::less<> { });
std::cout << "Sorted Output: " << values << "\n";
BTNode<T>* bst = createBST(values);
std::cout << "BST Output: " << *bst << "\n";
std::cout << "In order Traversal: "; BTNodeInOrderTraverse<const BTNode<T>>(bst, [](const BTNode<T>* node) noexcept
{
std::cout << node->value << " ";
});
std::cout << "\n";
std::cout << "**----BST Sequences----**\n";
std::cout << "Input Set 1: \n";
runBSTSequences<Solution1>(bst);
runBSTSequences<Solution2>(bst);
std::cout << "Input Set 2: \n";
auto* node = BTNodeGetRightMost(bst);
node->right = new BTNode<T> { 2355 };
node->right->right = new BTNode<T> { 5443 };
node->right->right->right = new BTNode<T> { -443 };
node->right->right->right->right = new BTNode<T> { -6490 };
runBSTSequences<Solution1>(bst);
runBSTSequences<Solution2>(bst);
destroyBST(bst);
std::cout << "Input Set 3: \n";
{
auto* node = new BTNode<T> { 2 };
node->left = new BTNode<T> { 1 };
node->right = new BTNode<T> { 3 };
node->right->right = new BTNode<T> { 4 };
runBSTSequences<Solution1>(node);
runBSTSequences<Solution2>(node);
destroyBST(node);
}
}
int main()
{
run<int>({ 100, 2, 3, 4, -1, -2, -3 });
return 0;
}