-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_tree.h
More file actions
186 lines (162 loc) · 3.39 KB
/
binary_tree.h
File metadata and controls
186 lines (162 loc) · 3.39 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
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
#ifndef BINARY_TREE_H
#define BINARY_TREE_H
#include <cstdlib>
#include <iostream>
#include "tools.h"
//defined empty tree
struct nil
{
typedef nil left, right, data;
static int const value = 0;
};
//Tree node
template<typename Data, typename Left, typename Right>
struct node
{
typedef Data data;
typedef Left left;
typedef Right right;
};
//Tree size
template<typename Tree>
struct size
{
static size_t const result = 1 + size<typename Tree::left>::result + size<typename Tree::right>::result;
};
template<>
struct size<nil>
{
static size_t const result = 0;
};
//Is list
template<typename Tree>
struct is_list
{
static bool const result = IF<(is_same<
typename Tree::left, nil>::result &&
is_same<typename Tree::right, nil>::result)
,
_true,
_false
>::result::result;
};
//Is child
template<typename Tree, typename X>
struct is_child
{
static bool const result = is_same<typename Tree::left, X>::result || is_same<typename Tree::right, X>::result;
};
template<typename X>
struct is_child<nil, X>
{
static bool const result = false;
};
//Find element
template<typename Tree, typename Value>
struct find
{
typedef typename IF<is_list<Tree>::result,
Tree,
typename IF<(Value::value < Tree::data::value),
typename find<typename Tree::left, Value>::result,
typename find<typename Tree::right, Value>::result
>::result
>::result result;
};
template<typename Tree>
struct find<Tree, typename Tree::data>
{
typedef Tree result;
};
template<typename Value>
struct find<nil, Value>
{
typedef nil result;
};
//Turn left rib
template<typename Tree>
struct turn_left_rib
{
typedef node<typename Tree::left::data,
typename Tree::left::left,
node<typename Tree::data,
typename Tree::left::right,
typename Tree::right
>
> result;
};
//Turn right rib
template<typename Tree>
struct turn_right_rib
{
typedef node<typename Tree::right::data,
node<typename Tree::data,
typename Tree::left,
typename Tree::right::left
>,
typename Tree::right::right
> result;
};
//Depth
template<typename Tree, typename X>
struct depth
{
static int const result = IF<X::value < Tree::data::value,
depth<typename Tree::left, X>,
depth<typename Tree::right, X>
>::result::result + 1;
};
template<typename X>
struct depth<nil, X>
{
static int const result = -1;
};
template<typename Tree>
struct depth<Tree, typename Tree::data>
{
static int const result = 0;
};
//Maximum
template<typename Tree>
struct max
{
typedef typename IF<is_same<typename Tree::right, nil>::result,
Tree,
typename max<typename Tree::right>::result
>::result result;
};
template<>
struct max<nil>
{
typedef nil result;
};
//Minimum
template<typename Tree>
struct min
{
typedef typename IF<is_same<typename Tree::left, nil>::result,
Tree,
typename min<typename Tree::left>::result
>::result result;
};
template<>
struct min<nil>
{
typedef nil result;
};
//Print tree to standart output
template<typename Tree>
void print_tree()
{
std::cout << " (";
std::cout << Tree::data::value;
print_tree<typename Tree::left>();
print_tree<typename Tree::right>();
std::cout << ")";
}
template<>
void print_tree<nil>()
{
std::cout << " ()";
}
#endif