1. (25 pts) Implement the insert method for binary search trees. Do not insert duplicate values. 2.
(15 pts) Implement the search method for binary search trees. 3. (15 pts) Implement isBST, a
method for checking whether or not a tree is a binary search tree. 4. (10 pts) Implement the pre-
order traversal method for binary search trees.
All of this should be implemented in the below code, in C++, without changing any of the
parameters or functions as they are laid out.
#include<iostream>
#include <vector>
#include <limits.h>
#include "BST.h"
BST::BST(){
}
BST::~BST(){}
std::shared_ptr<Node> BST::search(int target){
return nullptr;
}
std::shared_ptr<Node> BST::search(std::shared_ptr<Node> n, int target){
return nullptr;
}
std::shared_ptr<Node> BST::minimum(){
return nullptr;
}
std::shared_ptr<Node> BST::minimum(std::shared_ptr<Node> n){
return nullptr;
}
std::shared_ptr<Node> BST::maximum(){
return nullptr;
}
std::shared_ptr<Node> BST::maximum(std::shared_ptr<Node> n){
return nullptr;
}
void BST::insertValue(int val){
}
std::shared_ptr<Node> BST::insertValue(std::shared_ptr<Node> n, int val){
return nullptr;
}
void BST::deleteValue(int val){
}
std::shared_ptr<Node> BST::deleteValue(std::shared_ptr<Node> n, int val){
return nullptr;
}
bool BST::isBST(std::shared_ptr<Node> n){
return false;
}
bool BST::isBST(std::shared_ptr<Node> n, int low, int high){
return false;
}
void BST::preOrder(std::shared_ptr<Node> n, std::vector<std::shared_ptr<Node>>
&order){
}
void BST::inOrder(std::shared_ptr<Node> n, std::vector<std::shared_ptr<Node>>
&order){
}
void BST::postOrder(std::shared_ptr<Node> n, std::vector<std::shared_ptr<Node>>
&order){
}
The code above is meant to be grouped together with the below code:
BST.h:
// CSCI 311 - Spring 2023
// Lab 3 BST header
// Author: Carter Tillquist
#ifndef BST_H
#define BST_H
#include <memory>
#include <vector>
#include "Node.h"
class BST{
public:
std::shared_ptr<Node> root;
int size;
Node* left;
Node* right;
Node* data;
BST();
~BST();
std::shared_ptr<Node> search(int);
std::shared_ptr<Node> search(std::shared_ptr<Node>, int);
std::shared_ptr<Node> minimum();
std::shared_ptr<Node> minimum(std::shared_ptr<Node>);
std::shared_ptr<Node> maximum();
std::shared_ptr<Node> maximum(std::shared_ptr<Node>);
void insertValue(int);
std::shared_ptr<Node> insertValue(std::shared_ptr<Node>, int);
void deleteValue(int);
std::shared_ptr<Node> deleteValue(std::shared_ptr<Node>, int);
bool isBST(std::shared_ptr<Node>);
bool isBST(std::shared_ptr<Node>, int, int);
void preOrder(std::shared_ptr<Node>, std::vector<std::shared_ptr<Node>>&);
void inOrder(std::shared_ptr<Node>, std::vector<std::shared_ptr<Node>>&);
void postOrder(std::shared_ptr<Node>, std::vector<std::shared_ptr<Node>>&);
};
#endif
Node.cpp:
// CSCI 311 - Spring 2023
// Node Class
// Author: Carter Tillquist
#include "Node.h"
Node::Node(){
value = 0;
left = nullptr;
right = nullptr;
}
Node::Node(int v){
value = v;
left = nullptr;
right = nullptr;
}
Node::~Node(){}
Node.h
.
1- (25 pts) Implement the insert method for binary search trees- Do no.pdf
1. 1. (25 pts) Implement the insert method for binary search trees. Do not insert duplicate values. 2.
(15 pts) Implement the search method for binary search trees. 3. (15 pts) Implement isBST, a
method for checking whether or not a tree is a binary search tree. 4. (10 pts) Implement the pre-
order traversal method for binary search trees.
All of this should be implemented in the below code, in C++, without changing any of the
parameters or functions as they are laid out.
#include<iostream>
#include <vector>
#include <limits.h>
#include "BST.h"
BST::BST(){
}
BST::~BST(){}
std::shared_ptr<Node> BST::search(int target){
return nullptr;
}
std::shared_ptr<Node> BST::search(std::shared_ptr<Node> n, int target){
return nullptr;
}
std::shared_ptr<Node> BST::minimum(){
return nullptr;
}
std::shared_ptr<Node> BST::minimum(std::shared_ptr<Node> n){
return nullptr;
}
std::shared_ptr<Node> BST::maximum(){
return nullptr;
}
std::shared_ptr<Node> BST::maximum(std::shared_ptr<Node> n){
return nullptr;
}
void BST::insertValue(int val){
}
2. std::shared_ptr<Node> BST::insertValue(std::shared_ptr<Node> n, int val){
return nullptr;
}
void BST::deleteValue(int val){
}
std::shared_ptr<Node> BST::deleteValue(std::shared_ptr<Node> n, int val){
return nullptr;
}
bool BST::isBST(std::shared_ptr<Node> n){
return false;
}
bool BST::isBST(std::shared_ptr<Node> n, int low, int high){
return false;
}
void BST::preOrder(std::shared_ptr<Node> n, std::vector<std::shared_ptr<Node>>
&order){
}
void BST::inOrder(std::shared_ptr<Node> n, std::vector<std::shared_ptr<Node>>
&order){
}
void BST::postOrder(std::shared_ptr<Node> n, std::vector<std::shared_ptr<Node>>
&order){
}
The code above is meant to be grouped together with the below code:
BST.h:
// CSCI 311 - Spring 2023
// Lab 3 BST header
// Author: Carter Tillquist
#ifndef BST_H
#define BST_H
#include <memory>
#include <vector>
#include "Node.h"
4. right = nullptr;
}
Node::Node(int v){
value = v;
left = nullptr;
right = nullptr;
}
Node::~Node(){}
Node.h
// CSCI 311 - Spring 2023
// Node Header
// Author: Carter Tillquist
#ifndef NODE_H
#define NODE_H
#include <memory>
class Node{
public:
int value;
Node* left;
Node* right;
Node* data;
Node();
Node(int);
~Node();
};
#endif
BSTDriver.cpp
// CSCI 311 - Spring 2023
// Lab 3 - BST Driver
// Author: Carter Tillquist
#include <iostream>
#include <memory>
#include <vector>
5. #include "BST.h"
using namespace std;
void printVector(const vector<std::shared_ptr<Node>>& v);
void runSearch(std::shared_ptr<BST> T);
void runInsert(std::shared_ptr<BST> T);
void runDelete(std::shared_ptr<BST> T);
/**************************************************
* Simple main to test Binary Search Tree methods *
* ************************************************/
int main() {
std::shared_ptr<BST> T(new BST());
int operation;
cin >> operation;
while (operation > 0) {
vector<std::shared_ptr<Node>> order;
switch (operation) {
case 1: // search
cout << "SEARCH FOR ";
runSearch(T);
break;
case 2: // insert
cout << "INSERT ";
runInsert(T);
break;
case 3: // delete
cout << "DELETE ";
runDelete(T);
break;
case 4: // preorder
cout << "PREORDER" << endl;
T->preOrder(T->root, order);
printVector(order);
break;
case 5: // inorder
cout << "INORDER" << endl;
T->inOrder(T->root, order);
printVector(order);
break;
case 6: // postorder
cout << "POSTORDER" << endl;
T->postOrder(T->root, order);
printVector(order);
6. break;
case 7: // minimum
cout << "MINIMUM" << endl;
cout << T->minimum()->value << endl;
break;
case 8: // maximum
cout << "MAXIMUM" << endl;
cout << T->maximum()->value << endl;
break;
case 9: // is BST
cout << "IS BST" << endl;
cout << T->isBST(T->root) << endl;
break;
default:
break;
}
cin >> operation;
}
return 0;
}
/*****************************************************************
* Print the values of nodes in a vector *
* v - const vector<std::shared_ptr<Node>> & - a vector of Nodes *
* ***************************************************************/
void printVector(const vector<std::shared_ptr<Node>>& v) {
for (int i = 0; i < v.size(); i++) {
cout << v[i]->value << " ";
}
cout << endl;
}
/*****************************************************************************
**************
* Given a BST, get a value to search for from the console and apply the BST search method *
* T - std::shared_ptr<BST> - a Binary Search Tree *
*
******************************************************************************
***********/
void runSearch(std::shared_ptr<BST> T) {
int target;
cin >> target;
cout << target << endl;
std::shared_ptr<Node> n = T->search(target);
if (n) { cout << n->value << endl; }
7. else { cout << "Not found" << endl; }
}
/********************************************************************
* Given a BST, get a value from the console and add it to the tree *
* T - std::shared_ptr<BST> - a Binary Search Tree *
* ******************************************************************/
void runInsert(std::shared_ptr<BST> T) {
int newVal;
cin >> newVal;
cout << newVal << endl;
T->insertValue(newVal);
}
/*****************************************************************************
*********
* Given a BST, get a value from the console and remove it from the tree if it exists *
* T - std::shared_ptr<BST> - a Binary Search Tree *
*
******************************************************************************
******/
void runDelete(std::shared_ptr<BST> T) {
int remove;
cin >> remove;
cout << remove << endl;
T->deleteValue(remove);
}