Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ You will create a binary search tree for a product catalogue. The catalogue

ID: 3593032 • Letter: C

Question

C++

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.

Explanation / Answer

// c++ representation of finding shortest

// distance between node i and j

#include <bits/stdc++.h>

using namespace std;

// prints the path between node i and node j

void ShortestPath(int i, int j, int k, int m,

                                       int n)

{

    // path1 stores path of node i to lca and

    // path2 stores path of node j to lca

    vector<int> path1, path2;

    int x = m - 1;

    // push node i in path1

    path1.push_back(i);

    // keep pushing parent of node labelled

    // as i to path1 until lca is reached

    while (x != k) {

        path1.push_back(i / 2);

        i = i / 2;

        x--;

    }

    int y = n - 1;

    // push node j to path2

    path2.push_back(j);

    // keep pushing parent of node j till

    // lca is reached

    while (y != k) {

        path2.push_back(j / 2);

        j = j / 2;

        y--;

    }

    // printing path from node i to lca

    for (int l = 0; l < path1.size(); l++)

        cout << path1[l] << " ";

    // printing path from lca to node j

    for (int l = path2.size() - 2; l >= 0; l--)

        cout << path2[l] << " ";

    cout << endl;

}

// returns the shortest distance between

// nodes labelled as i and j

int ShortestDistance(int i, int j)

{

    // vector to store binary form of i and j

    vector<int> v1, v2;

    // finding binary form of i and j

    int p1 = i;

    int p2 = j;

    while (i != 0) {

        v1.push_back(i % 2);

        i = i / 2;

    }

    while (j != 0) {

        v2.push_back(j % 2);

        j = j / 2;

    }

    // as binary form will be in reverse order

    // reverse the vectors

    reverse(v1.begin(), v1.end());

    reverse(v2.begin(), v2.end());

    // finding the k that is lca (i, j)

    int m = v1.size(), n = v2.size(), k = 0;

    if (m < n) {

        while (k < m && v1[k] == v2[k])

            k++;

    } else {

        while (k < n && v1[k] == v2[k])

            k++;

    }

    ShortestPath(p1, p2, k - 1, m, n);

    return m + n - 2 * k;

}

// c++ representation of finding shortest

// distance between node i and j

#include <bits/stdc++.h>

using namespace std;

// prints the path between node i and node j

void ShortestPath(int i, int j, int k, int m,

                                       int n)

{

    // path1 stores path of node i to lca and

    // path2 stores path of node j to lca

    vector<int> path1, path2;

    int x = m - 1;

    // push node i in path1

    path1.push_back(i);

    // keep pushing parent of node labelled

    // as i to path1 until lca is reached

    while (x != k) {

        path1.push_back(i / 2);

        i = i / 2;

        x--;

    }

    int y = n - 1;

    // push node j to path2

    path2.push_back(j);

    // keep pushing parent of node j till

    // lca is reached

    while (y != k) {

        path2.push_back(j / 2);

        j = j / 2;

        y--;

    }

    // printing path from node i to lca

    for (int l = 0; l < path1.size(); l++)

        cout << path1[l] << " ";

    // printing path from lca to node j

    for (int l = path2.size() - 2; l >= 0; l--)

        cout << path2[l] << " ";

    cout << endl;

}

// returns the shortest distance between

// nodes labelled as i and j

int ShortestDistance(int i, int j)

{

    // vector to store binary form of i and j

    vector<int> v1, v2;

    // finding binary form of i and j

    int p1 = i;

    int p2 = j;

    while (i != 0) {

        v1.push_back(i % 2);

        i = i / 2;

    }

    while (j != 0) {

        v2.push_back(j % 2);

        j = j / 2;

    }

    // as binary form will be in reverse order

    // reverse the vectors

    reverse(v1.begin(), v1.end());

    reverse(v2.begin(), v2.end());

    // finding the k that is lca (i, j)

    int m = v1.size(), n = v2.size(), k = 0;

    if (m < n) {

        while (k < m && v1[k] == v2[k])

            k++;

    } else {

        while (k < n && v1[k] == v2[k])

            k++;

    }

    ShortestPath(p1, p2, k - 1, m, n);

    return m + n - 2 * k;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote