Page 1 of 1

I need help with C++ I have completed most of the work but am stuck on a couple of problems. The comments are how I need

Posted: Mon Jun 06, 2022 4:47 pm
by answerhappygod
I need help with C++ I have completed most of the work but am
stuck on a couple of problems. The comments are how I need to
complete the work. The code contains empty methods representing the
programming interface used to interact with a binary search tree. I
will need help to add logic to the methods to implement the
necessary behavior. I have bolden the question I have, again I have
completed most of the work. Here is the public API for
BinarySearchTree.cpp that I have to complete
#include <iostream>
#include <time.h>
#include <algorithm>
#include <string>
#include "CSVparser.hpp"
using namespace std;
// forward declarations
double strToDouble(string str, char ch);
// define a structure to hold bid information
struct Bid {
string bidId; // unique identifier
string title;
string fund;
double amount;
Bid() {
amount = 0.0;
}
};
// Internal structure for tree node
struct Node {
Bid bid;
Node* left;
Node* right;
// default constructor
Node() {
left = nullptr;
right = nullptr;
}
// initialize with a bid
Node(Bid aBid) :
Node() {
bid = aBid;
}
};
class BinarySearchTree {
private:
Node* root;
void addNode(Node* node, Bid bid);
void inOrder(Node* node);
Node* removeNode(Node* node, string bidId);
public:
BinarySearchTree();
virtual ~BinarySearchTree();
void InOrder();
void Insert(Bid bid);
void Remove(string bidId);
Bid Search(string bidId);
};
/**
* Default constructor
*/
BinarySearchTree::BinarySearchTree() {
// FixMe (1): initialize housekeeping variables
//root is equal to nullptr
root = nullptr;
}
/**
* Destructor
*/
BinarySearchTree::~BinarySearchTree() {
// recurse from root deleting every node
}
/**
* Traverse the tree in order
*/
void BinarySearchTree::InOrder() {
// FixMe (2): In order root
// call inOrder fuction and pass root
this->inOrder(root);
}
/**
* Traverse the tree in post-order
*/
void BinarySearchTree::PostOrder() {
// FixMe (3): Post order root
// postOrder root
}
/**
* Traverse the tree in pre-order
*/
void BinarySearchTree::PreOrder() {
// FixMe (4): Pre order root
// preOrder root
}
/**
* Insert a bid
*/
void BinarySearchTree::Insert(Bid bid) {
// FIXME (5) Implement inserting a bid into the
tree
// if root equarl to null ptr
// root is equal to new node bid
// else
// add Node root and bid
if (root == nullptr) {
root = new Node(bid);
}
else {
this->addNode(root, bid);
}
}
/**
* Remove a bid
*/
void BinarySearchTree::Remove(string bidId) {
// FIXME (6) Implement removing a bid from the
tree
// remove node root bidID
this->removeNode(root, bidId);
}
/**
* Search for a bid
*/
Bid BinarySearchTree::Search(string bidId) {
// FIXME (7) Implement searching the tree for a
bid
// set current node equal to root
// keep looping downwards until bottom reached or
matching bidId found
// if match found, return current
bid
// if bid is smaller than current
node then traverse left
// else larger so traverse right
// start searching from the root
Node* current = root;
// keep looping downwards until bottom reached or
bid found
while (current != nullptr) {
//if current node matches, return
it
if
(current->bid.bidId.compare(bidId) == 0) {
return
current->bid;
}
//if bid is smaller than current then
traverse left
if
(bidId.compare(current->bid.bidId) < 0) {
current =
current->left;
}
else {
current =
current->right;
}
}
Bid bid;
return bid;
}
/**
* Add a bid to some node (recursive)
*
* @param node Current node in tree
* @param bid Bid to be added
*/
void BinarySearchTree::addNode(Node* node, Bid bid) {
// FIXME (8) Implement inserting a bid into the
tree
// if node is larger then add to left
// if no left node
// this node becomes
left
// else recurse down the left
node
// else
// if no right node
// this node becomes
right
//else
// recurse down the left
node
//if node is larger than bid, add to left
subtree
if (node->bid.bidId.compare(bid.bidId) > 0)
{
if (node->left == nullptr) {
node->left = new
Node(bid);
}
else {

this->addNode(node->left, bid);
}
}
//add to right subtree
else {
if (node->right == nullptr) {
node->right = new
Node(bid);
}
else {

this->addNode(node->right, bid);
}
}
}
void BinarySearchTree::inOrder(Node* node) {
// FixMe (9): Pre order root
//if node is not equal to null ptr
//InOrder not left
//output bidID, title, amount, fund
//InOder right
if (node != nullptr) {
inOrder(node->left); // Traverse
left side first
cout << node->bid.bidId
<< ": " << node->bid.title << " | " <<
node->bid.amount << " | "
<<
node->bid.fund << endl;
inOrder(node->right); // Traverse
right side after each level's been checked
}
}
void BinarySearchTree::postOrder(Node* node) {
// FixMe (10): Pre order root
//if node is not equal to null ptr
//postOrder left
//postOrder right
//output bidID, title, amount, fund
}
void BinarySearchTree::preOrder(Node* node) {
// FixMe (11): Pre order root
//if node is not equal to null ptr
//output bidID, title, amount, fund
//postOrder left
//postOrder right
}
/**
* Display the bid information to the console (std::out)
*
* @param bid struct containing the bid info
*/
void displayBid(Bid bid) {
cout << bid.bidId << ": " <<
bid.title << " | " << bid.amount << " | "
<< bid.fund << endl;
return;
}
/**
* Load a CSV file containing bids into a container
*
* @param csvPath the path to the CSV file to load
* @return a container holding all the bids read
*/
void loadBids(string csvPath, BinarySearchTree* bst) {
cout << "Loading CSV file " << csvPath
<< endl;
// initialize the CSV Parser using the given
path
csv::Parser file = csv::Parser(csvPath);
// read and display header row - optional
vector<string> header = file.getHeader();
for (auto const& c : header) {
cout << c << " | ";
}
cout << "" << endl;
try {
// loop to read rows of a CSV
file
for (unsigned int i = 0; i <
file.rowCount(); i++) {
// Create a data
structure and add to the collection of bids
Bid bid;
bid.bidId =
file[1];
bid.title =
file[0];
bid.fund =
file[8];
bid.amount =
strToDouble(file[4], '$');
//cout << "Item:
" << bid.title << ", Fund: " << bid.fund <<
", Amount: " << bid.amount << endl;
// push this bid to
the end

bst->Insert(bid);
}
}
catch (csv::Error& e) {
std::cerr << e.what() <<
std::endl;
}
}
/**
* Simple C function to convert a string to a double
* after stripping out unwanted char
*
* credit: http://stackoverflow.com/a/24875936
*
* @param ch The character to strip out
*/
double strToDouble(string str, char ch) {
str.erase(remove(str.begin(), str.end(), ch),
str.end());
return atof(str.c_str());
}
/**
* The one and only main() method
*/
int main(int argc, char* argv[]) {
// process command line arguments
string csvPath, bidKey;
switch (argc) {
case 2:
csvPath = argv[1];
bidKey = "98109";
break;
case 3:
csvPath = argv[1];
bidKey = argv[2];
break;
default:
csvPath =
"eBid_Monthly_Sales_Dec_2016.csv";
bidKey = "98109";
}
// Define a timer variable
clock_t ticks;
// Define a binary search tree to hold all
bids
BinarySearchTree* bst;
bst = new BinarySearchTree();
Bid bid;
int choice = 0;
while (choice != 9) {
cout << "Menu:" <<
endl;
cout << " 1. Load Bids"
<< endl;
cout << " 2. Display All
Bids" << endl;
cout << " 3. Find Bid"
<< endl;
cout << " 4. Remove Bid"
<< endl;
cout << " 9. Exit" <<
endl;
cout << "Enter choice: ";
cin >> choice;
cout << "Good bye." << endl;
return 0;
}