Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

## 1. Информация о студенте

**Номер группы**: 00-000
**Номер группы**: 11-109

**Фамилия и Имя**: Иванов Иван
**Фамилия и Имя**: Шамсутдинов Рафаэль

## 2. Описание задания

Expand Down
119 changes: 96 additions & 23 deletions src/binary_search_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,74 +7,147 @@ namespace assignment {
}

void BinarySearchTree::Insert(int key, int value) {
// Write your code here...
insert(key, value, root_);
}

bool BinarySearchTree::Remove(int key) {
// Write your code here...
return false;
return remove(key, root_);
}

void BinarySearchTree::Clear() {
// Write your code here...
clear(root_);
root_ = nullptr;
}

std::optional<int> BinarySearchTree::Find(int key) const {
// Write your code here...
Node* found_node = find(key, root_);
if (found_node != nullptr) {
return found_node->value;
}
return std::nullopt;
}

bool BinarySearchTree::Contains(int key) const {
// Write your code here...
return false;
if (find(key, root_) == nullptr) {
return false;
}
return true;
}

bool BinarySearchTree::IsEmpty() const {
return false;
return root_ == nullptr;
}

std::optional<int> BinarySearchTree::FindMin() const {
// Write your code here...
Node* node = find_min(root_);
if (node != nullptr) {
return node->key;
}
return std::nullopt;
}

std::optional<int> BinarySearchTree::FindMax() const {
// Write your code here...
Node* node = find_max(root_);
if (node != nullptr) {
return node->key;
}
return std::nullopt;
}

Node* BinarySearchTree::root() const {
return nullptr;
return root_;
}

// вспомогательные методы

void BinarySearchTree::insert(int key, int value, Node*& node) {
// Write your code here ...
if (node == nullptr) {
node = new Node(key, value);
return;
}
if (key > node->key) {
return insert(key, value, node->right);
}
if (key < node->key) {
return insert(key, value, node->left);
}
node->value = value;
}

bool BinarySearchTree::remove(int key, Node*& node) {
// Write your code here...
return false;
if (node == nullptr) {
return false;
}

if (key == node->key) {

if (node->left != nullptr and node->right != nullptr) {
Node* min = find_min(node->right);
node->key = min->key;
node->value = min->value;
return remove(min->key, node->right);
}

if (node->left != nullptr or node->right == nullptr) {
Node* left_child = node->left;
delete node;
node = left_child;
return true;
}

Node* right_child = node->right;
delete node;
node = right_child;
return true;
}

if (key > node->key) {
return remove(key, node->right);
}

return remove(key, node->left);
}

void BinarySearchTree::clear(Node* node) {
// Write your code here...
delete node;
//node = nullptr;
}

Node* BinarySearchTree::find(int key, Node* node) const {
// Write your code here...
if (node == nullptr) {
return nullptr;
}
if (key == node->key) {
return node;
}
if (key < node->key) {
return find(key, node->left);
}
if (key > node->key) {
return find(key, node->right);
}
return nullptr;
}

Node* BinarySearchTree::find_min(Node* node) const {
// Write your code here...
return nullptr;
if (node == nullptr) {
return nullptr;
}
Node* curr_node = node;
while (curr_node->left != nullptr) {
curr_node = curr_node->left;
}
return curr_node;
}

Node* BinarySearchTree::find_max(Node* node) const {
// Write your code here...
return nullptr;
}

} // namespace assignment
if (node == nullptr) {
return nullptr;
}
Node* curr_node = node;
while (curr_node->right != nullptr) {
curr_node = curr_node->right;
}
return curr_node;
}
}