-
Notifications
You must be signed in to change notification settings - Fork 0
/
Simulado.cpp
143 lines (111 loc) · 2.1 KB
/
Simulado.cpp
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
#include "BinarySearchTree.h"
#include <concepts>
#include <iostream>
#include "BinarySearchTree.h"
template<std::totally_ordered T>
class BinarySearchTree
{
class BSTNode {
public:
T value;
std::unique_ptr<BSTNode> left;
std::unique_ptr<BSTNode> right;
BSTNode(T v) { value = v; }
};
public:
BinarySearchTree() {};
InsertionInfo insert(T v) {
return insert(v, root);
}
SearchInfo search(T v) {
return search(v, root);
}
void preorder() {
preorder(root);
}
void inorder() {
inorder(root);
}
void postorder() {
postorder(root);
}
void print() {
return print(root);
}
private:
std::unique_ptr<BSTNode> root;
InsertionInfo insert(T v, std::unique_ptr<BSTNode>& node) {
if (!node) {
node = std::make_unique<BSTNode>(v);
return InsertionInfo::Inserted;
}
else if (v == node->value) {
return InsertionInfo::AlreadyIn;
}
else {
return (v < node->value) ? insert(v, node->left) : insert(v, node->right);
}
}
SearchInfo search(T v, std::unique_ptr<BSTNode>& node) {
if (!node) {
return SearchInfo::NotFound;
}
if (v == node->value) {
return SearchInfo::Found;
}
else {
if (v < node->value) {
return search(v, node->left);
}
else {
return search(v, node->right);
}
}
}
//
void preorder(std::unique_ptr<BSTNode>& node) {
if (!node) {
std::cout << "";
}
else {
preorder(node->left);
preorder(node->right);
std::cout << " " << node->value << " ";
}
}
void inorder(std::unique_ptr<BSTNode>& node) {
if (!node) {
std::cout << "";
}
else {
inorder(node->left);
std::cout << " " << node->value << " ";
inorder(node->right);
}
}
void postorder(std::unique_ptr<BSTNode>& node) {
if (!node) {
std::cout << "";
}
else {
std::cout << " " << node->value << " ";
postorder(node->left);
postorder(node->right);
}
}
void reverse (std::unique_ptr<BSTNode>& node) {
if (!node) {
return;
}
else {
}
}
void print(std::unique_ptr<BSTNode>& node) {
if (!node) {
return;
}
print(node->left);
std::cout << node->value << "\t";
print(node->right);
}
};