Thread: How to insert a class object to a binary search tree

  1. #1
    Registered User
    Join Date
    Sep 2020
    Posts
    11

    Question How to insert a class object to a binary search tree

    Hello. I am trying to insert a new item to a binary search tree but I can't get my code to work right. Here I am suppose to ask the user for the new class object's data and then store it in the binary search tree. No matter what I try to inert it outputs 'Can't have duplicate code', even if I know it is not there. This is my first time working with binary search trees and my class is accelerated so I don't have the chance to really take my time and learn it on my own like I usually do. I'm just confused on where to go from here and how I can fix my code.

    This is what I have in main. This is the code I am having trouble with:
    Code:
    int main()
    {
        BinarySearchTree<Stock> tree;
        Stock company[23];                //a class to hold data about stocks
        string name,
            symbol;
        double price = 0;
    //this is a menu driven program, i only the part of the code I am having trouble with
            else if (choice == INSERT)
            {
                cout << "Enter the new company's name: ";
                cin.ignore();
                getline(cin, name);
                cout << "Enter the new company's symbol: ";
                getline(cin, symbol);
                cout << "Enter the new company's price: ";
                cin >> price;
    
    
                for (int i = 0; i < 23;)
                {
                                    //search for the next empty spot in the class array of objects
    
                    while (company[i].getPrice() != 0)
                    {
                        i++;
                    }
                    if (company[i].getPrice() == 0)
                    {
                        company[i].setName(name);        //the setters are in the class Stock
                        company[i].setSymbol(symbol);
                        company[i].setPrice(price);
                        tree.insert(company[i]);        //outputs 'can't have duplicate code' even if it is not in tree already
                    }
                }
            }

    Here is what the code looks like to insert an item to the binary search tree. I know there is nothing wrong with this code because my teacher helped the class write it. This code is just here for clarification:
    Code:
    template <typename T>
    void BinarySearchTree<T>::insert(const T& item)
    {
        insert(root, item);
    }
    
    
    template <typename T>
    void BinarySearchTree<T>::insert(Node<T> *&r, const T& item)
    {
        using namespace std;
        if (r == nullptr)
        {
            r = new Node<T>;
            r->info = item;
            r->left = nullptr;
            r->right = nullptr;
        }
    
    
        else if (item < r->info)
            insert(r->left, item);
    
    
        else if (item > r->info)
            insert(r->right, item);
    
    
        else
            cout << "Can't have duplicate code.\n";
    }
    Last edited by meagangramlin; 11-09-2020 at 05:29 PM.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,047
    Instead of this:
    Code:
                for (int i = 0; i < 23;)
                {
                    while (company[i].getPrice() != 0)
                    {
                        i++;
                    }
                    if (company[i].getPrice() == 0)
                    {
                        company[i].setName(name);
                        company[i].setSymbol(symbol);
                        company[i].setPrice(price);
                        tree.insert(company[i]);
                    }
                }
    Try this:
    Code:
                int i = 0;
                while (i < 23 && company[i].getPrice() != 0)
                {
                    i++;
                }
                if (i < 23)
                {
                    company[i].setName(name);
                    company[i].setSymbol(symbol);
                    company[i].setPrice(price);
                    tree.insert(company[i]);
                }
    I don't understand why you have an array as well as the tree. Are you sure you're supposed to do that? It would be more sensible to get rid of the array and do it like this:
    Code:
                Stock company;
                company.setName(name);
                company.setSymbol(symbol);
                company.setPrice(price);
                tree.insert(company);
    Philosophy is a battle against the bewitchment of our intelligence by means of language. - Wittgenstein

  3. #3
    Registered User
    Join Date
    Sep 2020
    Posts
    11
    Thank you so much I was able to insert correctly!! I guess I was just a bit off.

    I am reading from a file the contains info on a stock's name, symbol, and price. For example, the first 6 lines of data from the file would be:
    Motorola Inc.
    MOT 17.48
    Microsoft Corp.
    MSFT
    28.11
    In the main function a Stock object is created for each group of 3 lines in the data file. Then I insert each Stock object into the binary search tree. I though an array would be a good fit for this. Is there a better way to do this?

    Here is the code I used to fill the Stock array with the data from the file:
    Code:
    int main()
    {
            ifstream file;
        string line;
        double dbline;
    file.open("data.txt");
        if (!file)
            cout << "Can not open file.\n";
    
    
        
        while (!file.eof())
        {
            for (int i = 0; i < 20; i++)
            {
                getline(file, line, '\n');
                company[i].setName(line);
                getline(file, line, '\n');
                company[i].setSymbol(line);
                file >> dbline;
                company[i].setPrice(dbline);
                file.get();
            }
        }
        
        file.close();
    }
    I'm able to read the file just fine. But is there a more efficient way to create a new Stock class object for every 3 lines of data from the file?
    Last edited by meagangramlin; 11-10-2020 at 06:06 PM.

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,047
    Maybe something like this:
    Code:
    int main()
    {
        ifstream file("data.txt");
        if (!file)
        {
            cerr << "Cannot open input file.\n";
            return 1;
        }
     
        BinarySearchTree<Stock> tree;
     
        while (getline(file, line))
        {
            Stock company;
            company.setName(line);
            getline(file, line);
            company.setSymbol(line);
            double price;
            file >> price;
            company.setPrice(price);
            file.ignore(1000, '\n');
     
            tree.insert(company);
        }
     
        tree.print(); // or whatever
    }
    I'm assuming that the tree.insert() will actually copy the company data.
    Philosophy is a battle against the bewitchment of our intelligence by means of language. - Wittgenstein

  5. #5
    Registered User
    Join Date
    Sep 2020
    Posts
    11
    I did try this but it wouldn't work because the Stock object becomes a local variable in the while loop and I can't access it in the rest of the program.

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    1,047
    Quote Originally Posted by meagangramlin View Post
    I did try this but it wouldn't work because the Stock object becomes a local variable in the while loop and I can't access it in the rest of the program.
    That's why I said that I assumed tree.insert() made a copy of the data.
    Philosophy is a battle against the bewitchment of our intelligence by means of language. - Wittgenstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to search a binary search tree using a class object
    By meagangramlin in forum C++ Programming
    Replies: 2
    Last Post: 11-09-2020, 10:18 PM
  2. Replies: 1
    Last Post: 04-16-2011, 04:44 AM
  3. Binary Search Tree Insert
    By bleuz in forum C++ Programming
    Replies: 5
    Last Post: 04-30-2010, 09:53 AM
  4. Help with insert/delete binary search tree
    By Nazgulled in forum C Programming
    Replies: 39
    Last Post: 03-25-2009, 04:24 PM

Tags for this Thread