Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- using namespace std;
- struct Node{
- int data;
- Node* left{};
- Node* right{};
- };
- void append(int zahl, Node* &w);
- void print_right(Node* h);
- void print_left(Node* h);
- void clear_right(Node* &h);
- void clear_left(Node* &h);
- void fout_left(Node* h, ofstream &os);
- void fout_right(Node* h, ofstream &os);
- class Baum{
- private:
- Node* t{};
- public:
- Baum(int zahl){
- t = new Node;
- t->data = zahl;
- };
- ~Baum(){
- delete t;
- };
- void add(int zahl){
- append(zahl, t);
- }
- void print(){
- cout << t->data << endl;
- print_left(t->left);
- print_right(t->right);
- };
- void clear(){
- clear_left(t->left);
- clear_right(t->right);
- };
- void LoadFromFile(string datei){
- string hi;
- ifstream fin{datei};
- while(!fin.eof()){
- getline(fin, hi);
- add(stoi(hi));
- }
- };
- void LoadInFile(string file_name) {
- ofstream fout{file_name}; //output file stream, erwartet als Parameter den namen des Files welches der Funktionsparameter ist
- fout << t->data << endl;
- fout_left(t->left, fout);
- fout_right(t->right, fout);
- }
- };
- void append(int zahl, Node* &w){
- if (w == nullptr){
- w = new Node;
- w->data = zahl;
- }
- else if(w->data > zahl){
- append(zahl, w->left);
- }
- else if(w->data < zahl){
- append(zahl, w->right);
- }
- }
- void fout_left(Node* h, ofstream &os){
- if (h == nullptr){
- return;
- }
- else{
- if (h->right != nullptr){
- fout_right(h->right, os);
- }
- os << h->data << endl;
- fout_left(h->left, os);
- }
- };
- void fout_right(Node* h, ofstream &os){
- if (h == nullptr){
- return;
- }
- else{
- if (h->left != nullptr){
- fout_left(h->left, os);
- }
- os << h->data << endl;
- fout_right(h->right, os);
- }
- };
- void print_left(Node* h){
- if (h == nullptr){
- return;
- }
- else{
- if (h->right != nullptr){
- print_right(h->right);
- }
- cout << h->data << endl;
- print_left(h->left);
- }
- };
- void print_right(Node* h){
- if (h == nullptr){
- return;
- }
- else{
- if (h->left != nullptr){
- print_left(h->left);
- }
- cout << h->data << endl;
- print_right(h->right);
- }
- };
- void clear_right(Node* &h){
- if (h == nullptr){
- return;
- }
- else{
- if (h->left != nullptr){
- clear_left(h->left);
- }
- clear_right(h->right);
- delete h;
- h = nullptr;
- }
- };
- void clear_left(Node* &h){
- if (h == nullptr){
- return;
- }
- else{
- if (h->right != nullptr){
- clear_right(h->right);
- }
- clear_left(h->left);
- delete h;
- h = nullptr;
- }
- };
- int main(){
- Baum y(4);
- y.LoadFromFile("Test.txt");
- y.print();
- y.LoadInFile("SaveFile.txt");
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement