34 lines
681 B
C++
34 lines
681 B
C++
#include "bst.h"
|
|
|
|
Tree *insert(Tree *root, int value) {
|
|
if (root == nullptr) {
|
|
root = new Tree{value, nullptr, nullptr};
|
|
} else if (value < root->info) {
|
|
root->left = insert(root->left, value);
|
|
} else if (value > root->info) {
|
|
root->right = insert(root->right, value);
|
|
}
|
|
return root;
|
|
}
|
|
|
|
Tree *search(Tree *root, int value) {
|
|
Tree *ptr = root;
|
|
while (ptr != nullptr) {
|
|
if (value > ptr->info)
|
|
ptr = ptr->right;
|
|
else if (value < ptr->info)
|
|
ptr = ptr->left;
|
|
else
|
|
return ptr;
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
void freeTree(Tree *root) {
|
|
if (root != nullptr) {
|
|
freeTree(root->left);
|
|
freeTree(root->right);
|
|
delete root;
|
|
}
|
|
}
|