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)
This commit is contained in:
parent
0bc2b9690f
commit
42ba63e654
@ -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<ItemType> *_insert(BinaryNode<ItemType> *nodePtr, BinaryNode<ItemType> *newNode);
|
||||
|
||||
// search for target node
|
||||
//BinaryNode<ItemType>* _search(BinaryNode<ItemType>* treePtr, const ItemType &target) const;
|
||||
BinaryNode<ItemType> *_search(BinaryNode<ItemType> *treePtr, const ItemType &target) const;
|
||||
|
||||
// find the smallest node
|
||||
BinaryNode<ItemType> *_findSmallest(BinaryNode<ItemType> *nodePtr, ItemType &smallest) const;
|
||||
|
||||
// find the biggest node
|
||||
// find the largest node
|
||||
BinaryNode<ItemType> *_findLargest(BinaryNode<ItemType> *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<class ItemType>
|
||||
bool BinarySearchTree<ItemType>::insert(const ItemType &newEntry) {
|
||||
BinaryNode<ItemType> *newNodePtr = new BinaryNode<ItemType>(newEntry);
|
||||
@ -56,7 +57,6 @@ bool BinarySearchTree<ItemType>::insert(const ItemType &newEntry) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
// Finding the smallest, which is the leftmost leaf (wrapper function)
|
||||
template<class ItemType>
|
||||
bool BinarySearchTree<ItemType>::findSmallest(ItemType &returnedItem) const {
|
||||
@ -65,7 +65,7 @@ bool BinarySearchTree<ItemType>::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<class ItemType>
|
||||
bool BinarySearchTree<ItemType>::findLargest(ItemType &returnedItem) const {
|
||||
BinaryNode<ItemType> *temp = nullptr;
|
||||
@ -73,9 +73,26 @@ bool BinarySearchTree<ItemType>::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<class ItemType>
|
||||
bool BinarySearchTree<ItemType>::search(const ItemType &anEntry, ItemType &returnedItem) const {
|
||||
BinaryNode<ItemType> *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<class ItemType>
|
||||
BinaryNode<ItemType> *BinarySearchTree<ItemType>::_insert(BinaryNode<ItemType> *nodePtr,
|
||||
BinaryNode<ItemType> *newNodePtr) {
|
||||
@ -124,4 +141,19 @@ BinaryNode<ItemType> *BinarySearchTree<ItemType>::_findLargest(BinaryNode<ItemTy
|
||||
return _findLargest(nodePtr->getRightPtr(), biggest);
|
||||
}
|
||||
|
||||
#endif
|
||||
// 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<class ItemType>
|
||||
BinaryNode<ItemType> *BinarySearchTree<ItemType>::_search(BinaryNode<ItemType> *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
|
||||
|
@ -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
|
||||
|
@ -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<int> &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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user