-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbinary_search_tree.cpp
More file actions
53 lines (40 loc) · 1.01 KB
/
binary_search_tree.cpp
File metadata and controls
53 lines (40 loc) · 1.01 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
#include "classwork/binary_search_tree.hpp"
#include <iostream> // cout
namespace classwork {
BinarySearchTree::~BinarySearchTree() {
clear(root_);
root_ = nullptr;
}
void BinarySearchTree::Insert(int key, int value) {
insert(key, value, root_);
}
void BinarySearchTree::Traverse(TraversalStrategy* strategy) const {
strategy->Print(root_, std::cout);
}
Node* BinarySearchTree::root() const {
return root_;
}
// вспомогательные методы
void BinarySearchTree::insert(int key, int value, Node*& node) {
if (node == nullptr) {
node = new Node(key, value);
return;
}
if (key == node->key) {
node->value = value;
return;
}
if (key < node->key) {
insert(key, value, node->left);
} else {
insert(key, value, node->right);
}
}
void BinarySearchTree::clear(Node* node) {
if (node != nullptr) {
clear(node->left);
clear(node->right);
delete node;
}
}
} // namespace classwork