通过头文件功能将遍历的数据从二进制搜索树加载到Vector中

时间:2015-11-16 21:10:21

标签: c++ templates vector binary-tree binary-search-tree

我目前有一个Vector类,它是模板类,用于存储一些库存对象。例如。 Vector<Stock> vecA;

在我的作业中,要求使用二进制搜索树,对其执行inorderTraversal()以便对其进行排序,然后在Main()

中对其进行一些处理

试图从用户“隐藏”遍历过程,并在遍历二叉搜索树后存储已排序的数据,我正在更改cout << p->info部分以将遍历的数据输出到输出文件中。

这意味着:

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

然而,它并没有按照我希望的方式将项目从节点推送到我的向量中。它在技术上有效,我可以一个接一个地Print()整齐排列所有数据行,但当我执行Vector.getLength()时,它显示只有1行。

这里的问题是,当Vector只有1行(但奇怪地包含我拥有的所有项目并逐行显示)时,我无法使用此Vector,因为大多数进程都涉及for循环。

请告知,我怀疑我的inorder()方法有问题。也许这是BST输出数据等的方式。我对BST很新,我没有太多时间完成这项任务。

这是我的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's 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()

以下是我运行程序时的输出:

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's remaining data in input file
        Stock stk2(dd, mm, yy, hh, mn, ss, ch1, ch2, pr, vl, tp); 
        //if there's 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.是否有一个明显的错误我没有看到?

1 个答案:

答案 0 :(得分:2)

您必须重建函数以使用节点指针传递存储目标。这意味着

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's 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's remaining data in input file
    Stock stk2(dd, mm, yy, hh, mn, ss, ch1, ch2, pr, vl, tp); 
    //if there's 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支持此功能。