/* Unit 2: BST * * - Written in template format for flexibility * * - In project, BST holds primary key of CPU IDs * * - Sorted based off string comparison * * - Has insert, search, and remove functionality * * - Has multiple transversal functions (inorder, preorder, postorder) * * - Has printTree function that prints all nodes in function * * - In project, function pointer is used to print in indented format * * Written By: Tuhin Mondal */ #ifndef INC_08_TEAM_PROJECT_BINARYSEARCHTREE_H #define INC_08_TEAM_PROJECT_BINARYSEARCHTREE_H #include "BinaryTree.h" template class BinarySearchTree: public BinaryTree { private: BinaryTreeNode* _insert(BinaryTreeNode* nodePtr, BinaryTreeNode* newNode); BinaryTreeNode* _search(BinaryTreeNode* treePtr, const T& target) const; BinaryTreeNode* _remove(BinaryTreeNode* nodePtr, const T& target); public: void insert(const T& item); bool remove(const T& item); bool search(const T& target, T& returnedItem) const; }; // Caller function for private _insert() method template void BinarySearchTree::insert(const T& newEntry) { BinaryTreeNode* newNodePtr = new BinaryTreeNode(newEntry); this->root = _insert(this->root, newNodePtr); this->size++; } // Inserts and sorts a new node into BST template BinaryTreeNode* BinarySearchTree::_insert(BinaryTreeNode* nodePtr, BinaryTreeNode* newNodePtr) { BinaryTreeNode* pWalk = nodePtr, * parent = nullptr; if (!nodePtr) { nodePtr = newNodePtr; return nodePtr; } else { while (pWalk) { parent = pWalk; if (pWalk->getItem() > newNodePtr->getItem()) pWalk = pWalk->getLeftPtr(); else pWalk = pWalk->getRightPtr(); } if (parent->getItem() > newNodePtr->getItem()) parent->setLeftPtr(newNodePtr); else parent->setRightPtr(newNodePtr); } return nodePtr; } // Caller function for private _remove() function template bool BinarySearchTree::remove(const T& item) { BinaryTreeNode* removed = _remove(this->root, item); if (removed) { this->size--; return true; } return false; } // Removes a node from BST template BinaryTreeNode* BinarySearchTree::_remove(BinaryTreeNode* nodePtr, const T& target) { BinaryTreeNode* parNode = nullptr; BinaryTreeNode* currNode = nodePtr; while (currNode) { if (currNode->getItem() == target) { if (!currNode->getLeftPtr() && !currNode->getRightPtr()) { // if target is leaf node if (!parNode) this->root = nullptr; else if (parNode->getLeftPtr() == currNode) parNode->setLeftPtr(nullptr); else parNode->setRightPtr(nullptr); } else if (!currNode->getRightPtr()) { // if target has only left child if (!parNode) this->root = currNode->getLeftPtr(); else if (parNode->getLeftPtr() == currNode) parNode->setLeftPtr(currNode->getLeftPtr()); else parNode->setRightPtr(currNode->getLeftPtr()); } else if (!currNode->getLeftPtr()) { // if target has only right child if (!parNode) this->root = currNode->getRightPtr(); else if (parNode->getLeftPtr() == currNode) parNode->setLeftPtr(currNode->getRightPtr()); else parNode->setRightPtr(currNode->getRightPtr()); } else { // if target has both left and right child BinaryTreeNode* sucNode = currNode->getRightPtr(); while (sucNode->getLeftPtr()) sucNode = sucNode->getLeftPtr(); T sucData = sucNode->getItem(); _remove(this->root, sucData); currNode->setItem(sucData); } return currNode; } else if (currNode->getItem() < target) { parNode = currNode; currNode = currNode->getRightPtr(); } else { parNode = currNode; currNode = currNode->getLeftPtr(); } } return nullptr; } // Caller function for private _search() function template bool BinarySearchTree::search(const T& target, T& returnedItem) const { BinaryTreeNode* temp = nullptr; temp = _search(this->root, target); if (temp) { returnedItem = temp->getItem(); return true; } return false; } // Searches for a node and returns it if found. template BinaryTreeNode* BinarySearchTree::_search(BinaryTreeNode* nodePtr, const T& target) const { BinaryTreeNode* found = nullptr; if (nodePtr) { if (target == nodePtr->getItem()) found = nodePtr; else if (target < nodePtr->getItem()) found = _search(nodePtr->getLeftPtr(), target); else found = _search(nodePtr->getRightPtr(), target); } return found; } #endif //INC_08_TEAM_PROJECT_BINARYSEARCHTREE_H