25
25
#define FUNC_BTree
26
26
27
27
#include < map>
28
+ #include < iomanip>
28
29
#include " utils.h"
29
30
30
31
size_t debug;
@@ -324,20 +325,35 @@ class BTree {
324
325
}
325
326
return ans;
326
327
}
327
- void print_tree (BNode<T>* x, size_t depth) {
328
- std::string indent (4 * depth, ' ' );
329
- std::cout << indent << " ___" << std::endl;
328
+ void print_tree (BNode<T>* x, size_t depth, bool term) {
329
+ const size_t ind = 4 ;
330
+ const std::string indent (ind * depth, ' ' );
331
+ if (!term)
332
+ std::cout << indent << std::string (3 , ' _' ) << std::endl;
330
333
for (size_t i = 0 ; i <= x->n ; i++) {
331
334
if (!x->leaf )
332
- print_tree (x->c [i], depth + 1 );
335
+ print_tree (x->c [i], depth + 1 , term );
333
336
if (i == x->n )
334
337
break ;
335
- std::cout << indent << x->key [i] << std::endl;
338
+ std::cout << indent;
339
+ if (term) {
340
+ if (i == 0 )
341
+ std::cout << " \033 [53m" ;
342
+ if (i == x->n - 1 )
343
+ std::cout << " \033 [4m" ;
344
+ }
345
+ std::cout << std::left << std::setw (ind - 1 ) << x->key [i];
346
+ if (term)
347
+ std::cout << " \033 [0m" ;
348
+ std::cout << std::endl;
336
349
}
337
- std::cout << indent << " ^^^" << std::endl;
350
+ if (!term)
351
+ std::cout << indent << std::string (3 , ' ^' ) << std::endl;
338
352
}
339
- void print_tree () {
340
- print_tree (root, 0 );
353
+ void print_tree (bool term) {
354
+ std::cout << std::endl;
355
+ print_tree (root, 0 , term);
356
+ std::cout << std::endl;
341
357
}
342
358
~BTree () { delete root->recursive_destruct (); }
343
359
BNode<T> *root;
@@ -353,7 +369,7 @@ int main(int argc, char *argv[]) {
353
369
std::cout << " s / S: search" << std::endl;
354
370
std::cout << " i / I: insert" << std::endl;
355
371
std::cout << " d / D: delete" << std::endl;
356
- std::cout << " p / P: print tree" << std::endl;
372
+ std::cout << " p / P: print tree (two styles) " << std::endl;
357
373
std::cout << " q / Q: quit" << std::endl;
358
374
// Generate tree in Page 498, Figure 18.7 (a):
359
375
// i10 i11 i13 i14 i15 i1 i3 i7 i4 i5 i16 i18 i19 i24 i25 i26 i20 i21 i22
@@ -388,8 +404,10 @@ int main(int argc, char *argv[]) {
388
404
std::cout << std::boolalpha << BT.BTreeDelete (x) << std::endl;
389
405
break ;
390
406
case ' p' :
407
+ BT.print_tree (true );
408
+ break ;
391
409
case ' P' :
392
- BT.print_tree ();
410
+ BT.print_tree (false );
393
411
break ;
394
412
case ' q' :
395
413
case ' Q' :
0 commit comments