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

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

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

    Hello. I am trying to search for an element in a binary search tree using a class object but I cant get my code to work right. I have a class called Stock which has three private members: compName, compSymbol, compPrice. Here I am supposed to ask the user for a Stock symbol, search for that stock in the tree, and then display the name of that stock. Every time I try to search for a Stock symbol it just outputs 'Not found' even if I know it is in there. I don't understand why I can't get it to work.

    Here is what I have in main. This is the code I am having promblems with:
    Code:
    int main()
    {
        BinarySearchTree<Stock> tree;
        Stock company[23];
        string name,
            symbol;
        const int DISPLAY_NAME = 1,        
            DISPLAY_PRICE = 2,
            INSERT = 3,
            DISPLAY_ALL = 4,
            QUIT = 5;
    //this is a menu driven program, i only posted the part of the program i am having issues with
        if (choice == DISPLAY_PRICE)
            {
                 //Stock tempSymbol;
                cout << "Enter the stock's symbol to display the price: ";
                cin >> symbol;
    
    
                //symbol = tempSymbol.getSymbol();
    
    
                //tempSymbol.setSymbol(symbol);
                Stock* ptr = tree.search(symbol);
    
    
                if (ptr == nullptr)
                    cout << "Not found.\n";
                else
                    cout << ptr->getPrice();
    
    
                cout << endl;
            }
    }
    I tried using Stock's member functions get and set (accessor and mutator member functions) for both the symbol and the price but I still can't get it to work. I left a little bit of what I tried in the code above and commented it out.

    Here are some of the functions in the Stock class. The rest of the code here is just for clarification :
    Code:
    class Stock
    {
    private:
        string compName;
        string compSymbol;
        double compPrice;
    public:
        string getName() const;
        void setName(string name);
        string getSymbol() const;
        void setSymbol(string symbol);
        double getPrice() const;
        void setPrice(double price);

    And then the code in the BinarySearchTree is this:
    Code:
    template <typename T>
    T* BinarySearchTree<T>::search(const T& item) const
    {
        T* result;
        result = search(root, item);
    
    
        return result;
    }
    
    
    template <typename T>
    T* BinarySearchTree<T>::search(Node<T>*r, const T& item) const
    {
        T* result;
    
    
        if (r == nullptr)
            result = nullptr;
    
    
        else if (r->info > item)
            result = search(r->left, item);
    
    
        else if (r->info < item)
            result = search(r->right, item);
    
    
        else
            result = new T(r->info);
    
    
        return result;
    }
    I know there is nothing wrong with my search code because my teacher helped the class with it. I just put it here for clarification.
    Last edited by meagangramlin; 11-09-2020 at 05:12 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The code in the main function that you provided is syntactically incorrect, so I suggest that you relook it as there might be a serious copy/paste error. It may well be that you're mistaken about the data being in the binary search tree, or that you constructed the binary search tree incorrectly.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Tree-search method help?
    By shocklightning in forum C++ Programming
    Replies: 5
    Last Post: 03-25-2012, 10:57 PM
  2. Replies: 8
    Last Post: 08-11-2008, 04:19 AM
  3. Binary Search Tree - object instantiation problem
    By patricio2626 in forum C++ Programming
    Replies: 3
    Last Post: 11-14-2006, 02:11 AM
  4. A Binary Search Tree of... Binary Search Trees...
    By SlyMaelstrom in forum C++ Programming
    Replies: 5
    Last Post: 12-10-2005, 02:12 PM
  5. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM

Tags for this Thread