无法从文本文件c ++中读取输入

时间:2014-02-26 17:54:21

标签: c++ binary-tree

我无法从文本文件中读取输入。我想要做的是从单个输入文件打印多个二叉树。我想为输入的每一行打印一个新的二叉树,但我不确定如何。目前它只是将整个文件读作一棵树。

我输入文件的一个例子是:

  

ABCDEFG

     

BHYTGFHJU

     

KIJUTTEDS

     

JHYGFOKJHSG

这是我的代码的一部分,我相信问题在于:

int main()
{
    BinaryTree <string> BT;

    string line;
    ifstream myfile("input.txt");
    if (myfile.is_open())
    {
        while(getline (myfile, line, ' '))
        {
            BT.InsertData(line);

            cout << "Preorder: ";
            BT.PrintPreorder();
            cout << endl;
            cout << "Inorder: ";
            BT.PrintInorder();
            cout << endl;
            cout << "Postorder: ";
            BT.PrintPostorder();
            cout << endl;
            cout << "Reverse Inorder: ";
            BT.PrintReverseInorder();
            cout << endl;

            BT.PrintPrintTree();
            cout << endl;

        }
        myfile.close();
    }
    return 0;
}

编辑:正如我的评论中所提到的,这是我的BinaryTree类代码。

template <class T>
class BinaryTree
{
    private:
        struct TreeNode
        {
            TreeNode *left;
            TreeNode *right;
            T data;
        };
        TreeNode *root;

    public:

        BinaryTree()
        {
            root = NULL;
        }

        void Inorder(TreeNode *n)
        {
            if(n != NULL)
            {
                Inorder(n -> left);
                cout<< n -> data;
                Inorder(n -> right);
            }
        }

        void PrintInorder()
        {
           Inorder(root);
        }

        void Preorder(TreeNode *n)
        {
            if(n != NULL)
            {
                cout<< n -> data;
                Preorder(n -> left);
                Preorder(n -> right);
            }
        }

        void PrintPreorder()
        {
            Preorder(root);
        }

        void Postorder(TreeNode *n)
        {
            if(n != NULL)
            {
                Postorder(n -> left);
                Postorder(n -> right);
                cout<<n -> data;
            }
        }

        void PrintPostorder()
        {
            Postorder(root);
        }

        void ReverseInorder(TreeNode *n)
        {
            if(n != NULL)
            {
                ReverseInorder(n -> right);
                cout<< n -> data;
                ReverseInorder(n -> left);
            }
        }

        void PrintReverseInorder()
        {
            ReverseInorder(root);
        }

        void PrintTree(TreeNode* n, int lev)
        {
            if (n != NULL)
            {
                PrintTree(n -> right, lev+1);
                for (int i=0; i<lev; i++)
                    cout << "\t";
                cout << n -> data << endl;
                PrintTree(n -> left, lev+1);
            }
        }

        void InsertData(T data)
        {
            TreeNode *t = new TreeNode;
            TreeNode *parent;
            t -> data = data;
            t -> left = NULL;
            t -> right = NULL;
            parent = NULL;

            //is this a new tree?
            if (isEmpty())
                root = t;
            else
            {
               TreeNode *curr;
               curr = root;
               while(curr)
               {
                   parent = curr;
                   if (t -> data > curr -> data)
                        curr = curr -> right;
                   else
                        curr = curr -> left;
               }
               if(t -> data < parent -> data)
                    parent -> left = t;
               else
                    parent -> right =t;
            }

        }
        void PrintPrintTree()
        {
            PrintTree(root, 0);
        }


        bool isEmpty()
        {
            return (root == NULL);
        }

};

1 个答案:

答案 0 :(得分:0)

创建BinaryTrees的向量/数组并在while(getline(myfile,line,''))循环中将其推回。

int main() {
vector <BinaryTree <string> > BT;
int iteration = 0;

string line;
ifstream myfile("input.txt");
if (myfile.is_open())
{
    while(getline (myfile, line))
    { 
        BinaryTree <string> temptree;
        BT.push_back(temptree);
        BT[iteration].InsertData(line);

        cout << "Preorder: ";
        BT[iteration].PrintPreorder();
        cout << endl;
        cout << "Inorder: ";
        BT[iteration].PrintInorder();
        cout << endl;
        cout << "Postorder: ";
        BT[iteration].PrintPostorder();
        cout << endl;
        cout << "Reverse Inorder: ";
        BT[iteration].PrintReverseInorder();
        cout << endl;

        BT[iteration].PrintPrintTree();
        cout << endl;
        iteration++;

    }
    myfile.close();
}
return 0;
}
相关问题