The Binary Search Tree (BST) is a popular data structure that organizes your data (e.g., numbers, employee records) in a hierarchical manner. In this particular project, we provide a versatile BST template class, allowing you to work with any type of data that supports basic comparison operations.
- Custom Data Types Support: This BST supports not only primitive types like integers but also custom data types like Employee.
- Easy Navigation: Perform various standard operations like finding the height, checking if the tree is balanced, and much more.
- Traversal Options: Traverse the tree using different orders, including preorder.
- Modification: Add, remove, or search for entries with ease.
You can create a BST in multiple ways:
- Default constructor: Creates an empty tree.
- Parameterized constructor: Initializes the tree with a root item.
- Copy constructor: Makes a copy of another tree.
- Constructor with a vector: Builds the tree from a given vector of items.
add: Add an item to the tree.remove: Remove a specific item from the tree.contains: Check if a specific item is present in the tree.getHeight: Get the height of the tree.getNumberOfNodes: Get the number of nodes in the tree.isEmpty: Check if the tree is empty.displayPreorder: Show the preorder traversal of the tree.
isBalanced: Check if the tree is balanced.isComplete: Check if the tree is complete.isFull: Check if the tree is full.getDirectSupervisor: Get the direct supervisor for employees, assuming the tree contains Employee objects.
#include "BinarySearchTree.hpp"
#include "Employee.hpp"
int main() {
BinarySearchTree<int> myTree;
myTree.add(42);
myTree.add(17);
myTree.add(65);
if (myTree.contains(42)) {
std::cout << "Found 42!" << std::endl;
}
myTree.displayPreorder();
}
-
C++compiler with support for C++11 or later. -
Employee class, if you wish to utilize the getDirectSupervisor method.
- Feel free to contribute to this project. Open an issue or pull request.