通过头文件函数将二叉搜索树中遍历的数据加载到Vector中
Load traversed data from Binary Search Tree into Vector through header file function
我目前有一个模板 class 的 Vector class,用于存储一些股票对象。例如。 Vector<Stock> vecA
;
在我的作业中,需要使用二叉搜索树,对其执行 inorderTraversal()
以便对其进行排序,然后在 Main()
[=30] 中对其进行一些处理=]
为了"hide"用户的遍历过程,为了存储遍历二叉搜索树后的排序数据,我将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: 我不能post 图片请参考这个link 图片!!
下面是我运行bstData.Print()
检查向量内容时的输出。
请各位指教和帮助,我迷路了!
编辑:感谢@Mykola,我已经解决了上述问题,但出现了一个密切相关的问题。
在我的 main()
中执行 inorderTraversal()
之后,我想执行一个 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() returns 0。是否有我没有看到的明显错误?
您必须重建您的函数以传递带有节点指针的存储目标。意思是
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
。
我目前有一个模板 class 的 Vector class,用于存储一些股票对象。例如。 Vector<Stock> vecA
;
在我的作业中,需要使用二叉搜索树,对其执行 inorderTraversal()
以便对其进行排序,然后在 Main()
[=30] 中对其进行一些处理=]
为了"hide"用户的遍历过程,为了存储遍历二叉搜索树后的排序数据,我将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: 我不能post 图片请参考这个link 图片!!
下面是我运行bstData.Print()
检查向量内容时的输出。
请各位指教和帮助,我迷路了!
编辑:感谢@Mykola,我已经解决了上述问题,但出现了一个密切相关的问题。
在我的 main()
中执行 inorderTraversal()
之后,我想执行一个 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() returns 0。是否有我没有看到的明显错误?
您必须重建您的函数以传递带有节点指针的存储目标。意思是
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
。