-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbinary_tree.h
178 lines (152 loc) · 3.56 KB
/
binary_tree.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
#ifndef STRUCTURES_BINARY_TREE_H
#define STRUCTURES_BINARY_TREE_H
#include <iostream>
#include <traits.h>
#include <tree.h>
namespace structures {
template <typename T>
class AVLNode;
/**
* @brief BinaryTree node implementation
*/
template <typename T>
struct Node {
explicit Node(const T& data_) : data{data_} {}
Node(const T& data_, Node* parent_) : data{data_}, parent{parent_} {}
virtual ~Node() {
delete left;
delete right;
}
static Node<T>* insert(Node<T>* node, const T& data_) {
if (data_ < node->data) {
// insert left
if (node->left) {
return insert(node->left, data_);
} else {
node->left = new Node(data_, node);
return node->left;
}
} else if (data_ > node->data) {
// insert right
if (node->right) {
return insert(node->right, data_);
} else {
node->right = new Node(data_, node);
return node->right;
}
} else {
return nullptr;
}
}
static Node<T>* remove(Node<T>* node, const T& data_) {
if (node->data == data_) {
if (node->right && node->left) {
node->data = node->substitute();
return remove(node->right, node->data);
} else {
auto n = node->right ? node->right : node->left;
if (node->parent->right == node) {
node->parent->right = n;
} else {
node->parent->left = n;
}
if (n)
n->parent = node->parent;
node->left = nullptr;
node->right = nullptr;
auto parent = node->parent;
delete node;
return parent;
}
} else {
auto n = data_ < node->data ? node->left : node->right;
return n ? remove(n, data_) : nullptr;
}
}
bool contains(const T& data_) const {
if (data == data_) {
return true;
} else {
if (data_ < data) {
return left ? left->contains(data_) : false;
} else {
return right ? right->contains(data_) : false;
}
}
}
void pre_order(ArrayList<T>& v) const {
v.push_back(data);
if (left)
left->pre_order(v);
if (right)
right->pre_order(v);
}
void in_order(ArrayList<T>& v) const {
if (left)
left->in_order(v);
v.push_back(data);
if (right)
right->in_order(v);
}
void post_order(ArrayList<T>& v) const {
if (left)
left->post_order(v);
if (right)
right->post_order(v);
v.push_back(data);
}
// returns the smallest value of the right sub-tree
T substitute() const {
Node* it = right;
while (it->left) {
it = it->left;
}
return it->data;
}
virtual void print(int indent) const {
if (right)
right->print(indent + 1);
for (int i = 0; i < indent; ++i)
std::cout << " ";
std::cout << data << std::endl;
if (left)
left->print(indent + 1);
}
T data{};
Node* parent{nullptr};
Node* left{nullptr};
Node* right{nullptr};
protected:
Node<T>* find_node_to_delete(const T& data_) {
if (data == data_) {
if (right && left) {
data = substitute();
return right->find_node_to_delete(data);
} else {
return this;
}
} else {
if (data_ < data)
return left ? left->find_node_to_delete(data_) : nullptr;
else
return right ? right->find_node_to_delete(data_) : nullptr;
}
}
};
/**
* @brief Unbalanced binary search tree
*
* @details This structure provides O(log n) operations on the best case, but
* as it is unbalanced, the operations may be O(n) on the worst case (e.g. you
* insert members in order).
*/
template <typename T>
class BinaryTree : public Tree<T, Node<T>> {};
} // namespace structures
/* set trait */
template <>
const bool traits::is_set<structures::BinaryTree>::value = true;
/* name trait */
template <>
const std::string traits::type<structures::BinaryTree>::name = "BinaryTree";
#endif