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.
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
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
<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
; }</int>