forked from ChrisRyan98008/NwCpp-May2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.h
63 lines (53 loc) · 1.44 KB
/
util.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
#pragma once
#include <memory>
#include <iostream>
std::ostream& operator << (std::ostream& os, const std::string& str)
{
return os << str.c_str();
}
#include <random>
namespace Util
{
class Rand
{
std::random_device _rand;
public:
int get(int max = 99, int min = 0)
{
return _rand() % (max + 1 - min) + min;
}
};
template<typename Type>
class DrawTree
{
public:
DrawTree(const Type root, bool bNulls = false) :_root(root), _bNulls(bNulls) {}
friend std::ostream& operator << (std::ostream& os, const DrawTree& tree)
{
return tree.Draw(os, tree._root, tree._bNulls);
}
protected:
const Type _root;
bool _bNulls;
template<typename Type2>
std::ostream& Draw(std::ostream& os, Type2 node, bool bNulls, std::string indent = std::string()) const
{
os << *node << "\n";
if(node->_pLeft)
{
os << indent << (node->_pRight || bNulls ? "+-->" : "\\-->");
Draw(os, node->_pLeft, bNulls, indent + (node->_pRight || bNulls ? "| " : " "));
if(!node->_pRight && bNulls)
os << indent << "\\--x\n";
}
if(node->_pRight)
{
if(!node->_pLeft && bNulls)
os << indent << "+--x\n";
os << indent << "\\-->";
Draw(os, node->_pRight, bNulls, indent + " ");
}
return os;
};
};
}