Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am reading Lastname from file and I want to print Lastname sorted with how many time lastname appears and in which line.

e.G.
OUTPUT LOOKS LIKE BELOW
Dave...............4: 2, 7, 10, 15 (Dave LAST NAME APPEARS IN FILE 4 TIMES AND IN LINE2,7,10 AND 15)
patel..............2: 1,3 (pATWL LAST NAME APPEARS 2 TIMES . IN FIRST LINE AND IN 3RD LINE)


My main class dirName has following header files

I am using Binary Search Tree to store my last name
My Binary Search Tree has following Binary Node wher lastName stores as name.

C++
private:
    struct BinaryNode
    {
        Comparable name;
        BinaryNode *left;
        BinaryNode *right;

        BinaryNode( const Comparable & theName, BinaryNode *lt, BinaryNode 
*rt )
            : element( theName ), left( lt ), right( rt ) { }
    };

    BinaryNode *root;




class dirName{

public: 
	dirName();
	dirName(string inName, int lineNumber);
	void CountName(int lineNumber);
	int NameCount ();
	string GetName();
	bool operator< (dirName &RHS);
	dirName operator= (dirName &RHS);
	bool operator==(dirName &RHS);
	friend ostream &operator << (ostream &, dirName &);

private:
	int m_count;
	queue<int> m_lineNumbers;
	string nameText;
};

ostream  & operator << (ostream &strm, dirName &lstName)
	{
		int i;
		cout << lstName.nameText;
		for (i = lstName.m_lineNumbers.size(); i < 20;  i++)
		{
			cout << "." ;
		}
		
		cout << lstName.WordCount() <<": " ;

		for (unsigned int i = 0; i < lstName.m_lineNumbers.size(); i++)
		{
			int line;
			line = lstName.m_lineNumbers.front();
			lstName.m_lineNumbers.pop();
			cout << line;
		}
	}



I want to use << (for output) operator load function of dirName class to print
my BinarySearchTree in sorted order.

How can I use my following defined << overload operator function in following BST function

C++
void printTree( BinaryNode *t, ostream & out ) const
    {
        if( t != NULL )
        {
            printTree( t->left, out );
            out << t->name << endl;
            printTree( t->right, out );
        }
    }



and how can I use < overload operator of dirName in following BST function
C++
<pre lang="c++">void insert( const Comparable & x, BinaryNode * & t )
    {
        if( t == NULL )
            t = new BinaryNode( x, NULL, NULL );
        else if( x < t->name )
            insert( x, t->left );
        else if( t->name < x )
            insert( x, t->right );
        else
            ;  // Duplicate; do nothing
    }</int>
Posted
Updated 19-Oct-14 17:12pm
v2
Comments
Richard MacCutchan 20-Oct-14 4:11am    
You must code the implementation of operator<.
nv3 20-Oct-14 5:18am    
You have shown only a fraction of your binary tree class (the private part). And it appears you wanted to derive your class dirName from Comparable, but didn't do it. So it appears to me that your problem is not in writing the operator overload functions, but in finding the way how your (probably predefined) binary tree class should cooperate with your user-written class dirName. Is that correct?

1 solution

1. you should use the strm operand rather than cout in your implementation of operator<<. Also, at the end you should return strm:
C++
ostream  & operator << (ostream &strm, dirName &lstName)
{
   int i;
   strm << lstName.nameText;
...
   return strm;
}


2. You must supply the operator<</code> with the following signature:
C++
bool operator<(const Comparable& lhs, const Comparable& rhs);

How you implement it of course depends on the way you define ordering for the class Comparable, e. g.:
C++
class Comparable {
   string first_name;
   string last_name;
   string title;
public:
   string make_key() const { return last_name + first_name + title; }
};
bool operator<(const Comparable& lhs, const Comparable& rhs) {
   return lhs.make_key() < rhs.make_key();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900