899c844c78
- Replaced std::set with std::vector for sequence handling. - Added randomization of sequences to avoid ordered input bias. - Removed unused balance function and related code in bst.cpp and bst.h. - Fixed bugs in list insertion and search logic. - Updated plot.py to allow custom y-axis labels and enable log scale for build plots.
43 lines
937 B
C++
43 lines
937 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 deleteTree(Tree *root) {
|
|
if (root != nullptr) {
|
|
deleteTree(root->left);
|
|
deleteTree(root->right);
|
|
delete root;
|
|
}
|
|
}
|
|
|
|
int getHeight(Tree *root, int height) {
|
|
if (root == nullptr)
|
|
return height;
|
|
height += 1;
|
|
int leftHeight = getHeight(root->left, height);
|
|
int rightHeight = getHeight(root->right, height);
|
|
return std::max(leftHeight, rightHeight) + 1;
|
|
}
|