-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint_bst.h
297 lines (234 loc) · 9.29 KB
/
print_bst.h
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
#include <cmath>
#include <iomanip>
#include <map>
#include <vector>
#include <cstdint>
#ifndef PRINT_BST_H
#define PRINT_BST_H
// BST pretty-print function
// Version 1.2
// maximum depth of tree to actually print.
#define PPBST_MAX_HEIGHT 6
// Returns the node's distance from the given root.
// 1 means that it is the root.
// Returns -1 (not found) if the distance is more than PPBST_MAX_HEIGHT,
// or -2 if the tree is inconsistent.
template<typename Key, typename Value>
int getNodeDepth(BinarySearchTree<Key, Value> const & tree, Node<Key, Value> * root, Node<Key, Value> * node)
{
int dist = 1;
while(node != root)
{
if(node == nullptr)
{
return -2;
}
++dist;
node = node->getParent();
if(dist > PPBST_MAX_HEIGHT)
{
return -1;
}
}
return dist;
}
// Returns the height of the subtree at root.
// Uses recursion, not height values, so it is bulletproof
// against incorrect heights.
// Stops recursing after PPBST_MAX_HEIGHT calls.
template<typename Key, typename Value>
int getSubtreeHeight(Node<Key, Value> * root, int recursionDepth = 1)
{
if(root == nullptr)
{
return 0;
}
if(recursionDepth > PPBST_MAX_HEIGHT)
{
// bail out to prevent infinite loops on bad trees
return 0;
}
return std::max(getSubtreeHeight(root->getLeft(), recursionDepth + 1),
getSubtreeHeight(root->getRight(), recursionDepth + 1)) + 1;
}
/* Function to prettily print a BST out to the terminal.
Output should look a bit like this:
--------------------------------------------------
[01]
/ \
[02] [03]
/ \ / \
[04] [05] [06] [07]
/ \ / \ / \ \
[08] [09] [10] [11] [12] [13] [15] [15]
1: Foo
2: Bar
3: Baz
....
-------------------------------------------------
To keep the distance between nodes down, nodes should only
have a number inside them, and their actual value should be
printed out at the bottom.
This function should handle broken trees without crashing,
and should print as much of them as it can.
*/
template<typename Key, typename Value>
void BinarySearchTree<Key, Value>::printRoot (Node<Key, Value>* root) const
{
// special case for empty trees:
if(root == nullptr)
{
std::cout << "<empty tree>" << std::endl;
return;
}
#define BOX_WIDTH 4
#define PADDING 2 // distance between elements at bottom row
#define ELEMENT_WIDTH (BOX_WIDTH + PADDING)
// save initial cout state (from https://stackoverflow.com/questions/2273330/restore-the-state-of-stdcout-after-manipulating-it)
std::ios::fmtflags origCoutState(std::cout.flags());
// do some initial calculations
// ----------------------------------------------------------------------
uint32_t printedTreeHeight = getSubtreeHeight(root);
bool clippedFinalElements = false;
// with the width of a standard terminal, we can only print 2^5 = 32 elements
if(printedTreeHeight > PPBST_MAX_HEIGHT)
{
printedTreeHeight = PPBST_MAX_HEIGHT;
clippedFinalElements = true;
}
uint16_t finalRowNumElements = (uint16_t)std::pow(2, printedTreeHeight - 1);
uint16_t finalRowWidth = ((uint16_t)(ELEMENT_WIDTH * finalRowNumElements - PADDING));
// get placeholders
// ----------------------------------------------------------------------
std::map<Key, uint8_t> valuePlaceholders;
uint8_t nextPlaceHolderVal = 1;
for(typename BinarySearchTree<Key, Value>::iterator treeIter = this->begin(); treeIter != this->end(); ++treeIter)
{
if(getNodeDepth(*this, root, treeIter.current_) != -1)
{
// note; the iterator will traverse in sorted order so values should get the same placeholders between
// different calls as long as the tree is the same
valuePlaceholders.insert(std::make_pair(treeIter->first, nextPlaceHolderVal++));
}
}
// print tree
// ----------------------------------------------------------------------
// space in front of the first element of the previous row
uint16_t firstElementMargin = ((uint16_t)(finalRowWidth/2 - (BOX_WIDTH/2))); // start in the middle of where the last row will be
uint16_t elementPadding = ((uint16_t)(finalRowWidth - 2));
std::vector<Node<Key, Value> *> currRowNodes; // contains the 2^levelIndex nodes in this row, or nullptr to mark nonexistant nodes
currRowNodes.push_back(root);
for(size_t levelIndex = 0; levelIndex < printedTreeHeight; ++levelIndex)
{
uint16_t numElements =(uint16_t)std::pow(2, levelIndex);
// print elements themselves
std::cout << std::string(firstElementMargin, ' ');
for(size_t elementIndex = 0; elementIndex < numElements; ++elementIndex)
{
if(currRowNodes[elementIndex] == nullptr)
{
std::cout << " ";
}
else
{
uint16_t placeholder = valuePlaceholders[currRowNodes[elementIndex]->getItem().first];
std::cout << "[" << std::setfill('0') << std::setw(2) << placeholder << "]";
}
if(elementIndex != ((uint16_t)(numElements - 1)))
{
std::cout << std::string(elementPadding, ' ');
}
}
std::cout << std::endl;
// spacing values for next row (worked out on paper)
elementPadding = ((uint16_t)((elementPadding - BOX_WIDTH) / 2));
firstElementMargin = ((uint16_t)(firstElementMargin - (elementPadding / 2 + 2)));
// calculate node lists for next iteration
// ---------------------------------------------------------------------
std::vector<Node<Key, Value> *> prevRowNodes = currRowNodes;
currRowNodes.clear();
for(typename std::vector<Node<Key, Value> *>::iterator prevRowIter = prevRowNodes.begin(); prevRowIter != prevRowNodes.end() ; ++prevRowIter)
{
if(*prevRowIter == nullptr)
{
currRowNodes.push_back(nullptr);
currRowNodes.push_back(nullptr);
}
else
{
currRowNodes.push_back((*prevRowIter)->getLeft());
currRowNodes.push_back((*prevRowIter)->getRight());
}
}
// print connecting lines
// ---------------------------------------------------------------------
if(levelIndex < printedTreeHeight - 1)
{
// start above middle side of first element
std::cout << std::string(firstElementMargin + 2, ' ');
for(size_t prevRowElementIndex = 0; prevRowElementIndex < prevRowNodes.size(); ++prevRowElementIndex)
{
Node<Key, Value> * currNode = prevRowNodes[prevRowElementIndex];
// print first branch
if(currNode == nullptr || currNode->getLeft() == nullptr)
{
std::cout << std::string(elementPadding/2 + 3, ' ');
}
else
{
std::cout << "\u250c";
for(int numLines = 0; numLines < (elementPadding/2 - 1); ++numLines)
{
std::cout << u8"\u2500";
}
std::cout << "\u2518 ";
}
// print second branch
if(currNode == nullptr || currNode->getRight() == nullptr)
{
std::cout << std::string(elementPadding/2 + 3, ' ');
}
else
{
std::cout << "\u2514";
for(int numLines = 0; numLines < (elementPadding/2 - 1); ++numLines)
{
std::cout << u8"\u2500";
}
std::cout << "\u2510 ";
}
std::cout << std::string(elementPadding + 2, ' ');
}
std::cout << std::endl;
}
}
std::cout << std::endl;
if(clippedFinalElements)
{
std::cout << "(deeper levels omitted due to space limitations)" << std::endl;
}
if(!std::is_same<Key, uint8_t>::value) // print placeholder explanations if needed:
{
std::cout << "Tree Placeholders:------------------" << std::endl;
for(typename std::map<Key, uint8_t>::iterator placeholdersIter = valuePlaceholders.begin(); placeholdersIter != valuePlaceholders.end(); ++placeholdersIter)
{
std::cout << '[' << std::setfill('0') << std::setw(2) << ((uint16_t)placeholdersIter->second) << "] -> ";
// print element with original cout flags
std::cout.flags(origCoutState);
std::cout << '(' << placeholdersIter->first << ", ";
typename BinarySearchTree<Key, Value>::iterator elementIter = this->find(placeholdersIter->first);
if(elementIter == this->end())
{
std::cout << "<error: lookup failed>";
}
else
{
std::cout << elementIter->second;
}
std::cout << ')' << std::endl;
}
}
// restore original cout flags
std::cout.flags(origCoutState);
}
#endif