8.8 Lab: BT <--- BST (Smallest/Largest)

The assignment consists of the following classes/files:

- BinaryNode.h (template, given)
- BinaryTree.h (template, incomplete)
- BinarySearchTree.h(template, incomplete)
- main.cpp (incomplete)

Write the following functions:

- findSmallest (recursive)
- findLargest (recursive)
This commit is contained in:
Iurii Tatishchev 2024-05-03 13:15:46 -07:00
parent 2ff40ef4e9
commit 8e55246e9c
Signed by: CaZzzer
GPG Key ID: 28BE602058C08557
3 changed files with 64 additions and 27 deletions

View File

@ -12,14 +12,16 @@ class BinarySearchTree : public BinaryTree<ItemType> {
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<ItemType>* _search(BinaryNode<ItemType>* treePtr, const ItemType &target) const;
// find the smallest node
//BinaryNode<ItemType>* _findSmallest(BinaryNode<ItemType>* nodePtr, ItemType &smallest) const;
BinaryNode<ItemType> *_findSmallest(BinaryNode<ItemType> *nodePtr, ItemType &smallest) const;
// find the biggest node
//BinaryNode<ItemType>* _findLargest(BinaryNode<ItemType>* nodePtr, ItemType &smallest) const;
BinaryNode<ItemType> *_findLargest(BinaryNode<ItemType> *nodePtr, ItemType &smallest) const;
// internal remove node: locate and delete target node under nodePtr subtree
// BinaryNode<ItemType>* _remove(BinaryNode<ItemType>* nodePtr, const ItemType target, bool &success);
// delete target node from tree, called by internal remove node
//BinaryNode<ItemType>* _removeNode(BinaryNode<ItemType>* targetNodePtr);
// BinaryNode<ItemType>* _removeNode(BinaryNode<ItemType>* targetNodePtr);
// remove the leftmost node in the left subtree of nodePtr
//BinaryNode<ItemType>* _removeLeftmostNode(BinaryNode<ItemType>* nodePtr, ItemType &successor);
// BinaryNode<ItemType>* _removeLeftmostNode(BinaryNode<ItemType>* nodePtr, ItemType &successor);
};
///////////////////////// public function definitions ///////////////////////////
//Inserting items within a tree
// Inserting items within a tree
template<class ItemType>
bool BinarySearchTree<ItemType>::insert(const ItemType &newEntry) {
BinaryNode<ItemType> *newNodePtr = new BinaryNode<ItemType>(newEntry);
@ -56,10 +57,25 @@ bool BinarySearchTree<ItemType>::insert(const ItemType &newEntry) {
}
// Finding the smallest, which is the leftmost leaf (wrapper function)
template<class ItemType>
bool BinarySearchTree<ItemType>::findSmallest(ItemType &returnedItem) const {
BinaryNode<ItemType> *temp = nullptr;
temp = _findSmallest(this->rootPtr, returnedItem);
return temp != nullptr;
}
// Finding the biggest, which is the rightmost leaf (wrapper function)
template<class ItemType>
bool BinarySearchTree<ItemType>::findLargest(ItemType &returnedItem) const {
BinaryNode<ItemType> *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<class ItemType>
BinaryNode<ItemType> *BinarySearchTree<ItemType>::_insert(BinaryNode<ItemType> *nodePtr,
BinaryNode<ItemType> *newNodePtr) {
@ -87,4 +103,25 @@ BinaryNode<ItemType> *BinarySearchTree<ItemType>::_insert(BinaryNode<ItemType> *
return nodePtr;
}
#endif
// Implementation to find the smallest: recursive
template<class ItemType>
BinaryNode<ItemType> *
BinarySearchTree<ItemType>::_findSmallest(BinaryNode<ItemType> *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<class ItemType>
BinaryNode<ItemType> *BinarySearchTree<ItemType>::_findLargest(BinaryNode<ItemType> *nodePtr, ItemType &biggest) const {
if (nodePtr->getRightPtr() == nullptr) {
biggest = nodePtr->getItem();
return nodePtr;
}
return _findLargest(nodePtr->getRightPtr(), biggest);
}
#endif

View File

@ -11,7 +11,7 @@ template<class ItemType>
class BinaryTree {
protected:
BinaryNode<ItemType> *rootPtr; // ptr to root node
int count; // number of nodes in tree
int count; // number of nodes in tree
public:
// "admin" functions

View File

@ -1,5 +1,7 @@
// BST ADT - Traversals
// BST ADT
// Smallest/Largest
// Name: Iurii Tatishchev
#include "BinarySearchTree.h"
#include <iostream>
#include <string>
@ -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;
}