Advertisement
Guest User

BAUM

a guest
Aug 23rd, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.16 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. struct Node{
  8.     int data;
  9.     Node* left{};
  10.     Node* right{};
  11. };
  12.  
  13. void append(int zahl, Node* &w);
  14. void print_right(Node* h);
  15. void print_left(Node* h);
  16. void clear_right(Node* &h);
  17. void clear_left(Node* &h);
  18. void fout_left(Node* h, ofstream &os);
  19. void fout_right(Node* h, ofstream &os);
  20.  
  21.  
  22. class Baum{
  23.     private:
  24.     Node* t{};
  25.     public:
  26.     Baum(int zahl){
  27.         t = new Node;
  28.         t->data = zahl;
  29.     };
  30.     ~Baum(){
  31.         delete t;
  32.     };
  33.  
  34.     void add(int zahl){
  35.         append(zahl, t);
  36.     }
  37.  
  38.     void print(){
  39.         cout << t->data << endl;
  40.         print_left(t->left);
  41.         print_right(t->right);
  42.     };
  43.  
  44.     void clear(){
  45.         clear_left(t->left);
  46.         clear_right(t->right);
  47.     };
  48.  
  49.     void LoadFromFile(string datei){
  50.         string hi;
  51.         ifstream fin{datei};
  52.         while(!fin.eof()){
  53.             getline(fin, hi);
  54.             add(stoi(hi));
  55.         }
  56.     };
  57.  
  58.     void LoadInFile(string file_name) {
  59.         ofstream fout{file_name}; //output file stream, erwartet als Parameter den namen des Files welches der Funktionsparameter ist
  60.         fout << t->data << endl;
  61.         fout_left(t->left, fout);
  62.         fout_right(t->right, fout);
  63.     }
  64. };
  65.  
  66. void append(int zahl, Node* &w){
  67.     if (w == nullptr){
  68.         w = new Node;
  69.         w->data = zahl;
  70.     }
  71.     else if(w->data > zahl){
  72.         append(zahl, w->left);
  73.     }
  74.     else if(w->data < zahl){
  75.         append(zahl, w->right);
  76.     }
  77. }
  78.  
  79. void fout_left(Node* h, ofstream &os){
  80.     if (h == nullptr){
  81.         return;
  82.     }
  83.     else{
  84.         if (h->right != nullptr){
  85.             fout_right(h->right, os);
  86.         }
  87.         os << h->data << endl;
  88.         fout_left(h->left, os);
  89.     }
  90. };
  91.  
  92.  
  93. void fout_right(Node* h, ofstream &os){
  94.     if (h == nullptr){
  95.         return;
  96.     }
  97.     else{
  98.         if (h->left != nullptr){
  99.             fout_left(h->left, os);
  100.         }
  101.         os << h->data << endl;
  102.         fout_right(h->right, os);
  103.     }
  104. };
  105.  
  106. void print_left(Node* h){
  107.     if (h == nullptr){
  108.         return;
  109.     }
  110.    
  111.     else{
  112.         if (h->right != nullptr){
  113.             print_right(h->right);
  114.         }
  115.         cout << h->data << endl;
  116.         print_left(h->left);
  117.     }
  118. };
  119.  
  120. void print_right(Node* h){
  121.     if (h == nullptr){
  122.         return;
  123.     }
  124.     else{
  125.         if (h->left != nullptr){
  126.             print_left(h->left);
  127.         }
  128.         cout << h->data << endl;
  129.         print_right(h->right);
  130.     }
  131. };
  132.  
  133. void clear_right(Node* &h){
  134.     if (h == nullptr){
  135.         return;
  136.     }
  137.     else{
  138.         if (h->left != nullptr){
  139.             clear_left(h->left);
  140.         }
  141.         clear_right(h->right);
  142.         delete h;
  143.         h = nullptr;
  144.     }
  145. };
  146.  
  147. void clear_left(Node* &h){
  148.     if (h == nullptr){
  149.         return;
  150.     }
  151.     else{
  152.         if (h->right != nullptr){
  153.             clear_right(h->right);
  154.         }
  155.         clear_left(h->left);
  156.         delete h;
  157.         h = nullptr;
  158.     }
  159. };
  160.  
  161. int main(){
  162.     Baum y(4);
  163.     y.LoadFromFile("Test.txt");
  164.     y.print();
  165.     y.LoadInFile("SaveFile.txt");
  166. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement