-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.cpp
More file actions
99 lines (87 loc) · 2.16 KB
/
Copy pathexample.cpp
File metadata and controls
99 lines (87 loc) · 2.16 KB
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
/**
* @file example.cpp
* @author Moshe Pontch (pontch at gmail.com)
* @brief Simple, concise usage example of the AVL library
* @date 2025-06-24
*
*/
#include <bitset>
#include <iostream>
#include <thread>
#include "avl_tool.h"
struct TestData
{
int i;
};
template <typename Char, typename Traits>
std::basic_ostream<Char, Traits> &operator<<(std::basic_ostream<Char, Traits> &os, const TestData &data)
{
const auto flags = avl_flags(os);
if (flags & avl::preorder)
{
os << std::bitset<8>(data.i);
}
else if (flags & avl::inorder)
{
os << std::oct << std::showbase << data.i;
}
else if (flags & avl::postorder)
{
os << std::hex << std::showbase << data.i;
}
else
{
os << std::dec << data.i;
}
return os;
}
template <typename Char, typename Traits>
void example(std::basic_ostream<Char, Traits> &os)
{
AvlTree<TestData> tree;
srand(time(0));
for (int i = 0; i < 24; i++)
{
const auto key = rand() % 100;
tree.insert(key, {key});
}
os << "summary with the default flat representation" << std::endl;
os << avl_summary << tree;
os << avl_simple; // reset summary
os << std::endl
<< "preorder traversal using binary base" << std::endl;
os << avl_preorder << tree;
os << std::endl
<< "inorder traversal using octal base" << std::endl;
os << avl_inorder << tree;
os << std::endl
<< "postorder traversal using hexadecimal base" << std::endl;
os << avl_postorder << tree;
os << std::endl
<< "levelorder traversal using decimal base" << std::endl;
os << avl_levelorder << tree;
}
#ifdef __cplusplus
extern "C"
{
#endif
int main()
{
try
{
/* attempt to use wide characters */
std::locale::global(std::locale(""));
std::cout.imbue(std::locale());
example(std::wcout);
}
catch (const std::exception &e)
{
std::locale::global(std::locale::classic());
std::cout.imbue(std::locale::classic());
example(std::cout);
}
return 0;
}
#ifdef __cplusplus
}
#endif