-
Notifications
You must be signed in to change notification settings - Fork 0
/
minimal_tree.cpp
165 lines (151 loc) · 4.22 KB
/
minimal_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
#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 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";
destroyBST(bst);
}
int main()
{
run<int>({ 100, 2, 3, 4, 0, 45, 32, 56, 7, 8, 10, -1, 2, -4 });
return 0;
}