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:
parent
2ff40ef4e9
commit
8e55246e9c
@ -12,14 +12,16 @@ class BinarySearchTree : public BinaryTree<ItemType> {
|
|||||||
public:
|
public:
|
||||||
// insert a node at the correct location
|
// insert a node at the correct location
|
||||||
bool insert(const ItemType &item);
|
bool insert(const ItemType &item);
|
||||||
|
|
||||||
// remove a node if found
|
// remove a node if found
|
||||||
//bool remove(const ItemType &item);
|
//bool remove(const ItemType &item);
|
||||||
// find a target node
|
// find a target node
|
||||||
//bool search(const ItemType &target, ItemType &returnedItem) const;
|
//bool search(const ItemType &target, ItemType &returnedItem) const;
|
||||||
// find the smallest node
|
// find the smallest node
|
||||||
//bool findSmallest(ItemType &returnedItem) const;
|
bool findSmallest(ItemType &returnedItem) const;
|
||||||
|
|
||||||
// find the largest node
|
// find the largest node
|
||||||
//bool findLargest(ItemType &returnedItem) const;
|
bool findLargest(ItemType &returnedItem) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// internal insert node: insert newNode in nodePtr subtree
|
// internal insert node: insert newNode in nodePtr subtree
|
||||||
@ -29,10 +31,10 @@ private:
|
|||||||
//BinaryNode<ItemType>* _search(BinaryNode<ItemType>* treePtr, const ItemType &target) const;
|
//BinaryNode<ItemType>* _search(BinaryNode<ItemType>* treePtr, const ItemType &target) const;
|
||||||
|
|
||||||
// find the smallest node
|
// 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
|
// 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
|
// internal remove node: locate and delete target node under nodePtr subtree
|
||||||
// BinaryNode<ItemType>* _remove(BinaryNode<ItemType>* nodePtr, const ItemType target, bool &success);
|
// BinaryNode<ItemType>* _remove(BinaryNode<ItemType>* nodePtr, const ItemType target, bool &success);
|
||||||
@ -45,7 +47,6 @@ private:
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
///////////////////////// public function definitions ///////////////////////////
|
///////////////////////// public function definitions ///////////////////////////
|
||||||
// Inserting items within a tree
|
// Inserting items within a tree
|
||||||
template<class ItemType>
|
template<class ItemType>
|
||||||
@ -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 ////////////////////////////////////////////
|
//////////////////////////// private functions ////////////////////////////////////////////
|
||||||
|
|
||||||
//Implementation of the insert operation
|
// Implementation of the insert operation - iterative algorithm
|
||||||
template<class ItemType>
|
template<class ItemType>
|
||||||
BinaryNode<ItemType> *BinarySearchTree<ItemType>::_insert(BinaryNode<ItemType> *nodePtr,
|
BinaryNode<ItemType> *BinarySearchTree<ItemType>::_insert(BinaryNode<ItemType> *nodePtr,
|
||||||
BinaryNode<ItemType> *newNodePtr) {
|
BinaryNode<ItemType> *newNodePtr) {
|
||||||
@ -87,4 +103,25 @@ BinaryNode<ItemType> *BinarySearchTree<ItemType>::_insert(BinaryNode<ItemType> *
|
|||||||
return nodePtr;
|
return nodePtr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
#endif
|
@ -1,5 +1,7 @@
|
|||||||
// BST ADT - Traversals
|
// BST ADT
|
||||||
|
// Smallest/Largest
|
||||||
// Name: Iurii Tatishchev
|
// Name: Iurii Tatishchev
|
||||||
|
|
||||||
#include "BinarySearchTree.h"
|
#include "BinarySearchTree.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
@ -21,27 +23,25 @@ int main() {
|
|||||||
|
|
||||||
cout << "What is the number of nodes in the BST? " << endl;
|
cout << "What is the number of nodes in the BST? " << endl;
|
||||||
cin >> n;
|
cin >> n;
|
||||||
cout << "What traversal[prE/posT]? " << endl;
|
cout << "Find Smallest or Largest[S/L]? " << endl;
|
||||||
cin >> option;
|
cin >> option;
|
||||||
|
|
||||||
buildBST(n, bst);
|
buildBST(n, bst);
|
||||||
|
|
||||||
|
if (n < 15) {
|
||||||
cout << " Inorder: ";
|
cout << " Inorder: ";
|
||||||
bst.inOrder(hDisplay); // pass hDisplay to inOrder
|
bst.inOrder(hDisplay);
|
||||||
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
|
|
||||||
cout << endl;
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user