Advertisement
Abe_Matt

Main

Nov 29th, 2013
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.29 KB | None | 0 0
  1. #include <string>
  2. #include <sstream>
  3. #include <fstream>
  4. #include <iostream>
  5. #include <deque>
  6. #include <iomanip>
  7. #include "BinaryTree.h"
  8. #include <queue>        
  9. #include <iostream>
  10. #include <fstream>
  11. #include <string>  
  12. ;using namespace std;
  13.  
  14.  
  15. //OTHER VERSION OF OUTPUT
  16. void show(BinaryTree *&Tree) //Функция обхода
  17. {
  18.     if (Tree!=NULL) //Пока не встретится пустое звено
  19.     {
  20.        show(Tree->left); //Рекурсивная функция для вывода левого поддерева
  21.        cout<<Tree->data; //Отображаем корень дерева
  22.        show(Tree->right); //Рекурсивная функци для вывода правого поддерева
  23.     }//else cout<<"ty loh";
  24. }
  25.  
  26. //OTHER VERSION OF OUTPUT 2
  27.  
  28.  
  29. void printLevelOrder(BinaryTree *root) {
  30.   if (!root) return;
  31.   queue<BinaryTree*> nodesQueue;
  32.   int nodesInCurrentLevel = 1;
  33.   int nodesInNextLevel = 0;
  34.   nodesQueue.push(root);
  35.   while (!nodesQueue.empty()) {
  36.     BinaryTree *currNode = nodesQueue.front();
  37.     nodesQueue.pop();
  38.     nodesInCurrentLevel--;
  39.     if (currNode) {
  40.       cout << currNode->data << " ";
  41.       nodesQueue.push(currNode->left);
  42.       nodesQueue.push(currNode->right);
  43.       nodesInNextLevel += 2;
  44.     }
  45.     if (nodesInCurrentLevel == 0) {
  46.       cout << endl;
  47.       nodesInCurrentLevel = nodesInNextLevel;
  48.       nodesInNextLevel = 0;
  49.     }
  50.   }
  51. }
  52.    
  53.  
  54. //MAIN IS HERE
  55.  
  56. int main() {
  57.  
  58.  
  59.     BinaryTree *root = new BinaryTree;
  60.     BinaryTree *current_node= new BinaryTree;
  61.     root=current_node=NULL;
  62.     string str1;
  63.     string str2;
  64.     ifstream infile;
  65.     infile.open("text.txt", ios::in); //
  66.     int a=0;
  67.    
  68.     std::vector<int> massiv;     //перекидываем числа из строки в массив
  69.  
  70.     while ( !infile.eof() ){
  71.         //infile >> str1;
  72.         getline(infile,str2);
  73.     for (int i=0; i<str2.length(); i++){   
  74.         std:: stringstream ss;
  75.         if(str2[i]!=' '){
  76.         ss << str2[i];
  77.         ss >> a;
  78.         massiv.push_back(a);}
  79.         ss.str( std::string() );
  80.         ss.clear();
  81.         //cout<<a<<endl;
  82.     }
  83.         //a=0;
  84.         current_node=root;
  85.         current_node->add_node(current_node,massiv);
  86.         for (int i=0; i<massiv.size();i++)
  87.         cout<<massiv[i]<<' ';
  88.         massiv.clear();
  89.         cout<<endl;
  90.        
  91.     }  
  92.     current_node=root;
  93.  
  94.     show(current_node);
  95.  
  96.      system("pause");
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement