Wrote BST tests and list structure
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
#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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user