0

How would i use the find function in splay tree for the following code?

I want to be able to see if that node is in the tree, and if it is not, then i would want to be able to return false.

here's the link to the splay tree files.

SplayTree.cpp

SplayTree.h

#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;
#include "mystring.h"
#include "string.cpp"
#include "SplayTree.h"
#include "QueueAr.h"

class GrabStuff
{
        public:
                GrabStuff();
                GrabStuff(string);
                ~GrabStuff();
                void setWord (string);
                string getWord ();
                void setLineNumber (int);
                bool operator!= (const GrabStuff&) const;
                bool operator== (const GrabStuff&) const;
                bool operator< (const GrabStuff&) const;
                bool operator> (const GrabStuff&) const;
                friend ostream& operator<< (ostream &, const GrabStuff&);
        private:
                string myWord;
                Queue<int> *myLineNumbers;
                Queue<int> *temp;
};

GrabStuff::GrabStuff()
{}

GrabStuff::GrabStuff(string s)
{
        myWord = s;
        myLineNumbers = new Queue<int>();
        temp = new Queue<int>();
}

GrabStuff::~GrabStuff()
{}

void GrabStuff::setWord (string w)
{
        myWord = w;
}

string GrabStuff::getWord ()
{
        return myWord;
}

void GrabStuff::setLineNumber (int num)
{
        myLineNumbers->enqueue(num);
}

bool GrabStuff::operator!= (const GrabStuff& rhs) const
{
        return(myWord != rhs.myWord);
}

bool GrabStuff::operator== (const GrabStuff& rhs) const
{
        return (myWord == rhs.myWord);
}

bool GrabStuff::operator< (const GrabStuff& rhs) const
{
        return (myWord < rhs.myWord);
}

bool GrabStuff::operator> (const GrabStuff& rhs) const
{
        return (myWord > rhs.myWord);
}

ostream& operator<< (ostream &os, const GrabStuff &name)
{
        os << name.myWord << " ";
        while (!name.myLineNumbers->isEmpty())
        {
                int num = name.myLineNumbers->dequeue();
                os << num << " ";
                name.temp->enqueue(num);
                //code to print out numbers inside the queue
        }
        while (!name.temp->isEmpty())
        {
                name.myLineNumbers->enqueue(name.temp->dequeue());
        }
        return os;
}

int main(int argc, char *argv[])
{
        ifstream myfile (argv[1]);
        char command;                   // storage for command
        string line, word;              // storage for each line
        string junk = " ";
        int lineNumber = 0;             // storage for line number
        char * temp;
        GrabStuff bob(junk);
        SplayTree <GrabStuff> mytree(bob);

        if (myfile.is_open())           // If the file is open
        {
                while (! myfile.eof() )
                {
                        getline(myfile, line);
                        lineNumber++;
                        temp = new char [line.length()+1];      // set up 'temp' to be big enough to contain 'line'
                        strcpy (temp, line.c_str());            // 'temp' now contains a copy of 'line'
                        word = strtok (temp, " !\"#$%&'()*+,-./0123456789:;<=>?@[\\]^_`{|}~¿«»¡©®¹²³¼½¾");
                        while (word != NULL)
                        {
                                GrabStuff joe(word);
                                if (mytree->find(joe) == " ")
                                {
                                        mytree.insert(joe);
                                        joe.setLineNumber(lineNumber);
                                }
                                cout << word << " " << lineNumber << endl;
                                word = strtok(NULL," !\"#$%&'()*+,-./0123456789:;<=>?@[\\]^_`{|}~¿«»¡©®¹²³¼½¾");
                        }
                }
                cout << endl;
                myfile.close();         // Closes the file
        }

        do
        {
                cout << "Please enter a command (a, s, or q)>> ";       // Prompts the user to enter a command
                cin >> command;

                if (command == 'a')
                {
                        mytree.printTree();
                }
                if (command == 's')
                {

                }
                if (command == 'q')
                {
                        cout << endl << "Goodbye";
                }
                cout << endl;

        } while (command != 'q');

        return 0;
}
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Tom Gunn
1

That class has a kind of strange way of implementing a failed search. In the constructor you pass a bogus object that will act as the sentinel value, and find() returns that object if it fails. So in your case you will compare mytree->find(joe) against bob , since you passed bob as the constructor argument.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.