Load passed data from a binary search tree into a vector through the header file function

I currently have a Vector class that has a template class to store some stock objects. For example. Vector<Stock> vecA;

In my purpose, it is necessary to use a binary search tree, execute inorderTraversal()on it so that it is sorted, and then execute some processes with it inMain()

In an attempt to "hide" the trace process from the user and save the sorted data after passing through the binary search tree, I change the part cout << p->infoto output the data transferred to the output file.

It means:

    if (p != NULL)
    {
        inorder(p->lLink);
        cout << (p->info) << endl; //changed to vecA.Push_back(p->info);
        inorder(p->rLink);
    }

However, this does not push elements from nodes into my vector in the way I would like. It works technically, I can Print()output all rows of data neatly one after another, but when I do Vector.getLength(), it shows that there is only one row.

The problem here is that when Vector has only one line (but strangely contains all the elements that I have and displays line by line), I cannot work with this Vector, since most processes are loops.

Please inform me, I suspect that there is a problem with my method inorder()or something else. Perhaps this is how BST outputs data, etc. I am very new to BST, and I don’t take much time to complete this task.

Here is my code for my function inorder()

    template <class elemType>
    void binaryTreeType<elemType>::inorder(nodeType<elemType> *p) const
    {
    Vector<Stock> bstData;
    ofstream of("output.csv"); 
    of << fixed << showpoint << setprecision(2); 

    if (p != NULL)
    {
        inorder(p->lLink);
        bstData.Push_back(p->info);
        inorder(p->rLink);
    }

    //Below is a for-loop that I was planning to use to get the traversed data 
    //from the Vector into an output file so I can access the traversed data 
    //through reading an output file from my Main() function

    for(int i = 0; i < bstData.getLength(); i++)
    {
        cout << "bstData data at " << i << ": " << bstData.at(i) << endl;
        //above statement is to check if i did an increment
        cout << "bstData length is: " << bstData.getLength() << endl;
        //above statement is to check my vector length
        //the following statement is to output data from vector into a .csv file
        of << bstData.at(i).d1.getDay() << "/" << bstData.at(i).d1.getMonth() << "/" << bstData.at(i).d1.getYear() << "," << setw(2) << setfill('0') << bstData.at(i).t1.getHour() << ":" << setw(2) << setfill('0') << bstData.at(i).t1.getMin() << ":" << setw(2) << setfill('0') << bstData.at(i).t1.getSec() << "," << bstData.at(i).getPrice() << "," << bstData.at(i).getVolume() << "," << bstData.at(i).getValue() << endl;
        cout << "i is now at: " << i << endl; //check i again
    }
    of.close();
    } //close inorder()

The following is the output when I run my program:

P.S: , , , !!

http://i132.photobucket.com/albums/q28/LoveSHE911/Screen%20Shot%202015-11-17%20at%205.05.48%20am_zpstldxxags.png

, bstData.Print() .

http://i132.photobucket.com/albums/q28/LoveSHE911/Screen%20Shot%202015-11-17%20at%205.07.27%20am_zpsrqqo8kzb.png

, , !

: @Mykola, , , .

inorderTraversal() Main() ifstream inFile("output.csv") while (inFile >> dd >> c >> mm >> c >> yy >> ...) , push_back . .

    ifstream inputfile("output.csv"); //open user chosen data file
    //load traversed data from output file output.csv into vAll 
    while (inputfile >> dd >> c >> mm >> c >> yy >> hh >> c >> mn >> c >> ss >> ch1 >> ch2 >> pr >> vl >> tp) 
    {   //check if there remaining data in input file
        Stock stk2(dd, mm, yy, hh, mn, ss, ch1, ch2, pr, vl, tp); 
        //if there still remaining data, create new stock object
        vAll.Push_back(stk2); //Insert stock object into vector 
    }
        cout << vAll.getLength() << endl; //check vector length

vAll.getLength() 0. , ?

+4
1

, node.

void binaryTreeType<elemType>::inorder(nodeType<elemType> *p, Vector<Stock>& storage) const

template <class elemType>
void binaryTreeType<elemType>::inorder(nodeType<elemType> *p) const
{
    Vector<Stock> bstData;
    ofstream of("output.csv"); 
    of << fixed << showpoint << setprecision(2); 

    inorder(p, bstData); // fill bstData recursively

    for(int i = 0; i < bstData.getLength(); i++)
    {
        cout << "bstData data at " << i << ": " << bstData.at(i) << endl;
        //above statement is to check if i did an increment
        cout << "bstData length is: " << bstData.getLength() << endl;
        //above statement is to check my vector length
        //the following statement is to output data from vector into a .csv file
        of << bstData.at(i).d1.getDay() << "/" << bstData.at(i).d1.getMonth() << "/" << bstData.at(i).d1.getYear() << "," << setw(2) << setfill('0') << bstData.at(i).t1.getHour() << ":" << setw(2) << setfill('0') << bstData.at(i).t1.getMin() << ":" << setw(2) << setfill('0') << bstData.at(i).t1.getSec() << "," << bstData.at(i).getPrice() << "," << bstData.at(i).getVolume() << "," << bstData.at(i).getValue() << endl;
        cout << "i is now at: " << i << endl; //check i again
    }
    of.close();
} //close inorder()

template <class elemType>
void binaryTreeType<elemType>::inorder(nodeType<elemType> *p, Vector<Stock>& storage) const
{
    if (p != NULL)
    {
        inorder(p->lLink, storage); // Fill storage with left values 
        storage.Push_back(p->info); // Add current value to storage (actualy bstData).
        inorder(p->rLink, storage); // Fill storage with right values
    }
} //close inorder()

,

while (inputFile.good()) // if stream is good
{   
    inputfile >> dd >> c >> mm >> c >> yy >> hh >> c >> mn >> c >> ss >> ch1 >> ch2 >> pr >> vl >> tp;
    //check if there remaining data in input file
    Stock stk2(dd, mm, yy, hh, mn, ss, ch1, ch2, pr, vl, tp); 
    //if there still remaining data, create new stock object
    vAll.Push_back(stk2); //Insert stock object into vector 
}
cout << vAll.getLength() << endl; //check vector length

,

inputfile >> dd >> c >> mm >> c >> yy >> hh >> c >> mn >> c >> ss >> ch1 >> ch2 >> pr >> vl >> tp;

istream , , , - fscanf, .

+2

Source: https://habr.com/ru/post/1616118/


All Articles