Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "function.h"
- ofstream f("iesire1.txt");
- ofstream fout("iesire.txt");
- void First_Display(int i)
- {
- f << endl << "Adauga un nod";
- f << endl << "Sterge un nod";
- f << endl << "Preorder";
- f << endl << "Inorder";
- f << endl << "Postorder";
- f << endl << "Arborescenta";
- f << endl << endl << "Optiunea dumneavoastra: ";
- if(i==2)
- f << endl << "Alegeti nodul pe care doriti sa-l stergeti";
- }
- void Display(Node* root, int nivel)
- {
- int i;
- if (root != 0)
- {
- Display(root->right, nivel + 1);
- for (i = 0; i <= nivel; i++)
- cout<<" ";
- cout<< endl<<root->data;
- Display(root->left, nivel + 1);
- }
- }
- //programul va primi ca parametru numărul pe care trebuie să îl adauge
- Node* Add_Node(Node* root, int data)
- {//verificam mai intai dacă există o rădăcină (dacă arborele a fost creat)
- //Alocam memorie pentru noul nod si stabilim valorile pentru data,left_child,right_child.
- if (root == NULL)
- {
- root = (Node*)malloc(sizeof(Node));
- root->data = data;
- root->left = root->right = NULL;
- }
- else if (data <= root->data)
- {
- root->left = Add_Node(root->left, data);//se aseaza in stanga parintelui daca este mai mic
- }
- else {
- root->right = Add_Node(root->right, data);//se aseaza in dreapta parintelui daca este mai mare
- }
- return root;
- }
- void preorder(Node* root)//RSD
- {
- //daca nu s-a ajuns la ultimul nod
- if (root != NULL)
- {
- //se viziteaza radacina
- cout<<root->data<<endl;
- //se viziteaza copilul din stanga apoi cel din dreapta
- preorder(root->left);
- preorder(root->right);
- }
- }
- void inorder(Node* root) //SRD
- {
- //daca nu s-a ajuns la ultimul nod
- if (root != NULL)
- {
- inorder(root->left);//se viziteaza copilul din stanga
- cout<<endl<< root->data;//se viziteaza radacina
- inorder(root->right);//se viziteaza copilul din dreapta
- }
- }
- void postorder(Node* root)
- {
- if (root != NULL)//daca nu s-a ajuns la ultimul nod
- {
- postorder(root->left);//se viziteaza copilul din dreapta
- postorder(root->right);//se viziteaza copilul din stanga
- cout<<endl<< root->data;//se viziteaza radacina
- }
- }
- Node* FindMin(Node* root)
- {
- while (root->left != NULL) root = root->left;
- return root;
- }
- Node* Delete(Node* root, int data) {
- if (root == NULL) return root;
- else if (data < root->data)
- root->left = Delete(root->left, data);
- else if (data > root->data)
- root->right = Delete(root->right, data);
- else {
- // Cazul 1:
- if (root->left == NULL && root->right == NULL) {
- free(root);
- root = NULL;
- }
- //Cazul 2:
- else if (root->left == NULL) {
- Node* temp = root;
- root = root->right;
- free(temp);
- }
- else if (root->right == NULL) {
- Node* temp = root;
- root = root->left;
- free(temp);
- }
- // cazul 3:
- else {
- Node* temp = FindMin(root->right);//mergem in dreapta si cautam nr minim
- root->data = temp->data;
- root->right = Delete(root->right, temp->data);
- }
- }
- return root;
- }
- void DeleteT(Node* root)
- {
- if (root == NULL)
- cout << "The tree is empty";
- else
- {
- DeleteT(root->left);
- DeleteT(root->right);
- free(root);
- }
- root = NULL;
- }
- void Save(Node * root)
- {
- if (root != NULL)
- {
- //se viziteaza radacina
- fout << root->data <<endl;
- //se viziteaza copilul din stanga apoi cel din dreapta
- Save(root->left);
- Save(root->right);
- }
- }
- void Load(Node* root)
- {
- fout.close();
- ifstream fin;
- int x;
- DeleteT(root);
- fin.open("iesire.txt", ios::in);
- while (fin>>x)
- root = Add_Node(root, x);
- }
- #include"function.h"
- int main()
- {
- Node* root = NULL;
- int i, n, x;
- do {
- cout << endl << "Adauga un nod";
- cout << endl << "Sterge un nod";
- cout << endl << "Preorder";
- cout << endl << "Inorder";
- cout << endl << "Postorder";
- cout << endl << "Arborescenta";
- cout << endl << endl<< "Optiunea dumneavoastra: ";
- cin>>i;
- //First_Display(i);
- switch (i)
- {
- case 1:
- cin>>n;
- root = Add_Node(root, n);
- break;
- case 2:
- cout<<endl<<"Alegeti nodul pe care doriti sa-l stergeti";
- cin>>x;
- root = Delete(root, x);
- break;
- case 3:
- preorder(root);
- break;
- case 4:
- inorder(root);
- break;
- case 5:
- postorder(root);
- break;
- case 6:
- Display(root, 0);
- break;
- case 7:
- Save(root);
- break;
- case 8:
- Load(root);
- break;
- }
- } while (i);
- }
- #pragma once
- #include<iostream>
- #include<fstream>
- using namespace std;
- typedef struct _Node
- {
- int data;
- struct _Node* left;
- struct _Node* right;
- }
- Node;
- void First_Display(int i);
- void Display(Node* root, int nivel);
- Node* Add_Node(Node* root, int data);
- void preorder(Node* root);
- void inorder(Node* root);
- void postorder(Node* root);
- Node* FindMin(Node* root);
- Node* Delete(Node* root, int data);
- void Save(Node* root);
- void Load(Node* root);
- void DeleteT(Node* root);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement