#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
struct Node{
string data;
int lineNum;
bool printed = false;
Node* left;
Node* right;
};
Node* GetNewNode(string data, int lineNum){
Node* newNode = new Node();
newNode->data=data;
newNode->lineNum=lineNum;
newNode->left = newNode->right = NULL;
return newNode;
}
Node* Insert(Node* rootPtr,string data,int lineNum){
if(rootPtr == NULL){
rootPtr = GetNewNode(data,lineNum);
for (int i =0; rootPtr->data[i]; i++){
while(ispunct(rootPtr->data[i]))
rootPtr->data.erase(i,1);
}
rootPtr->data = rootPtr->data.substr(0,10);
return rootPtr;
}
else if(data<= rootPtr->data){
rootPtr->left = Insert(rootPtr->left,data,lineNum);
for (int i =0; rootPtr->data[i]; i++){
while(ispunct(rootPtr->data[i]))
rootPtr->data.erase(i,1);
}
rootPtr->data = rootPtr->data.substr(0,10);
}
else {
rootPtr->right = Insert(rootPtr->right,data,lineNum);
for (int i =0; rootPtr->data[i]; i++){
while(ispunct(rootPtr->data[i]))
rootPtr->data.erase(i,1);
}
rootPtr->data = rootPtr->data.substr(0,10);
}
return rootPtr;
}
void inOrderPrint(Node *rootPtr ) {
//ofstream outputFile;
//outputFile.open("Output.txt");
if ( rootPtr != NULL ) {
inOrderPrint( rootPtr->left );
if (rootPtr->printed)
cout << rootPtr->lineNum;
else{
cout << (rootPtr->data)<<" " << rootPtr->lineNum <<endl;
rootPtr->printed = true;
}
//outputFile << (rootPtr->data)<<rootPtr->lineNum <<endl;
inOrderPrint( rootPtr->right );
}
//outputFile.close();
}
int main(){
char c;
int counter = 1;
string word;
ifstream inFile;
Node* rootPtr = NULL; // Pointer to the root node
inFile.open("example.txt");
if (!inFile)
cout << "Unable to open text file";
while (inFile >> word) {
c = inFile.get();
//cout<<c;
if (word == "#")
break;
else if (c=='\n'){
rootPtr = Insert(rootPtr,word,counter);
counter++;
}
else
rootPtr = Insert(rootPtr,word,counter);
}
inOrderPrint(rootPtr);
inFile.close();
}