hi
I'm new to c++,I really need your help :(
I have an assignment that it's title is a latin sort.It needs to read from the text file and then sort the words by bst and again write the sorted files to a text file,I also attach the assignment
I can read from the file and I think by using getline(in,this.s0,',') we can read delimetered words.but i dont know how to insert the words to the bst for iorder traversal.how to pass the words one by one for inserting in the bst.
and to write the words into a text file.this is my code.with lots of mistakes :),would you please help me with this?
thanks

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <assert.h>
#include <string.h>
#include "stdafx.h"
#include "cstdlib"
#include "stdio.h"
#include "conio.h"
#include "iostream"
#include "fstream" 
#include "stdlib.h"
#include "math.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <string>
#include <fstream>
#include <iostream>
#include <sstream>
#include<iostream>
#include <sstream>
#include <string>
#include <vector>
#include <fstream>
#include <string>
 #include <vector>    
#include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;

	//======================
	struct words
{
	string s0;
	string s1;
	string s2;
	string s3;
	string s4;
};
	//=======================
     
    class BSTree
    {
    private:
    struct treeNode
    {
    treeNode* left;
    treeNode* right;
    string word;
     
    treeNode(string s)
    {
    word = s;
    left = NULL;
    right = NULL;
    }
    };
     
    public:
    treeNode* root;
     
    BSTree()
    {
    root = NULL;
    }
     
    void insert(treeNode *&root, string newNode)
    {
     
    if(root == NULL)
    {
    root = new treeNode(newNode);
    return;
    }
    else if(newNode < root->word)
    {
    insert(root->left, newNode);
    }
    else
    {
    insert(root->right, newNode);
    }
    }
     
    void print(treeNode *node)
    {
    if(node != NULL)
    {
    print(node->left);
    cout << node->word << endl;
    print(node->right);
    }
    }
    };


     
    int main()	
    {
		while(1)
		{
    ifstream fis;
    BSTree bst;
	fis.open("E:\example.txt");
     
    while(!fis.eof())
    {
   //=================
vector<words> wordList;
words thisWord;


	while( getline(fis,thisWord.s0,':') )
  {
	getline(fis,thisWord.s1,',');
        getline(fis,thisWord.s2,',');
	getline(fis,thisWord.s3);
    //getline(in,pp.s4);
	wordList.push_back(thisWord);
  }
    
//    // now just display all the data in the vector
   vector<words>::iterator it = wordList.begin();
    for(; it != wordList.end(); it++)
    {
	string word;
	thisWord.word = word;
    
    wordList.push_back(thisWord);
   //cout << (*it).s0 << ":" << (*it).s1 << "," << (*it).s2 << "," << (*it).s3 /*<< (*it).s4 */<< "\n";
	//cout << (*it).s0 << ":" <<arr1 << "," << arr2 << "," << arr3 << "\n";
    }

}


    //fis >> str;
	//--------------------------------------
	while( getline(fis,thisWord.s0,'\n') )
   {
	   getline(fis,thisWord.s1,':');
	getline(fis,thisWord.s1,',');
	getline(fis,thisWord.s2,',');
	getline(fis,thisWord.s3);
    //getline(fis,thisWord.s4);
	wordList.push_back(thisWord);
	string word1, word2,word3 ;
	//thisWord.word = word;
	word1=thisWord.s1;
	word2=thisWord.s2;
	word3=thisWord.s3;
	cout << word1<<"---"<<word2<<"---"<<"\n";

  }

	string word;
	bst.insert(bst.root,word);
	}  
    bst.print(bst.root);
	
		}
    }

Recommended Answers

All 8 Replies

I'm not sure what you're doing with your words struct or why you need a vector<> of them. Or why your only reference to the bst, once you declare it, is to insert an empty (by default) string into it, and then print it back out.

What exactly is the format of the lines in your input? Which word (or words, or whetever string) from each line needs to be sorted? What should the output look like? If you can explain that clearly, you might just find that you now know how to write the code to do it! But if not, we're still here to help.

thanks
I attached the format also
the input file is like this for example for one unit
I think we have to read the words from the file to ':' and in front of each word in the meanings

a. Unit names are preceded by a percentage symbol.
b. There is only one entry per line.
c. A Latin word is separated by a colon from its English

To output English words in alphabetical order, create a binary search tree for each unit containing English words and linked lists of Latin equivalents. Make sure that there is only one node for each English word in the tree. For example, there is only one node for and, although and is used twice in unit 6: with words ac and atque. After the task has been completed for a given unit (that is, the content of the tree has been stored in an output file), delete the tree along with all linked lists from computer memory before creating a tree for next unit.

%Unit 5
ante : before, in front of, previously
antiques : ancient
ardeo : burn, be on fire, desire
arma : arms, weapons
aurum : gold
aureus : golden, of gold

thanks
I attached the format also
the input file is like this for example for one unit
I think we have to read the words from the file to ':' and in front of each word in the meanings

a. Unit names are preceded by a percentage symbol.
b. There is only one entry per line.
c. A Latin word is separated by a colon from its English

To output English words in alphabetical order, create a binary search tree for each unit containing English words and linked lists of Latin equivalents. Make sure that there is only one node for each English word in the tree. For example, there is only one node for and, although and is used twice in unit 6: with words ac and atque. After the task has been completed for a given unit (that is, the content of the tree has been stored in an output file), delete the tree along with all linked lists from computer memory before creating a tree for next unit.

%Unit 5
ante : before, in front of, previously
antiques : ancient
ardeo : burn, be on fire, desire
arma : arms, weapons
aurum : gold
aureus : golden, of gold

and the out put should be like this

%Unit5
ancient : antiques
arms : arma
be on fire : ardeo
before : ante
burn : ardeo
desire : ardeo
gols : aurum
golden : aureus
in front of : ante
of gold : aureus
previously : ante
weapons : arma

OK, let's scrap that last, the attached assignment document provides all that. The question is actually: what should you store in your binary tree?

Restating the problem: for each -English- word (or phrase) found on an input line, insert the corresponding -Latin- word (from the front of the line) into a BST, sorted by English word. If the English word is already in the tree, the Latin word should be appended to a linked list of Latin equivalents.

So you need to revise (a) the definition of a tree_node, to include not only an English-word string, but also a pointer to a linked-list of Latin-word strings, and (b) the insert() method so that it can take a single English word (or phrase) and a single Latin word, and perform the necessary operations. Note that you now also should define a list_node type and a LinkedList class, so that the BST can make use of them.

Once you have that much done, you can implement the part about clearing out the tree and its linked-lists at the end of each Unit.

Thanks shinnoon, we were typing past each other at the same time, sorry for the confusion.

Thanks
So i need two classes one for the bst and one for link list
for example for the first line
read:"ante" put it in the bst
read the line :before, in front of, previously store them in a linked list?

No, I think you have it backwards. While that would be easy, it doesn't get you any closer to generating the correct output. What you want in the linked-list is the latin words, so that you can print out the results by traversing the BST in-order to get the English words in alphabetical order, and then print out the corresponding Latin words for each English word.

Keeping it very specific, given the first line for Unit 5:

ante : before, in front of, previously

you will need to insert "before" and "in front of" and "previously" into the BST, each with the corresponding Latin word "ante". Each bst_node needs:

string englishWord;
LinkedList latinWords;

Does that help?

I will try and let you know,thank you very much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.