Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <stdlib.h>
  4.  
  5. typedef struct nod
  6. {
  7. int valoare;
  8. struct nod *st;
  9. struct nod *dr;
  10. }nod;
  11.  
  12. nod *rad1=NULL;
  13. nod *rad2=NULL;
  14.  
  15. nod* adaugare1(nod *t, int v)
  16. {
  17. if(t==NULL){
  18. t=(nod*)malloc(sizeof(nod));
  19. t->valoare=v;
  20. t->st=NULL;
  21. t->dr=NULL;
  22. }
  23. else
  24. if(t->valoare>v)
  25. t->st=adaugare1(t->st,v);
  26. else
  27. if(t->valoare<v)
  28. t->dr=adaugare1(t->dr,v);
  29. else
  30. printf("Elementul deja exista in arbore.\n");
  31. return t;
  32. }
  33. nod* adaugare2(nod *t, int v)
  34. {
  35. if(t==NULL){
  36. t=(nod*)malloc(sizeof(nod));
  37. t->valoare=v;
  38. t->st=NULL;
  39. t->dr=NULL;
  40. }
  41. else
  42. if(t->valoare>v)
  43. t->st=adaugare2(t->st,v);
  44. else
  45. if(t->valoare<v)
  46. t->dr=adaugare2(t->dr,v);
  47. else
  48. printf("Elementul deja exista in arbore.\n");
  49. return t;
  50. }
  51.  
  52. void citire1()
  53. {
  54. FILE *f;
  55. int v;
  56. f=fopen("in.txt","rt");
  57. if(f==NULL)
  58. printf("fisierul nu poate fi deschis\n");
  59. else{
  60. while(!feof(f)){
  61. fscanf(f,"%d",&v);
  62. rad1=adaugare1(rad1,v);
  63. }
  64. fclose(f);
  65. }
  66. }
  67.  
  68. void citire2()
  69. {
  70. FILE *f;
  71. int v;
  72. f=fopen("in2.txt","rt");
  73. if(f==NULL)
  74. printf("fisierul nu poate fi deschis\n");
  75. else{
  76. while(!feof(f)){
  77. fscanf(f,"%d",&v);
  78. rad2=adaugare2(rad2,v);
  79. }
  80. fclose(f);
  81. }
  82. }
  83.  
  84. void afisare1(nod *t)
  85. {
  86. if(t != NULL){
  87. afisare1(t->st);
  88. printf(" %d", t->valoare);
  89. afisare1(t->dr);
  90. }
  91. }
  92.  
  93. void afisare2(nod *t)
  94. {
  95. if(t != NULL){
  96. afisare2(t->st);
  97. printf(" %d", t->valoare);
  98. afisare2(t->dr);
  99. }
  100. }
  101.  
  102. nod *elim1(nod *t, nod *p)
  103. {
  104. nod *q, *q1;
  105. q = t;
  106. if(q->dr != NULL)
  107. {
  108. q->dr = elim1(q->dr, p);
  109. }
  110. else
  111. {
  112. q1 = q;
  113. p->valoare = q->valoare;
  114. q = q->st;
  115. }
  116. return q;
  117. }
  118.  
  119. nod *elim2(nod *t, nod *p)
  120. {
  121. nod *q, *q1;
  122. q = t;
  123. if(q->dr != NULL)
  124. {
  125. q->dr = elim2(q->dr, p);
  126. }
  127. else
  128. {
  129. q1 = q;
  130. p->valoare = q->valoare;
  131. q = q->st;
  132. }
  133. return q;
  134. }
  135.  
  136. nod *sterge1(nod *t, int valoare)
  137. {
  138. nod *q, *q1;
  139. if(t == NULL)
  140. printf("Nodul nu exista!");
  141. else
  142. if(t->valoare > valoare)
  143. t->st = sterge1(t->st, valoare);
  144. else{
  145. if(t->valoare < valoare)
  146. t->dr = sterge1(t->dr, valoare);
  147. else{
  148. if(t->st == NULL)
  149. {
  150. q1 = t;
  151. q = t->dr;
  152. free(q1);
  153. return q;
  154. }
  155. else
  156. if(t->dr == NULL)
  157. {
  158. q1 = t;
  159. q = t->st;
  160. free(q1);
  161. return q;
  162. }
  163. else
  164. t->st = elim1(t->st, t);
  165. }
  166. }
  167. return t;
  168. }
  169.  
  170. nod *sterge2(nod *t, int valoare)
  171. {
  172. nod *q, *q1;
  173. if(t == NULL)
  174. printf("Nodul nu exista!");
  175. else
  176. if(t->valoare > valoare)
  177. t->st = sterge2(t->st, valoare);
  178. else{
  179. if(t->valoare < valoare)
  180. t->dr = sterge2(t->dr, valoare);
  181. else{
  182. if(t->st == NULL)
  183. {
  184. q1 = t;
  185. q = t->dr;
  186. free(q1);
  187. return q;
  188. }
  189. else
  190. if(t->dr == NULL)
  191. {
  192. q1 = t;
  193. q = t->st;
  194. free(q1);
  195. return q;
  196. }
  197. else
  198. t->st = elim2(t->st, t);
  199. }
  200. }
  201. return t;
  202. }
  203. int main(){
  204. citire1();
  205. citire2();
  206. printf("Arborii completi sunt: \n");
  207. afisare1(rad1);
  208. printf("\n");
  209. afisare2(rad2);
  210. sterge2(rad2,5);
  211. sterge1(rad1,4);
  212. printf("\nArborii dupa eliminarea elementelor dorite sunt: \n");
  213. afisare1(rad1);
  214. printf("\n");
  215. afisare2(rad2);
  216. getch();
  217. return 0;
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement