-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_tree.h
106 lines (85 loc) · 2.14 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
#pragma once
#include <vector>
#include <algorithm>
#include "node.h"
template <class T>
class binary_tree {
public:
binary_tree() : tree(nullptr) {}
explicit binary_tree(Node<T>* node) : tree(node) {}
explicit binary_tree(const std::vector<T> v) : tree(nullptr) {
for (const auto item: v) {
Add_Element(tree, item);
}
}
~binary_tree() {
del(tree);
}
std::vector<T> klp_tree() {
return klp_node(tree);
}
std::vector<T> kpl_tree() {
return kpl_node(tree);
}
std::vector<T> lpk_tree() {
return lpk_node(tree);
}
std::vector<T> lkp_tree() {
return lkp_node(tree);
}
std::vector<T> plk_tree() {
return plk_node(tree);
}
std::vector<T> pkl_tree() {
return pkl_node(tree);
}
Node<T>* GetNode() {
return tree;
}
void CopyTree(binary_tree* tree_) {
tree = tree_->GetNode();
}
void AddElement(const T& item) {
Add_Element(tree, item);
}
bool SearchElement(const T& item) {
return Search_Element(tree, item);
}
bool DeleteElement(const T item) {
return Delete_Element(tree, item);
}
void BalanceTree() {
std::vector<T> v = kpl_node(tree);
std::sort(v.begin(), v.end());
std::vector<T> v_ = middle_elements(v);
Node<T>* new_tree = nullptr;
for (const auto item: v_) {
Add_Element(new_tree, item);
}
del(tree);
tree = new_tree;
del(new_tree);
}
void Map(T (*f)(T)) {
Map_node(tree, f);
}
void Where(bool (*f)(T)) {
std::vector<T> v = Where_node(tree, f);
Node<T>* new_tree = nullptr;
for (const auto item: v) {
Add_Element(new_tree, item);
}
tree = new_tree;
}
T Reduce(T (*f)(T, T), T c) {
return Reduce_node(tree, f, c);
}
binary_tree<T>* SubTree(T new_root) {
Node<T>* new_node = Sub_Tree(tree, new_root);
auto new_tree = new binary_tree<T>;
new_tree->tree = new_node;
return new_tree;
}
private:
Node<T>* tree;
};