c++ - 从文件读取到二叉树并写入文件

标签 c++ binary-tree

我正在将一个名为“Lincoln.txt”的文件读入二叉树,并按字母顺序将它们放入我的树中,我应该将单词和单词的计数写入名为“index.txt”的文件中。 TXT”。现在,我可以毫无问题地将它们读入我的二叉树,但我不太确定如何将它们写入“index.txt”。完成后我检查我的文件,我得到的只是地址。你能帮帮我吗?

这是我的代码:

主要.cpp

#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();
}

节点类.cpp

#include <iostream>
#include "NodeClass.h"

using namespace std;

void BinaryTree::insert(ElementType data, TreeNode *&tree)
{
    if (tree == NULL)
    {
        tree = new TreeNode(data);
    }
    else if(data < tree->data)
        insert(data, tree->left);
    else if(data > tree->data)
        insert(data, tree->right);
    else if(data == tree->data)
        tree->count++;

}

void BinaryTree::display(TreeNode *tree, ostream& out)
{
    if (tree != NULL)
    {
        display(tree->left, out);
        out << tree->data<<" ";
        display(tree->right, out);
    }
}

void BinaryTree::print(TreeNode *tree, ofstream& outFile)
{

    if (tree != NULL)
    {
        outFile << tree->left;
        outFile << tree->data << " " << tree->count;
        outFile << tree->right;
    }

}

节点类.h

#include <iostream>
#include <string>
#include <fstream>
#include <fstream>

using namespace std;

#ifndef TREENODE_H
#define TREENODE_H

typedef std::string ElementType;

class BinaryTree
{
public:
    class TreeNode
    {
    public:
        ElementType data;
        int count;
        TreeNode *left;
        TreeNode *right;
        TreeNode(ElementType new_data)
            { data = new_data; left = NULL; right = NULL;};     
    };
TreeNode *root;
BinaryTree(ElementType root_data)
    { root = new TreeNode(root_data); };
~BinaryTree() { delete root;};
void insert(ElementType data, TreeNode *&tree);
void display(TreeNode *tree, ostream& out);
void print(TreeNode *tree, ofstream& outFile);
};

#endif TREENODE_H

林肯.txt

 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. 

写入 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);
    }
}

引用wiki了解遍历是如何工作的。上面给出的称为Inorder traversal

  1. 先遍历左子树
  2. 遍历根
  3. 先遍历右子树

关于c++ - 从文件读取到二叉树并写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23187376/

相关文章:

c++ - 可以使用指针创建类的实例吗?

c++ - 如何在不打开 GUI 的情况下从 Visual Studio 执行 WinZip?

c++ - 将 C++ 链接到 gams 软件

java - 实现二叉树的节点类

c - 如何统计二叉树的总节点数

algorithm - 当给定节点的二叉树时,如何编写返回节点链表的递归函数?

c++ - 模板-模板参数给出神秘的类型/值不匹配

c++ - 多次从第一行读取文本文件(C++)

c - 二叉搜索树代码不起作用

algorithm - 红黑树存储颜色信息时如何节省内存?