/* BST312.h CS 312 Fall 2018 a simple implementation of a binary search tree */ #ifndef BST_312_H #define BST_312_H #include // Provides size_t #include #include using namespace std; template class BST_312 { public: BST_312(); //Copy constructor BST_312(const BST_312 & src); ~BST_312(); /**************************** makeEmpty Function: Removes all the items from the BST. Preconditions: BST has been initialized Postconditions: All the items have been removed *****************************/ void makeEmpty(); /**************************** isEmpty Function: Checks to see if there are any items in the BST. Preconditions: BST has been initialized Postconditions: Returns true if there are no items in the BST, else false. *****************************/ bool isEmpty() const; /**************************** isFull Function: Determines if you have any more room to add items to the BST Preconditions: BST has been initialized Postconditions: Returns true if there is no more room to add items, else false *****************************/ bool isFull() const; /**************************** insertItem Function: Adds newItem to the BST. Preconditions: BST has been initialized and is not full. Postconditions: newItem is in the proper position for a BST. *****************************/ void insertItem(const ItemType &); /**************************** deleteItem Function: Removes target from BST. Preconditions: BST has been initialized. Postconditions: If found, element has been removed from BST. *****************************/ void deleteItem(const ItemType& newItem); /**************************** countNodes Function: Counts the number of nodes in the BST Preconditions: BST has been initialized. Postconditions: returns the number of nodes in the BST *****************************/ int countNodes(); /**************************** preOrderTraversal Function: Returns the preOder (Node, Left, Right) as a vector of ItemTypes Preconditions: BST has been initialized. Postconditions: none *****************************/ vector preOrderTraversal(); /**************************** inOrderTraversal Function: Returns the inOder (Left, Node, Right) as a vector of ItemTypes Preconditions: BST has been initialized. Postconditions: none *****************************/ vector inOrderTraversal(); /**************************** postOrderTraversal Function: returns the postOder (Left, Right, Node) as a vector of ItemTypes Preconditions: BST has been initialized. Postconditions: none *****************************/ vector postOrderTraversal(); /******************** isItemInTree Function: Determines if a given Item is in tree. Preconditions: BST has been initialized. Postconditions: none *****************************/ bool isItemInTree(const ItemType& item); private: struct TreeNode { ItemType data; TreeNode *left; TreeNode *right; }; TreeNode * root; void insertItem(TreeNode*& t, const ItemType& newItem); void inOrderTraversal(TreeNode* t,vector& result) const; int countNodes(TreeNode* t) const; void deleteNode(TreeNode*& node); void makeEmpty(TreeNode*& t); void deleteItem(TreeNode*& t, const ItemType& newItem); void getPredecessor(TreeNode* t, ItemType& data); void preOrderTraversal(TreeNode* t,vector& result) const; void postOrderTraversal(TreeNode* t,vector& result) const; void copyTree(TreeNode*& copy, const TreeNode *originalTree); }; /******************************* //Function implementations ********************************/ template BST_312::BST_312 () { root = NULL; } template BST_312::BST_312(const BST_312 & src) { copyTree(root, src.root); } template BST_312::~BST_312() { makeEmpty(); } template void BST_312::copyTree(TreeNode*& copy, const TreeNode* originalTree) { if (originalTree == NULL) copy = NULL; else { copy = new TreeNode; copy->data = originalTree->data; copyTree(copy->left, originalTree->left); copyTree(copy->right, originalTree->right); } } template void BST_312 ::deleteNode(TreeNode*& t) { ItemType info; TreeNode *tempPtr; tempPtr = t; if (t->left == NULL) { t = t->right; delete tempPtr; } else if (t->right == NULL) { t = t->left; delete tempPtr; } else { getPredecessor(t->left, info); t->data = info; deleteItem(t->left, info); } } //find largest in left subtree template void BST_312 ::getPredecessor(TreeNode* t, ItemType& data) { while (t->right != NULL) t = t->right; data = t->data; } template void BST_312 ::deleteItem(TreeNode*& t, const ItemType& newItem) { if (t == NULL) return; else if (newItem < t->data) deleteItem(t->left, newItem); else if (newItem > t->data) deleteItem(t->right, newItem); else deleteNode(t); } template void BST_312 ::deleteItem(const ItemType& newItem) { deleteItem(root, newItem); } template void BST_312 ::makeEmpty(TreeNode*& t) { //YOUR CODE GOES HERE } template void BST_312 ::makeEmpty() { makeEmpty(root); root = NULL; } template bool BST_312 ::isEmpty() const { return root == NULL; } template bool BST_312 ::isFull() const { try { TreeNode *temp = new TreeNode; delete temp; return false; } catch (std::bad_alloc) { return true; } } template void BST_312 ::insertItem(TreeNode*& t, const ItemType& newItem) { //YOUR CODE GOES HERE } template void BST_312 ::insertItem(const ItemType& newItem) { //YOUR CODE GOES HERE } template int BST_312 ::countNodes(TreeNode* t) const { //YOUR CODE GOES HERE } template int BST_312 ::countNodes() { //YOUR CODE GOES HERE } template void BST_312 ::preOrderTraversal(TreeNode* t,vector& result) const { //YOUR CODE GOES HERE } template vector BST_312 ::preOrderTraversal() { //YOUR CODE GOES HERE } template void BST_312 ::inOrderTraversal(TreeNode* t,vector& result) const { //YOUR CODE GOES HERE } template vector BST_312 ::inOrderTraversal() { //YOUR CODE GOES HERE } template void BST_312 ::postOrderTraversal(TreeNode* t,vector& result) const { //YOUR CODE GOES HERE } template vector BST_312 ::postOrderTraversal() { //YOUR CODE GOES HERE } template bool BST_312 ::isItemInTree(const ItemType& item) { //YOUR CODE GOES HERE } #endif