Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <sstream>
- #include <fstream>
- #include <iostream>
- #include <deque>
- #include <iomanip>
- #include "BinaryTree.h"
- #include <queue>
- #include <iostream>
- #include <fstream>
- #include <string>
- ;using namespace std;
- //OTHER VERSION OF OUTPUT
- void show(BinaryTree *&Tree) //Функция обхода
- {
- if (Tree!=NULL) //Пока не встретится пустое звено
- {
- show(Tree->left); //Рекурсивная функция для вывода левого поддерева
- cout<<Tree->data; //Отображаем корень дерева
- show(Tree->right); //Рекурсивная функци для вывода правого поддерева
- }//else cout<<"ty loh";
- }
- //OTHER VERSION OF OUTPUT 2
- void printLevelOrder(BinaryTree *root) {
- if (!root) return;
- queue<BinaryTree*> nodesQueue;
- int nodesInCurrentLevel = 1;
- int nodesInNextLevel = 0;
- nodesQueue.push(root);
- while (!nodesQueue.empty()) {
- BinaryTree *currNode = nodesQueue.front();
- nodesQueue.pop();
- nodesInCurrentLevel--;
- if (currNode) {
- cout << currNode->data << " ";
- nodesQueue.push(currNode->left);
- nodesQueue.push(currNode->right);
- nodesInNextLevel += 2;
- }
- if (nodesInCurrentLevel == 0) {
- cout << endl;
- nodesInCurrentLevel = nodesInNextLevel;
- nodesInNextLevel = 0;
- }
- }
- }
- //MAIN IS HERE
- int main() {
- BinaryTree *root = new BinaryTree;
- BinaryTree *current_node= new BinaryTree;
- root=current_node=NULL;
- string str1;
- string str2;
- ifstream infile;
- infile.open("text.txt", ios::in); //
- int a=0;
- std::vector<int> massiv; //перекидываем числа из строки в массив
- while ( !infile.eof() ){
- //infile >> str1;
- getline(infile,str2);
- for (int i=0; i<str2.length(); i++){
- std:: stringstream ss;
- if(str2[i]!=' '){
- ss << str2[i];
- ss >> a;
- massiv.push_back(a);}
- ss.str( std::string() );
- ss.clear();
- //cout<<a<<endl;
- }
- //a=0;
- current_node=root;
- current_node->add_node(current_node,massiv);
- for (int i=0; i<massiv.size();i++)
- cout<<massiv[i]<<' ';
- massiv.clear();
- cout<<endl;
- }
- current_node=root;
- show(current_node);
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement