2014-02-26 50 views
1

我無法打印我的二進制樹的順序,預訂和後序。我在讀cin的時候讓它工作。但是,現在我正在從txt文件中讀取數據,我按順序,預訂單和後期訂單全部打印出從txt文件讀入的內容。我提供了相信問題的代碼,如果需要查看我的其他代碼,請發表評論。按順序排列故障打印,二叉樹的後序排序

我的代碼:

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; 
} 
+0

「但是,現在我正在從txt文件中讀取數據,按順序排列,預訂單和後期訂單都打印出從txt文件讀入的內容。」 - 聽起來很正確。有什麼問題? – Shoe

+0

如果我的文件讀取'abcd',我的inorder,preorder和postorder全部打印出'abcd'。但是,後期應打印出反向'dcba' – user3335367

回答

0

是您的輸入應該是一次一個字符?看起來當你想要一次接受一個字符時,你會接受文本行,這將解釋你的輸出('abcd'是單個節點,你的根)。

+0

我該如何將它一次更改爲一個字符?現在你就這樣解釋了,我覺得這可能是問題 – user3335367

+0

你的程序一行一行地抓取輸入,所以要麼使用get()一次從流中檢索一個字符,要麼將樹中的每個元素都放入在輸入文件中它自己的行 – MrDiggles