Question
Using C++ I am having several problems with this code. I don't think it is reading my text file correctly. This is my .txt file.
Using C++
I am having several problems with this code. I don't think it is reading my text file correctly. This is my .txt file.
Here is the code I have, I know there are tons of errors. Please Help!!!
//Binary Search Tree Program
#include #include #include #include #include #include #include using namespace std;
typedef string treeType;
class Products { private: string name; int price; int quantity; public: Products(); Products(string name, int price, int quantity); string getName(); int getprice(); int getquantity(); void setName(string newname); void setprice(int newprice); void setquantity(int newquantity);
};
class BinarySearchTree { private: struct tree_node { string key; tree_node* left; tree_node* right; //treeType data; Products data;
}; tree_node* root;
public: BinarySearchTree() { root = NULL; }
bool isEmpty() const { return root == NULL; } void print_inorder(); void inorder(tree_node*); void print_preorder(); void preorder(tree_node*); void print_postorder(); void postorder(tree_node*); void levelOrder(tree_node* n); void insert(Products); void remove(string); void search(string key); void changeprice(string key, int newprice); void edit(string key, string newname, int newprice); void displaytree(tree_node *begin); };
Products::Products() { }
Products::Products(string newname, int newprice, int newquantity) { name = newname; price = newprice; quantity = newquantity; }
string Products::getName() { return name; }
int Products::getprice() { return price; }
int Products::getquantity() { return quantity; }
void Products::setName(string newname) { name = newname; }
void Products::setprice(int newprice) { price = newprice; }
void Products::setquantity(int newquantity) { quantity = newquantity; }
// Smaller elements go left // larger elements go right void BinarySearchTree::insert(Products p) { tree_node* t = new tree_node; tree_node* parent; t->data = p; t->left = NULL; t->right = NULL; parent = NULL;
// is this a new tree? if (isEmpty()) root = t; else { //Note: ALL insertions are as leaf nodes tree_node* current; current = root; // Find the Node's parent while (current) { parent = current; if (t->data.getName() > current->data.getName()) current = current->right; else current = current->left; }
if (t->data.getName() data.getName()) parent->left = t; else parent->right = t; } }
void BinarySearchTree::remove(string p) { //Locate the element bool found = false; if (isEmpty()) { cout
tree_node* current; tree_node* parent; current = root; parent = root;
while (current != NULL) { if (current->data.getName() == p) { found = true; break; } else { parent = current; if (p > current->data.getName()) current = current->right; else current = current->left; } } if (!found) { cout
// 3 cases : // 1. We're removing a leaf node // 2. We're removing a node with a single child // 3. we're removing a node with 2 children
// Node with single child if ((current->left == NULL && current->right != NULL) || (current->left != NULL && current->right == NULL)) { if (current->left == NULL && current->right != NULL) { if (current->left == current) { parent->left = current->right; delete current; } else { parent->right = current->right; delete current; } } else // left child present, no right child { if (parent->left == current) { parent->left = current->left; delete current; } else { parent->right = current->left; delete current; } } return; }
//We're looking at a leaf node if (current->left == NULL && current->right == NULL) { if (parent->left == current) { parent->left = NULL; } else { parent->right = NULL; } delete current; return; }
//Node with 2 children // replace node with smallest value in right subtree if (current->left != NULL && current->right != NULL) { tree_node* chkr; chkr = current->right; if ((chkr->left == NULL) && (chkr->right == NULL)) { current = chkr; delete chkr; current->right = NULL; } else // right child has children { //if the node's right child has a left child // Move all the way down left to locate smallest element
if ((current->right)->left != NULL) { tree_node* lcurr; tree_node* lcurrp; lcurrp = current->right; lcurr = (current->right)->left; while (lcurr->left != NULL) { lcurrp = lcurr; lcurr = lcurr->left; } current->data = lcurr->data; delete lcurr; lcurrp->left = NULL; } else { tree_node* tmp; tmp = current->right; current->data = tmp->data; current->right = tmp->right; delete tmp; }
} return; }
}
void BinarySearchTree::print_inorder() { inorder(root); }
void BinarySearchTree::inorder(tree_node* p) { if (p != NULL) { if (p->left) inorder(p->left); cout data.getName() right) inorder(p->right); } else return; }
void BinarySearchTree::edit(string p, string newname, int newprice) { bool found = false; if (isEmpty()) { cout
tree_node* curr; tree_node* parent; curr = root;
while (curr != NULL) { if (curr->data.getName() == p) { found = true; break; } else { parent = curr; if (p>curr->data.getName()) curr = curr->right; else curr = curr->left; } } if (!found) { cout
//change the phonenumber associated with the node curr->data.setName(newname); cout
void BinarySearchTree::print_preorder() { preorder(root); }
void BinarySearchTree::preorder(tree_node* p) { if (p != NULL) { cout data.getName() left) preorder(p->left); if (p->right) preorder(p->right); } else return; }
void BinarySearchTree::print_postorder() { postorder(root); }
void BinarySearchTree::postorder(tree_node* p) { if (p != NULL) { if (p->left) postorder(p->left); if (p->right) postorder(p->right); cout data.getName()
// Print the tree level-order assisted by queue void BinarySearchTree::levelOrder(tree_node* n) { // Create a queue queue q;
// Push the root q.push(n);
while (!q.empty()) { // Dequeue a node from front tree_node* v = q.front(); cout key
// Enqueue the left children if (v->left != NULL) { q.push(v->left); }
// Enqueue the right children if (v->right != NULL) { q.push(v->right); }
// Pop the visited node q.pop(); } }
void BinarySearchTree::search(string key) { bool found = false; if (isEmpty()) { cout
tree_node* curr; tree_node* parent; curr = root;
while (curr != NULL) { if (curr->data.getName() == key) { found = true; cout data.getprice() curr->data.getName()) curr = curr->right; else curr = curr->left; } } if (!found) { cout
void BinarySearchTree::changeprice(string p, int newprice) { bool found = false; if (isEmpty()) { cout
tree_node* curr; tree_node* parent; curr = root;
while (curr != NULL) { if (curr->data.getName() == p) { found = true; break; } else { parent = curr; if (p>curr->data.getName()) curr = curr->right; else curr = curr->left; } } if (!found) { cout
//change the price associated with the node curr->data.setprice(newprice); cout
void fillTree(BinarySearchTree *b) { ifstream file; file.open("productlist.txt"); if (!file) { cout > name >> price>> quantity) { p.setName(name); p.setprice(price); p.setquantity(quantity); cout
void BinarySearchTree::displaytree(tree_node *b) //this displays the items in the list at a given time. It is used throughout the whole program to keep the user informed { //about what is in the list. tree_node *p; if (b == NULL) { cout key right; } cout
} int main() {
BinarySearchTree b; int ch; string name; string key; key = name; int price; Products tmp; Products tmp1; fillTree(&b); b.print_preorder(); while (1) { cout > ch; switch (ch) { case 1: cout > name; cout > price; tmp.setName(name); tmp.setprice(price); b.insert(tmp); break; case 2: cout > name; cout > price; cout > key; b.search(key); break; case 4: cout > key; b.remove(key); b.print_preorder(); b.displaytree(); break; case 6: cout > key; cout
} } }
Binary Search Trees and Object-oriented Programming In this individual assignment, you will create a binary search tree for a product catalogue. The catalogue will be used to display products. The program's user will add and delete products as well as be able to create an order of products. A product class will consist of a name, its price and the number of items that the user is buying (all initial buying quantities are 0). Your products should be related so that program is selling related products. Your binary search tree class will be a collection class (like the list class in the previous project with .h and.cpp files). Your tree class will make use of node pointers/objects defined in a node class. The node class will consist of the product data and node pointers left and right. The tree class will have a root data attribute that will also be a node pointer When the program starts, the program should read from a textfile at least 16 products with their names, prices, and quantities being purchased (quantities initially should equal 0). As the data is read, it should be added into the binary search tree. Arrange the data so that the tree will be reasonably balanced. Use the name of the product as the sorting key for the binary search tree After the data has been loaded from the text file, a menu should appear that prompts the user to (1) add a product, (2) edit a product, (3) find and display a product, (4) view all products, (5) delete a product, (6) find and purchase a product, (7) display the current order, (8) clear the current order, or (9) quit the program When the program quits, the products in the current program, including those just added and having removed all those removed, are saved to the text file so that the updated collection of products will be loaded when the program runs the next time. The quantities should all be set to 0 when saving the text file Additional requirements (to practice traversals): Your tree of products should have a destructor that uses a post-order traversal strategy. The view all products menu option should use an in order traversal strategy. When displaying the current order, use a pre-order traversal strategy When clearing the current order use a level traversal strategy Binary Search Trees and Object-oriented Programming In this individual assignment, you will create a binary search tree for a product catalogue. The catalogue will be used to display products. The program's user will add and delete products as well as be able to create an order of products. A product class will consist of a name, its price and the number of items that the user is buying (all initial buying quantities are 0). Your products should be related so that program is selling related products. Your binary search tree class will be a collection class (like the list class in the previous project with .h and.cpp files). Your tree class will make use of node pointers/objects defined in a node class. The node class will consist of the product data and node pointers left and right. The tree class will have a root data attribute that will also be a node pointer When the program starts, the program should read from a textfile at least 16 products with their names, prices, and quantities being purchased (quantities initially should equal 0). As the data is read, it should be added into the binary search tree. Arrange the data so that the tree will be reasonably balanced. Use the name of the product as the sorting key for the binary search tree After the data has been loaded from the text file, a menu should appear that prompts the user to (1) add a product, (2) edit a product, (3) find and display a product, (4) view all products, (5) delete a product, (6) find and purchase a product, (7) display the current order, (8) clear the current order, or (9) quit the program When the program quits, the products in the current program, including those just added and having removed all those removed, are saved to the text file so that the updated collection of products will be loaded when the program runs the next time. The quantities should all be set to 0 when saving the text file Additional requirements (to practice traversals): Your tree of products should have a destructor that uses a post-order traversal strategy. The view all products menu option should use an in order traversal strategy. When displaying the current order, use a pre-order traversal strategy When clearing the current order use a level traversal strategyStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started