// Binary Search Tree interface #ifndef _BST_H #define _BST_H #include #include using namespace std; /* Binary search tree class */ template class BST { public: BST(); // create an empty binary search tree BST(const BST& src); // copy constructor ~BST(); // destructor const BST& operator=(const BST& src); // copy assignment operator void add(const T& value); // add value to binary search tree T getMin() const; // return smallest value in the binary search tree bool isEmpty() const; // return true if the binary search tree is empty, false otherwise bool contains(const T& value) const; // return true if value is in the tree, false otherwise void remove(const T& value); // if value is in the tree, remove it int size() const; // return number of nodes in tree int getHeight() const; // return height of tree vector inOrder() const; // add tree's node values to a vector during an inorder traversal vector preOrder() const; // add tree's node values to a vector during a preorder traversal vector postOrder() const; // add tree's node values to a vector during a postorder traversal bool inTree(const T& value) const; // return true if value is in the tree, and false otherwise private: struct TreeNode { T data; TreeNode* right; TreeNode* left; TreeNode(T d, TreeNode* r = NULL, TreeNode* l = NULL) { data = d; right = r; left = l; } }; TreeNode* root; /* Helper functions */ void add(TreeNode* node, const T& value); T getMin(TreeNode* node) const; bool contains(TreeNode* node, const T& value) const; void remove(TreeNode*& node, const T& value); void destroy(TreeNode* node); void inOrder(TreeNode* node, vector& result) const; void preOrder(TreeNode* node, vector& result) const; void postOrder(TreeNode* node, vector& result)const; int size(TreeNode* node) const; int getHeight(TreeNode* node) const; // Make a deep copy of the tree rooted at originalTree. Helper for copy assignment operator and // copy constructor. void copyTree(TreeNode*& copy, const TreeNode* originalTree); }; template bool BST::inTree(const T& value) const { // YOUR CODE GOES HERE } template BST::BST() { // YOUR CODE GOES HERE } template void BST::add(const T& value) { // YOUR CODE GOES HERE } template void BST::add(TreeNode* node, const T& value) { // YOUR CODE GOES HERE } template T BST::getMin() const { // YOUR CODE GOES HERE } template T BST::getMin(TreeNode* node) const { // YOUR CODE GOES HERE } template bool BST::contains(const T& value) const { // YOUR CODE GOES HERE } template bool BST::contains(TreeNode* node, const T& value) const { // YOUR CODE GOES HERE } template void BST::remove(const T& value) { // YOUR CODE GOES HERE } template void BST::remove(TreeNode*& node, const T& value) { // YOUR CODE GOES HERE } template BST::~BST() { // YOUR CODE GOES HERE } template void BST::destroy(TreeNode* node) { // YOUR CODE GOES HERE } template bool BST::isEmpty() const { // YOUR CODE GOES HERE } // copy assignment operator template const BST& BST::operator=(const BST& rhs) { // YOUR CODE GOES HERE } // copy constructor template BST::BST(const BST& rhs) { // YOUR CODE GOES HERE } template vector BST::inOrder() const { // YOUR CODE GOES HERE } template void BST::inOrder(TreeNode* node, vector& result)const { // YOUR CODE GOES HERE } template vector BST::preOrder() const{ // YOUR CODE GOES HERE } template void BST::preOrder(TreeNode* node, vector& result)const { // YOUR CODE GOES HERE } template vector BST::postOrder() const{ // YOUR CODE GOES HERE } template void BST::postOrder(TreeNode* node, vector& result) const{ // YOUR CODE GOES HERE } template int BST::size() const { // YOUR CODE GOES HERE } template int BST::size(TreeNode* node) const { // YOUR CODE GOES HERE } template int BST::getHeight() const { // YOUR CODE GOES HERE } template int BST::getHeight(TreeNode* node) const{ // YOUR CODE GOES HERE } template void BST::copyTree(TreeNode*& copy, const TreeNode* originalTree) { // YOUR CODE GOES HERE } #endif