From 42ba63e654fddc33732d4fec014e1979826ab0fa Mon Sep 17 00:00:00 2001 From: Iurii Tatishchev Date: Fri, 3 May 2024 15:11:07 -0700 Subject: [PATCH] 8.12 Lab: BT <--- BST (Search) Implement the pair of search functions for searching the BST: - _search - recursive (private function) - search - wrapper for _search (public function) --- 05-trees/BinarySearchTree.h | 48 ++++++++++++++++++++++++++++++------- 05-trees/BinaryTree.h | 3 ++- 05-trees/main.cpp | 27 ++++++++++++++++----- 3 files changed, 63 insertions(+), 15 deletions(-) diff --git a/05-trees/BinarySearchTree.h b/05-trees/BinarySearchTree.h index 5043a28..242d1fc 100644 --- a/05-trees/BinarySearchTree.h +++ b/05-trees/BinarySearchTree.h @@ -16,7 +16,8 @@ public: // remove a node if found //bool remove(const ItemType &item); // find a target node - //bool search(const ItemType &target, ItemType &returnedItem) const; + bool search(const ItemType &target, ItemType &returnedItem) const; + // find the smallest node bool findSmallest(ItemType &returnedItem) const; @@ -28,12 +29,12 @@ private: BinaryNode *_insert(BinaryNode *nodePtr, BinaryNode *newNode); // search for target node - //BinaryNode* _search(BinaryNode* treePtr, const ItemType &target) const; + BinaryNode *_search(BinaryNode *treePtr, const ItemType &target) const; // find the smallest node BinaryNode *_findSmallest(BinaryNode *nodePtr, ItemType &smallest) const; - // find the biggest node + // find the largest node BinaryNode *_findLargest(BinaryNode *nodePtr, ItemType &smallest) const; // internal remove node: locate and delete target node under nodePtr subtree @@ -48,7 +49,7 @@ private: }; ///////////////////////// public function definitions /////////////////////////// -// Inserting items within a tree +// Wrapper for _insert - Inserting items within a tree template bool BinarySearchTree::insert(const ItemType &newEntry) { BinaryNode *newNodePtr = new BinaryNode(newEntry); @@ -56,7 +57,6 @@ bool BinarySearchTree::insert(const ItemType &newEntry) { return true; } - // Finding the smallest, which is the leftmost leaf (wrapper function) template bool BinarySearchTree::findSmallest(ItemType &returnedItem) const { @@ -65,7 +65,7 @@ bool BinarySearchTree::findSmallest(ItemType &returnedItem) const { return temp != nullptr; } -// Finding the biggest, which is the rightmost leaf (wrapper function) +// Finding the largest, which is the rightmost leaf (wrapper function) template bool BinarySearchTree::findLargest(ItemType &returnedItem) const { BinaryNode *temp = nullptr; @@ -73,9 +73,26 @@ bool BinarySearchTree::findLargest(ItemType &returnedItem) const { return temp != nullptr; } +// Wrapper for _search +// - it calls the private _search function that returns a Node pointer or NULL +// - if found, it copies data from that node and sends it back to the caller +// via the output parameter, and returns true, otherwise it returns false. +template +bool BinarySearchTree::search(const ItemType &anEntry, ItemType &returnedItem) const { + BinaryNode *temp = nullptr; + temp = _search(this->rootPtr, anEntry); + + if (temp != nullptr) { + returnedItem = temp->getItem(); + return true; + } + return false; +} + + //////////////////////////// private functions //////////////////////////////////////////// -// Implementation of the insert operation - iterative algorithm +// Implementation of the insert operation: iterative algorithm template BinaryNode *BinarySearchTree::_insert(BinaryNode *nodePtr, BinaryNode *newNodePtr) { @@ -124,4 +141,19 @@ BinaryNode *BinarySearchTree::_findLargest(BinaryNodegetRightPtr(), biggest); } -#endif \ No newline at end of file +// Implementation for the search operation: recursive algorithm +// - return NULL if target not found, otherwise +// - returns a pointer to the node that matched the target +template +BinaryNode *BinarySearchTree::_search(BinaryNode *nodePtr, + const ItemType &target) const { + // Two base cases: root is NULL or target is found + if (nodePtr == nullptr) return nullptr; + if (nodePtr->getItem() == target) return nodePtr; + + // Recursive cases, search either left or right subtree based on the target + if (target < nodePtr->getItem()) return _search(nodePtr->getLeftPtr(), target); + if (target > nodePtr->getItem()) return _search(nodePtr->getRightPtr(), target); +} + +#endif diff --git a/05-trees/BinaryTree.h b/05-trees/BinaryTree.h index ca72414..f910bda 100644 --- a/05-trees/BinaryTree.h +++ b/05-trees/BinaryTree.h @@ -45,8 +45,9 @@ public: // abstract functions to be implemented by derived class virtual bool insert(const ItemType &newData) = 0; + //virtual bool remove(const ItemType &data) = 0; - //virtual bool search(const ItemType &target, ItemType & returnedItem) const = 0; + virtual bool search(const ItemType &target, ItemType &returnedItem) const = 0; private: // delete all nodes from the tree diff --git a/05-trees/main.cpp b/05-trees/main.cpp index 469a660..1e09de9 100644 --- a/05-trees/main.cpp +++ b/05-trees/main.cpp @@ -25,12 +25,22 @@ int main() { buildBST(n, bst); - cout << " Inorder: "; + cout << "Inorder: "; bst.inOrder(hDisplay); cout << endl; + cout << "Search Section:" << endl; + int searchCount = n / 2; + int target, item; + while (searchCount--) { + target = rand() % 30 + 10; + cout << target; + if (bst.search(target, item)) { + cout << " FOUND! Data contains: " + << item << endl; + } else + cout << " NOT FOUND!\n"; + } - cout << "Indented Tree:" << endl; - bst.printTree(iDisplay); return 0; } @@ -39,11 +49,16 @@ int main() { of integers */ void buildBST(int n, BinarySearchTree &bst) { + bool duplicate[41] = {false}; // all are set to 0 (false) int item; - while (n--) { - item = rand() % 30 + 10; - bst.insert(item); + while (n > 0) { + item = rand() % 31 + 10; + if (duplicate[item] == false) { // not a duplicate + bst.insert(item); + n--; + duplicate[item] = true; + } } }