Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.88 KB | None | 0 0
  1. #include "function.h"
  2. ofstream f("iesire1.txt");
  3. ofstream fout("iesire.txt");
  4.  
  5. void First_Display(int i)
  6. {
  7. f << endl << "Adauga un nod";
  8. f << endl << "Sterge un nod";
  9. f << endl << "Preorder";
  10. f << endl << "Inorder";
  11. f << endl << "Postorder";
  12. f << endl << "Arborescenta";
  13. f << endl << endl << "Optiunea dumneavoastra: ";
  14. if(i==2)
  15. f << endl << "Alegeti nodul pe care doriti sa-l stergeti";
  16. }
  17. void Display(Node* root, int nivel)
  18. {
  19. int i;
  20. if (root != 0)
  21. {
  22. Display(root->right, nivel + 1);
  23. for (i = 0; i <= nivel; i++)
  24. cout<<" ";
  25. cout<< endl<<root->data;
  26. Display(root->left, nivel + 1);
  27. }
  28. }
  29.  
  30. //programul va primi ca parametru numărul pe care trebuie să îl adauge
  31. Node* Add_Node(Node* root, int data)
  32. {//verificam mai intai dacă există o rădăcină (dacă arborele a fost creat)
  33. //Alocam memorie pentru noul nod si stabilim valorile pentru data,left_child,right_child.
  34.  
  35. if (root == NULL)
  36. {
  37. root = (Node*)malloc(sizeof(Node));
  38. root->data = data;
  39. root->left = root->right = NULL;
  40. }
  41. else if (data <= root->data)
  42. {
  43. root->left = Add_Node(root->left, data);//se aseaza in stanga parintelui daca este mai mic
  44. }
  45. else {
  46. root->right = Add_Node(root->right, data);//se aseaza in dreapta parintelui daca este mai mare
  47. }
  48. return root;
  49. }
  50. void preorder(Node* root)//RSD
  51. {
  52. //daca nu s-a ajuns la ultimul nod
  53. if (root != NULL)
  54. {
  55. //se viziteaza radacina
  56. cout<<root->data<<endl;
  57. //se viziteaza copilul din stanga apoi cel din dreapta
  58. preorder(root->left);
  59. preorder(root->right);
  60. }
  61. }
  62. void inorder(Node* root) //SRD
  63. {
  64. //daca nu s-a ajuns la ultimul nod
  65. if (root != NULL)
  66. {
  67. inorder(root->left);//se viziteaza copilul din stanga
  68. cout<<endl<< root->data;//se viziteaza radacina
  69. inorder(root->right);//se viziteaza copilul din dreapta
  70. }
  71. }
  72. void postorder(Node* root)
  73. {
  74. if (root != NULL)//daca nu s-a ajuns la ultimul nod
  75. {
  76. postorder(root->left);//se viziteaza copilul din dreapta
  77. postorder(root->right);//se viziteaza copilul din stanga
  78. cout<<endl<< root->data;//se viziteaza radacina
  79. }
  80. }
  81.  
  82. Node* FindMin(Node* root)
  83. {
  84. while (root->left != NULL) root = root->left;
  85. return root;
  86. }
  87. Node* Delete(Node* root, int data) {
  88. if (root == NULL) return root;
  89. else if (data < root->data)
  90. root->left = Delete(root->left, data);
  91.  
  92. else if (data > root->data)
  93. root->right = Delete(root->right, data);
  94.  
  95. else {
  96. // Cazul 1:
  97. if (root->left == NULL && root->right == NULL) {
  98. free(root);
  99. root = NULL;
  100. }
  101. //Cazul 2:
  102. else if (root->left == NULL) {
  103. Node* temp = root;
  104. root = root->right;
  105. free(temp);
  106. }
  107. else if (root->right == NULL) {
  108. Node* temp = root;
  109. root = root->left;
  110. free(temp);
  111. }
  112. // cazul 3:
  113. else {
  114. Node* temp = FindMin(root->right);//mergem in dreapta si cautam nr minim
  115. root->data = temp->data;
  116. root->right = Delete(root->right, temp->data);
  117. }
  118. }
  119. return root;
  120. }
  121. void DeleteT(Node* root)
  122. {
  123. if (root == NULL)
  124. cout << "The tree is empty";
  125. else
  126. {
  127. DeleteT(root->left);
  128. DeleteT(root->right);
  129. free(root);
  130. }
  131. root = NULL;
  132. }
  133. void Save(Node * root)
  134. {
  135. if (root != NULL)
  136. {
  137. //se viziteaza radacina
  138. fout << root->data <<endl;
  139. //se viziteaza copilul din stanga apoi cel din dreapta
  140. Save(root->left);
  141. Save(root->right);
  142. }
  143.  
  144. }
  145. void Load(Node* root)
  146. {
  147. fout.close();
  148. ifstream fin;
  149. int x;
  150. DeleteT(root);
  151. fin.open("iesire.txt", ios::in);
  152. while (fin>>x)
  153. root = Add_Node(root, x);
  154.  
  155.  
  156. }
  157.  
  158.  
  159.  
  160. #include"function.h"
  161.  
  162.  
  163.  
  164. int main()
  165.  
  166. {
  167. Node* root = NULL;
  168. int i, n, x;
  169. do {
  170. cout << endl << "Adauga un nod";
  171. cout << endl << "Sterge un nod";
  172. cout << endl << "Preorder";
  173. cout << endl << "Inorder";
  174. cout << endl << "Postorder";
  175. cout << endl << "Arborescenta";
  176. cout << endl << endl<< "Optiunea dumneavoastra: ";
  177.  
  178. cin>>i;
  179. //First_Display(i);
  180. switch (i)
  181. {
  182. case 1:
  183. cin>>n;
  184. root = Add_Node(root, n);
  185. break;
  186. case 2:
  187. cout<<endl<<"Alegeti nodul pe care doriti sa-l stergeti";
  188. cin>>x;
  189. root = Delete(root, x);
  190. break;
  191. case 3:
  192. preorder(root);
  193. break;
  194. case 4:
  195. inorder(root);
  196. break;
  197. case 5:
  198. postorder(root);
  199. break;
  200. case 6:
  201. Display(root, 0);
  202. break;
  203. case 7:
  204. Save(root);
  205. break;
  206. case 8:
  207. Load(root);
  208. break;
  209. }
  210.  
  211. } while (i);
  212. }
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220. #pragma once
  221.  
  222. #include<iostream>
  223. #include<fstream>
  224. using namespace std;
  225.  
  226.  
  227. typedef struct _Node
  228. {
  229. int data;
  230. struct _Node* left;
  231. struct _Node* right;
  232. }
  233. Node;
  234.  
  235. void First_Display(int i);
  236. void Display(Node* root, int nivel);
  237. Node* Add_Node(Node* root, int data);
  238. void preorder(Node* root);
  239. void inorder(Node* root);
  240. void postorder(Node* root);
  241. Node* FindMin(Node* root);
  242. Node* Delete(Node* root, int data);
  243. void Save(Node* root);
  244. void Load(Node* root);
  245. void DeleteT(Node* root);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement