diff --git a/05-trees/BinarySearchTree.h b/05-trees/BinarySearchTree.h index 75a9477..5043a28 100644 --- a/05-trees/BinarySearchTree.h +++ b/05-trees/BinarySearchTree.h @@ -12,14 +12,16 @@ class BinarySearchTree : public BinaryTree { public: // insert a node at the correct location bool insert(const ItemType &item); + // remove a node if found //bool remove(const ItemType &item); // find a target node //bool search(const ItemType &target, ItemType &returnedItem) const; // find the smallest node - //bool findSmallest(ItemType &returnedItem) const; + bool findSmallest(ItemType &returnedItem) const; + // find the largest node - //bool findLargest(ItemType &returnedItem) const; + bool findLargest(ItemType &returnedItem) const; private: // internal insert node: insert newNode in nodePtr subtree @@ -29,25 +31,24 @@ private: //BinaryNode* _search(BinaryNode* treePtr, const ItemType &target) const; // find the smallest node - //BinaryNode* _findSmallest(BinaryNode* nodePtr, ItemType &smallest) const; + BinaryNode *_findSmallest(BinaryNode *nodePtr, ItemType &smallest) const; // find the biggest node - //BinaryNode* _findLargest(BinaryNode* nodePtr, ItemType &smallest) const; + BinaryNode *_findLargest(BinaryNode *nodePtr, ItemType &smallest) const; // internal remove node: locate and delete target node under nodePtr subtree // BinaryNode* _remove(BinaryNode* nodePtr, const ItemType target, bool &success); // delete target node from tree, called by internal remove node - //BinaryNode* _removeNode(BinaryNode* targetNodePtr); + // BinaryNode* _removeNode(BinaryNode* targetNodePtr); // remove the leftmost node in the left subtree of nodePtr - //BinaryNode* _removeLeftmostNode(BinaryNode* nodePtr, ItemType &successor); + // BinaryNode* _removeLeftmostNode(BinaryNode* nodePtr, ItemType &successor); }; - ///////////////////////// public function definitions /////////////////////////// -//Inserting items within a tree +// Inserting items within a tree template bool BinarySearchTree::insert(const ItemType &newEntry) { BinaryNode *newNodePtr = new BinaryNode(newEntry); @@ -56,10 +57,25 @@ bool BinarySearchTree::insert(const ItemType &newEntry) { } +// Finding the smallest, which is the leftmost leaf (wrapper function) +template +bool BinarySearchTree::findSmallest(ItemType &returnedItem) const { + BinaryNode *temp = nullptr; + temp = _findSmallest(this->rootPtr, returnedItem); + return temp != nullptr; +} + +// Finding the biggest, which is the rightmost leaf (wrapper function) +template +bool BinarySearchTree::findLargest(ItemType &returnedItem) const { + BinaryNode *temp = nullptr; + temp = _findLargest(this->rootPtr, returnedItem); + return temp != nullptr; +} //////////////////////////// private functions //////////////////////////////////////////// -//Implementation of the insert operation +// Implementation of the insert operation - iterative algorithm template BinaryNode *BinarySearchTree::_insert(BinaryNode *nodePtr, BinaryNode *newNodePtr) { @@ -87,4 +103,25 @@ BinaryNode *BinarySearchTree::_insert(BinaryNode * return nodePtr; } -#endif +// Implementation to find the smallest: recursive +template +BinaryNode * +BinarySearchTree::_findSmallest(BinaryNode *nodePtr, ItemType &smallest) const { + if (nodePtr->getLeftPtr() == nullptr) { + smallest = nodePtr->getItem(); + return nodePtr; + } + return _findSmallest(nodePtr->getLeftPtr(), smallest); +} + +// Implementation to find the largest: recursive +template +BinaryNode *BinarySearchTree::_findLargest(BinaryNode *nodePtr, ItemType &biggest) const { + if (nodePtr->getRightPtr() == nullptr) { + biggest = nodePtr->getItem(); + return nodePtr; + } + return _findLargest(nodePtr->getRightPtr(), biggest); +} + +#endif \ No newline at end of file diff --git a/05-trees/BinaryTree.h b/05-trees/BinaryTree.h index 8cefc92..d9653cc 100644 --- a/05-trees/BinaryTree.h +++ b/05-trees/BinaryTree.h @@ -11,7 +11,7 @@ template class BinaryTree { protected: BinaryNode *rootPtr; // ptr to root node - int count; // number of nodes in tree + int count; // number of nodes in tree public: // "admin" functions diff --git a/05-trees/main.cpp b/05-trees/main.cpp index 5f77e4c..d2385c4 100644 --- a/05-trees/main.cpp +++ b/05-trees/main.cpp @@ -1,5 +1,7 @@ -// BST ADT - Traversals +// BST ADT +// Smallest/Largest // Name: Iurii Tatishchev + #include "BinarySearchTree.h" #include #include @@ -21,27 +23,25 @@ int main() { cout << "What is the number of nodes in the BST? " << endl; cin >> n; - cout << "What traversal[prE/posT]? " << endl; + cout << "Find Smallest or Largest[S/L]? " << endl; cin >> option; buildBST(n, bst); - cout << " Inorder: "; - bst.inOrder(hDisplay); // pass hDisplay to inOrder - cout << endl; - if (option == 'T' || option == 't') { - cout << "Postorder: "; - bst.postOrder(hDisplay); // pass hDisplay to postOrder - cout << endl; - bst.postOrder(vDisplay); // pass vDisplay to postOrder - cout << endl; - } else if (option == 'E' || option == 'e') { - cout << " Preorder: "; - bst.preOrder(hDisplay); // pass hDisplay to preOrder - cout << endl; - bst.preOrder(vDisplay); // pass vDisplay to preOrder + if (n < 15) { + cout << " Inorder: "; + bst.inOrder(hDisplay); cout << endl; } + if (option == 'S' || option == 's') { + int minVal; + bst.findSmallest(minVal); + cout << "Smallest: " << minVal << endl; + } else if (option == 'L' || option == 'l') { + int maxVal; + bst.findLargest(maxVal); + cout << "Largest: " << maxVal << endl; + } return 0; }