C++ 从文件读入二叉树并写入文件
我在一棵二叉树上读取一个名为“Lincoln.txt”的文件,并将它们按字母顺序放入我的树中,我应该将单词和单词计数写入一个名为“index.txt”的文件中。现在,我可以把它们读入我的二叉树中,没有问题,但我不太确定如何将它们写入“index.txt”。我检查完我的文件后,我得到的是地址。你能帮我吗 这是我的代码: main.cppC++ 从文件读入二叉树并写入文件,c++,binary-tree,C++,Binary Tree,我在一棵二叉树上读取一个名为“Lincoln.txt”的文件,并将它们按字母顺序放入我的树中,我应该将单词和单词计数写入一个名为“index.txt”的文件中。现在,我可以把它们读入我的二叉树中,没有问题,但我不太确定如何将它们写入“index.txt”。我检查完我的文件后,我得到的是地址。你能帮我吗 这是我的代码: main.cpp #include <iostream> #include "NodeClass.h" #include <fstream> using
#include <iostream>
#include "NodeClass.h"
#include <fstream>
using namespace std;
int main()
{
string myString;
BinaryTree myTree("");
ifstream infile;
infile.open("Lincoln.txt");
while(infile)
{
infile >> myString;
myTree.insert(myString, myTree.root);
}
ofstream outFile;
outFile.open("index.txt");
myTree.print(myTree.root, outFile);
outFile.close();
}
在写入index.txt后,我得到以下结果:
00000000 -8421504510035D230
编辑:我的教授说我的打印函数应该是递归函数,但我不知道怎么做。你需要一个递归来遍历所有节点并打印它们。试试这个
void BinaryTree::print(TreeNode *tree, ofstream& outFile)
{
if (tree != NULL)
{
print( tree->left, outfile);
outFile << tree->data << " " << tree->count << ".";
print( tree->right, outfile);
}
}
void BinaryTree::print(TreeNode*tree,of stream&outFile)
{
如果(树!=NULL)
{
打印(树->左,输出文件);
outFile data谢谢你,但我不明白为什么我在打印我的计数时会得到这么大的数字!因为你还没有在constructorwow中初始化count
。天哪,我是个白痴。非常感谢你的帮助。祝你愉快,先生。
The Gettysburg Address
Gettysburg, Pennsylvania
November 19, 1863
Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in
Liberty, and dedicated to the proposition that all men are created equal.
Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and
so dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate
a portion of that field, as a final resting place for those who here gave their lives that that nation
might live. It is altogether fitting and proper that we should do this.
But, in a larger sense, we can not dedicate -- we can not consecrate -- we can not hallow -- this ground.
The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add
or detract. The world will little note, nor long remember what we say here, but it can never forget what
they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they
who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great
task remaining before us -- that from these honored dead we take increased devotion to that cause for
which they gave the last full measure of devotion -- that we here highly resolve that these dead shall not
have died in vain -- that this nation, under God, shall have a new birth of freedom -- and that government
of the people, by the people, for the people, shall not perish from the earth.
00000000 -8421504510035D230
void BinaryTree::print(TreeNode *tree, ofstream& outFile)
{
if (tree != NULL)
{
print( tree->left, outfile);
outFile << tree->data << " " << tree->count << ".";
print( tree->right, outfile);
}
}