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;
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);
}
for(int i = 0; i < bstData.getLength(); i++)
{
cout << "bstData data at " << i << ": " << bstData.at(i) << endl;
cout << "bstData length is: " << bstData.getLength() << endl;
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;
}
of.close();
}
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. , ?