Advertisement
Guest User

ABC

a guest
Oct 19th, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. using namespace std;
  4.  
  5. struct NodABC {
  6. NodABC *fiu_st;
  7. int info;
  8. int inaltime;
  9. NodABC *fiu_dr;
  10. };
  11.  
  12. NodABC*creareNod_ABC(int info_nou){
  13.  
  14. NodABC *nod_nou = new NodABC;
  15. nod_nou->info = info_nou;
  16. nod_nou->fiu_st = NULL;
  17. nod_nou->fiu_dr = NULL;
  18. nod_nou->inaltime = 0;
  19.  
  20. return nod_nou;
  21. }
  22.  
  23. NodABC* cautare(NodABC* nod, int info_caut)
  24. {
  25. NodABC* tmp = nod;
  26. while (tmp != NULL) {
  27. if (info_caut<tmp->info) {
  28. tmp = tmp->fiu_st;
  29. }
  30. else if (info_caut>tmp->info) {
  31. tmp = tmp->fiu_dr;
  32. }
  33. else {
  34. return tmp;
  35. }
  36. }
  37. return NULL;
  38. }
  39.  
  40. NodABC* actualizare(NodABC* nod, int info_caut, int info_nou) {
  41. NodABC* tmp = cautare(nod, info_caut);
  42. if (tmp != NULL) {
  43. if (tmp->info == info_caut) {
  44. tmp->info = info_nou;
  45. }
  46. }
  47. return nod;
  48. }
  49. int inaltime_ABC(NodABC *ROOT) {
  50. if (ROOT == NULL)
  51. return -1;
  52.  
  53. int stanga = inaltime_ABC(ROOT->fiu_st);
  54. int dreapta = inaltime_ABC(ROOT->fiu_dr);
  55.  
  56. if (stanga > dreapta)
  57. return stanga + 1;
  58. else
  59. return dreapta + 1;
  60. }
  61.  
  62. NodABC *inserare_ABC(NodABC *ROOT, int info_nou) {
  63. NodABC *nod_nou = creareNod_ABC(info_nou);
  64.  
  65. if (ROOT == NULL) {
  66. ROOT = nod_nou;
  67. }
  68. else {
  69. NodABC *tmp = ROOT;
  70. NodABC *tata = NULL;
  71.  
  72. while (tmp != NULL) {
  73. tata = tmp;
  74.  
  75. if (tmp->info < info_nou) {
  76. tmp = tmp->fiu_dr;
  77. }
  78. else if (tmp->info > info_nou) {
  79. tmp = tmp->fiu_st;
  80. }
  81. else {
  82. cout << "Eroare, cheie dubla!\n";
  83. return ROOT;
  84. }
  85. }
  86. if (nod_nou->info < tata->info) {
  87. tata->fiu_st = nod_nou;
  88. }
  89. else {
  90. tata->fiu_dr = nod_nou;
  91. }
  92. }
  93. nod_nou->inaltime = inaltime_ABC(ROOT);
  94.  
  95. return ROOT;
  96. }
  97.  
  98.  
  99. void preordine(NodABC *nod) {
  100. if (nod == NULL) return;
  101. else {
  102. cout << nod->info << " intm = " << nod->inaltime << endl;
  103. preordine(nod->fiu_st);
  104. preordine(nod->fiu_dr);
  105. }
  106. }
  107.  
  108. void inordine(NodABC *nod) {
  109. if (nod == NULL) return;
  110. else {
  111. inordine(nod->fiu_st);
  112. cout << nod->info << " intm = " << nod->inaltime << endl;
  113. inordine(nod->fiu_dr);
  114. }
  115. }
  116.  
  117. void postordine(NodABC* nod) {
  118. if (nod == NULL) return;
  119. else {
  120. postordine(nod->fiu_st);
  121. postordine(nod->fiu_dr);
  122. cout << nod->info << " intm = " << nod->inaltime << endl;
  123. }
  124. }
  125.  
  126. NodABC* eliberare_ABC(NodABC* nod) {
  127. if (nod != NULL) {
  128. eliberare_ABC(nod->fiu_st);
  129. eliberare_ABC(nod->fiu_dr);
  130. delete nod;
  131. }
  132. else {
  133. cout << "Arbore vid!\n";
  134. }
  135. return NULL;
  136. }
  137.  
  138. NodABC* caz1(NodABC* ROOT, NodABC* nod, NodABC* tata)
  139. {
  140. if (nod == ROOT)
  141. {
  142. delete ROOT;
  143. ROOT = NULL;
  144. return ROOT;
  145. }
  146. else
  147. {
  148. if (nod == tata->fiu_st)
  149. {
  150. tata->fiu_st = NULL;
  151. }
  152. else
  153. {
  154. tata->fiu_dr = NULL;
  155. }
  156. delete nod;
  157. return ROOT;
  158. }
  159. }
  160. NodABC* caz2(NodABC* ROOT, NodABC* nod, NodABC* tata)
  161. {
  162. if (ROOT == nod)
  163. {
  164. if (ROOT->fiu_st)
  165. {
  166. ROOT = ROOT->fiu_st;
  167. }
  168. else
  169. {
  170. ROOT = ROOT->fiu_dr;
  171. }
  172.  
  173. return ROOT;
  174. }
  175. else
  176. {
  177. if (nod == tata->fiu_dr)
  178. {
  179. if (nod->fiu_dr != NULL)
  180. {
  181. tata->fiu_dr = nod->fiu_dr;
  182. }
  183. else
  184. {
  185. tata->fiu_dr = nod->fiu_st;
  186. }
  187. }
  188. else
  189. {
  190. if (nod->fiu_st != NULL)
  191. {
  192. tata->fiu_st = nod->fiu_st;
  193. }
  194. else
  195. {
  196. tata->fiu_st = nod->fiu_dr;
  197. }
  198. }
  199. delete nod;
  200. }
  201.  
  202. return ROOT;
  203. }
  204. NodABC* caz3(NodABC* ROOT, NodABC* nod, NodABC* tata)
  205. {
  206. NodABC* max = nod->fiu_st;
  207. NodABC* tatamax = nod;
  208. while (max->fiu_dr != NULL)
  209. {
  210. tatamax = max;
  211. max = max->fiu_dr;
  212. }
  213. nod->info = max->info;
  214. if (max == tatamax->fiu_st)
  215. {
  216. tatamax->fiu_st = max->fiu_st;
  217. }
  218. else tatamax->fiu_dr = max->fiu_dr;
  219. delete max;
  220. return ROOT;
  221. }
  222. NodABC* stergere_ABC(NodABC* ROOT, int cheie_sters)
  223. {
  224. if (ROOT == NULL)
  225. {
  226. cout << "ABC vid\n";
  227. return ROOT;
  228. }
  229. else
  230. {
  231. NodABC* tmp = ROOT;
  232. NodABC* tata = NULL;
  233. while (tmp != NULL)
  234. {
  235. if (tmp->info == cheie_sters)
  236. {
  237. break;
  238. }
  239. else
  240. {
  241. tata = tmp;
  242. if (tmp->info<cheie_sters)
  243. {
  244. tmp = tmp->fiu_dr;
  245. }
  246. else
  247. {
  248. tmp = tmp->fiu_st;
  249. }
  250. }
  251. }
  252. if (tmp == NULL)
  253. {
  254. cout << "Nu s-a gasit cheia in arbore\n";
  255. return NULL;
  256. }
  257. else
  258. {
  259. if (tmp->fiu_st == NULL && tmp->fiu_dr == NULL)
  260. {
  261. ROOT = caz1(ROOT, tmp, tata);
  262. }
  263. else
  264. if ((tmp->fiu_st != NULL && tmp->fiu_dr == NULL) || (tmp->fiu_st == NULL && tmp->fiu_dr != NULL))
  265. {
  266. ROOT = caz2(ROOT, tmp, tata);
  267. }
  268. else
  269. if (tmp->fiu_st != NULL && tmp->fiu_dr != NULL)
  270. {
  271. ROOT = caz3(ROOT, tmp, tata);
  272. }
  273. return ROOT;
  274. }
  275. }
  276. return ROOT;
  277. }
  278. NodABC *stergere_frunze(NodABC* ROOT)
  279. {
  280. NodABC *tmp = ROOT;
  281. NodABC *tata;
  282.  
  283. if (tmp == NULL)return NULL;
  284. else {
  285. if (tmp->fiu_dr == NULL && tmp->fiu_st == NULL) {
  286. delete tmp;
  287. ROOT->fiu_dr = NULL;
  288. ROOT->fiu_st = NULL;
  289. return ROOT;
  290. }
  291.  
  292. stergere_frunze(tmp->fiu_st);
  293. stergere_frunze(tmp->fiu_dr);
  294. }
  295. }
  296.  
  297.  
  298. int main() {
  299. int meniu;
  300. NodABC* ROOT = NULL;
  301.  
  302. do
  303. {
  304. cout << "0. Iesire din program\n";
  305. cout << "1.Initializare\n";
  306. cout << "2.Inserare ABC\n";
  307. cout << "3.Traversare preordine\n";
  308. cout << "4.Traversare inordine\n";
  309. cout << "5.Traversare postordine\n";
  310. cout << "6.Actualizare\n";
  311. cout << "7.Cautare\n";
  312. cout << "8.Stergere\n";
  313. cout << "9.Eliberare\n";
  314. cout << "10.Op. Extinse\n";
  315.  
  316. cout << "Alegeti varianta dorita (0-10):";
  317. cin >> meniu;
  318.  
  319. cout << "\n\n";
  320. switch (meniu)
  321. {
  322. case 0:break;
  323. case 1:
  324. {
  325. cout << "1.Initializare\n";
  326. ROOT = NULL;
  327. break;
  328. }
  329. case 2:
  330. {
  331. cout << "2.Inserare ABC\n";
  332. int info_nou;
  333. cout << "Info_nou\n";
  334. cin >> info_nou;
  335. ROOT = inserare_ABC(ROOT, info_nou);
  336. break;
  337. }
  338. case 3:
  339. {
  340. cout << "3.Traversare preordine\n";
  341. preordine(ROOT);
  342. break;
  343. }
  344. case 4:
  345. {
  346. cout << "Traversare inordine\n";
  347. inordine(ROOT);
  348. break;
  349. }
  350. case 5:
  351. {
  352. cout << "5.Traversare postordine\n";
  353. postordine(ROOT);
  354. break;
  355. }
  356. case 6:
  357. {
  358. cout << "6.Actualizare\n";
  359. int info_caut, info_nou;
  360. cout << "Info caut\n";
  361. cin >> info_caut;
  362. cout << "Introduceti valoarea noua\n";
  363. cin >> info_nou;
  364. actualizare(ROOT, info_caut, info_nou);
  365. break;
  366. }
  367. case 7:
  368. {
  369. cout << "7.Cautare\n";
  370. int info_caut;
  371. cout << "Info caut\n";
  372. cin >> info_caut;
  373. NodABC* var = cautare(ROOT, info_caut);
  374. if (var != NULL)
  375. {
  376. cout << "Elementul " << var->info << " exista in arbore\n";
  377. }
  378. else
  379. {
  380. cout << "Elementul nu exista in arbore\n";
  381. }
  382. break;
  383. }
  384. case 8:
  385. {
  386. cout << "8.Stergere\n";
  387. int info_sters;
  388. cout << "Info_sters\n";
  389. cin >> info_sters;
  390. ROOT = stergere_ABC(ROOT, info_sters);
  391. break;
  392. }
  393. case 9:
  394. {
  395. cout << "9.Eliberare\n";
  396. ROOT = eliberare_ABC(ROOT);
  397. break;
  398. }
  399. case 10:
  400. {
  401. int x;
  402. do {
  403. cout << "\nApasati Enter pentru a sterge ecranul\n";
  404. _getch();
  405. system("cls");
  406.  
  407. cout << "0. Iesire.\n"
  408. << "1. Stergere frunze\n"
  409. << "Stergere nivel\n";
  410. cin >> x;
  411.  
  412. switch (x)
  413. {
  414. case 0: break;
  415. case 1:
  416.  
  417. cout << "Stergere frunze\n";
  418. ROOT = stergere_frunze(ROOT);
  419. break;
  420. default:
  421. break;
  422. }
  423.  
  424. } while (x != 0);
  425. }
  426. default:
  427. {
  428. cout << "Varianta gresita\n";
  429. }
  430. }
  431. cout << "\nApasati Enter pentru a sterge ecranul\n";
  432. _getch();
  433. system("cls");
  434. } while (meniu != 0);
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445. system("PAUSE");
  446. return 0;
  447. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement