Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The code is correct, just copy and paste and add code where says your code here. Thanks. I nput.txt in MikesLemon 86 in Smirnoff 66

The code is correct, just copy and paste and add code where says "your code here". Thanks.

Input.txt

in MikesLemon 86 in Smirnoff 66 in DosEquis 9 in Guiness 66 in Heineken 12 in Coors 45 print_tree out MikesLemon 31 print_tree out Guiness 32 print_tree in Miller 79 print_tree out Guiness 30 print_tree in Guiness 74 print_tree out DosEquis 10 print_tree

print size main.cpp

#include #include #include #include #include #include #include #include #include

#include using namespace std;

void loadFile(string fname, fstream& file) {file.open(fname.c_str()); if (file.fail()) {cout << "Cannot open file " << fname << endl; } }

string lowercase(string s) { for (unsigned int i = 0; i < s.length(); i++) { s[i] = std::tolower(s[i]); } return s;} struct Node { string key;int ht; int value;Node* left;Node* right;Node* parent; // constructors Node(string k, int v) : key(k), value(v), left(NULL), right(NULL), parent(NULL) { } Node(string k, int v, Node* l, Node* r, Node* p) : key(k), value(v), left(l), right(r), parent(p) { }}; class BSTMap {public: Node* root; private: void deleteAll(); Node* successor(Node* w) const;Node* removeNode(Node* w);int n; protected: void printAux(const Node* w) const; // print utility public: BSTMap() : root(NULL), n(0) { }; ~BSTMap(); Node** find(string k) const;Node* put(string k, int v);Node* erase(string k);Node* eraseNode(Node* w); int size() const; bool empty() const;void print() const; void printTree(Node* s, int space) const; };void BSTMap::printAux(const Node* w) const { if (w) { cout << "[" << w->key << ":" << w->value << "]"; cout << "("; printAux(w->left);cout << "),("; printAux(w->right);cout << ")";}} void BSTMap::print() const { printAux(root); cout << endl;} void BSTMap::printTree(Node* s, int space) const { int addSpace = 8; if (!s){ return; } space = space + addSpace; this->printTree(s->right, space);

cout << endl; for (int i = addSpace; i < space; i++) cout << " "; cout << s->key << ":" << s->value << endl; this->printTree(s->left, space); } void BSTMap::deleteAll() { Node* w = root; while (w) { if (!(w->left || w->right)) { Node* x = w; w = w->parent; if (w) { if (w->left == x) w->left = NULL; else w->right = NULL; } delete x; n--; continue; } w = (w->left) ? w->left : w->right; } } BSTMap::~BSTMap() { deleteAll(); } Node* BSTMap::removeNode(Node* w) { Node* z = w->parent; Node* x = (w->left) ? w->left : w->right; if (x) x->parent = z; if (z) { if (z->left == w) z->left = x; else z->right = x; } else root = x; delete w; n--; return z; } Node** BSTMap::find(string k) const { Node* w = root; Node* z = NULL; while (w && (w->key != k)) { z = w; w = (w->key > k) ? w->left : w->right; } Node** wAndZ = new Node * [2]; wAndZ[0] = w; wAndZ[1] = z; return wAndZ; } Node* BSTMap::put(string k, int v) { Node** wAndZ = find(k); Node* w = wAndZ[0]; Node* z = wAndZ[1]; delete wAndZ; if (w) { w->value = v; return w; } Node* x = new Node(k, v, NULL, NULL, z); if (z) { if (z->key > k) z->left = x; else z->right = x; } else root = x; n++; if (n == 1) root = x; return x; } Node* BSTMap::eraseNode(Node* w) { if (!w) return NULL; if (w->left && w->right) { Node* x = successor(w); w->key = x->key; w->value = x->value; w = x; } Node* z = removeNode(w); return z; }

Node* BSTMap::erase(string k) { Node** wAndZ = find(k); Node* w = wAndZ[0]; Node* z = wAndZ[1]; delete wAndZ; return ((w) ? eraseNode(w) : z); } Node* BSTMap::successor(Node* w) const { if (!w) return NULL; if (w->right) { w = w->right; while (w->left) w = w->left; } else { Node* z = w; w = w->parent; while (w->right == z) { z = w; w = w->parent; } } return w; }

// OUTPUT: size of the tree int BSTMap::size() const { return n; }

// OUTPUT: true if the tree is empty; false otherwise bool BSTMap::empty() const { return (!root); } class SplayTreeMap : public BSTMap { private: void zig(Node* x); void zigZig(Node* x); void zigZag(Node* x); void splay(Node* x); public: SplayTreeMap() { }; // default constructor Node* findSplay(string k); Node* putSplay(string k, int v); Node* eraseSplay(string k); }; // INPUT: a node x in the splay tree void SplayTreeMap::zig(Node* x) { // Your code here

} // INPUT: a node x in the splay tree void SplayTreeMap::zigZig(Node* x) { // Your code here }

// INPUT: a node x in the splay tree void SplayTreeMap::zigZag(Node* x) { // Your code here } // INPUT: a node x in the splay tree void SplayTreeMap::splay(Node* x) { // Your code here } Node* SplayTreeMap::findSplay(string k) { Node** wAndZ = this->find(k); Node* w = wAndZ[0]; // w is the node with key k, if it exists, or NULL otherwise Node* z = wAndZ[1]; // z is the parent of node w, or if w is NULL, the last node found while searching for key k delete wAndZ; Node* x = (w) ? w : z; this->splay(x); return x; } Node* SplayTreeMap::putSplay(string k, int v) { Node* x = this->put(k, v); this->splay(x); return x; }

Node* SplayTreeMap::eraseSplay(string k) { Node* x = this->erase(k); this->splay(x); return x; }

class SplayTreeInventory { private: SplayTreeMap* stmap;

public: int numProducts(); int available(string id); int size(); void into(string id, int amount); void out(string id, int amount); void printProduct(string id); void printTree(); void printAll(); void printAllHelper(Node* s); void printSize(); // constructor SplayTreeInventory() : stmap(new SplayTreeMap()) {}

};

// OUTPUT: number of different items in inventory int SplayTreeInventory::numProducts() { return this->stmap->size(); }

int SplayTreeInventory::available(string id) { // Your code here }

// INPUT: string key id, integer value void SplayTreeInventory::into(string id, int amount) { // Your code here }

// INPUT: string key id, integer value void SplayTreeInventory::out(string id, int amount) { // Your code here }

// PRECONDITION: // POSTCONDITION: // OUTPUT: total number of items in the inventory int SplayTreeInventory::size() { // Your code here }

// OUTPUT: string representation of an entry with a specified key id void SplayTreeInventory::printProduct(string id) { cout << id << ": " << this->available(id) << endl; }

// helper function to print all entries void SplayTreeInventory::printAllHelper(Node* s) { if (s == NULL) { return; } printAllHelper(s->left); cout << s->key << ": " << s->value << endl; printAllHelper(s->right); }

// OUTPUT: prints string representation of all entries in the inventory void SplayTreeInventory::printAll() { printAllHelper(this->stmap->root); }

// OUTPUT: prints string representation of the splay tree representing the inventory void SplayTreeInventory::printTree() { this->stmap->printTree(this->stmap->root, 0); }

// OUTPUT: prints number of entries in the inventory void SplayTreeInventory::printSize() { cout << this->size() << endl; }

int main() { string inputFilename = "input.txt"; string line;SplayTreeInventory S; // open input file fstream inputFile; loadFile(inputFilename, inputFile); while (getline(inputFile, line)) { cout << line << endl;stringstream lineSS(line); string token; string command = "";vector tokens; while (getline(lineSS, token, ' ')) {token.erase(token.find_last_not_of(" \t") + 1); tokens.push_back(token); }

if (tokens.size() > 0) { command = tokens[0]; // first token is the command}

if (tokens.size() > 1){if (command == "in") {if (tokens.size() > 2) {S.into(tokens[1], stoi(tokens[2])); } else{S.into(tokens[1], 1);}} if (command == "out") { if (tokens.size() > 2){ S.out(tokens[1], stoi(tokens[2]));} else{S.out(tokens[1], 1); } } if (command == "print_item") { S.printProduct(tokens[1]); }} if (command == "print"){ S.printAll(); } if (command == "size") {S.printSize();} if (command == "print_tree"){ S.printTree();}} inputFile.close();return EXIT_SUCCESS;}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Expert Oracle Database Architecture

Authors: Thomas Kyte, Darl Kuhn

3rd Edition

1430262990, 9781430262992

More Books

Students also viewed these Databases questions

Question

What percentage of loans have a 36-month term?

Answered: 1 week ago