Advertisement
Dani_info

Arbore binar_dinamic + parcurgeri

May 8th, 2020
1,909
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.99 KB | None | 0 0
  1. // arbore binar_dinamic.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3.  
  4. #include <iostream>
  5. #pragma warning(disable:4996)
  6.  
  7. using namespace std;
  8.  
  9. struct nod {
  10.     int info;
  11.     nod* stg;
  12.     nod* dr;
  13. };
  14.  
  15. void creare(nod* n);
  16.  
  17. void creare_stg(nod *tata) {
  18.     int info;
  19.     cout << "Introdu fiul stang al lui " << tata->info<<" "; cin >> info;
  20.     if (info) {
  21.         nod* n = new nod;
  22.         n->info = info;
  23.         tata->stg = n;
  24.         tata = n;
  25.         creare(n);
  26.     }
  27.     else
  28.         tata->stg = NULL;
  29. }
  30.  
  31. void creare_dr(nod* tata) {
  32.     int info;
  33.     cout << "Introdu fiul drept al lui " << tata->info<<" "; cin >> info;
  34.     if (info) {
  35.         nod* n = new nod;
  36.         n->info = info;
  37.         tata->dr = n;
  38.         tata = n;
  39.         creare(n);
  40.     }
  41.     else
  42.         tata->dr = NULL;
  43. }
  44.  
  45. void creare(nod*n) {
  46.     creare_stg(n);
  47.     creare_dr(n);
  48. }
  49.  
  50.  
  51. void srd(nod* n){
  52.     if (n){
  53.         srd(n->stg);
  54.         cout << n->info << " ";
  55.         srd(n->dr);
  56.     }
  57. }
  58.  
  59. void rsd(nod* n) {
  60.     if (n) {
  61.         cout << n->info << " ";
  62.         rsd(n->stg);
  63.         rsd(n->dr);
  64.     }
  65. }
  66.  
  67. void sdr(nod* n) {
  68.     if (n) {
  69.         sdr(n->stg);
  70.         sdr(n->dr);
  71.         cout << n->info << " ";
  72.     }
  73. }
  74. int main()
  75. {  
  76.     nod* radacina = new nod;
  77.     cout << "Introduceti radacina:"; cin >> radacina->info;
  78.     creare(radacina);
  79.     cout << "SRD: "; srd(radacina);
  80.     cout << endl;
  81.     cout << "RSD: "; rsd(radacina);
  82.     cout << endl;
  83.     cout << "SDR: "; sdr(radacina);
  84.     return 0;
  85. }
  86.  
  87. // Run program: Ctrl + F5 or Debug > Start Without Debugging menu
  88. // Debug program: F5 or Debug > Start Debugging menu
  89.  
  90. // Tips for Getting Started:
  91. //   1. Use the Solution Explorer window to add/manage files
  92. //   2. Use the Team Explorer window to connect to source control
  93. //   3. Use the Output window to see build output and other messages
  94. //   4. Use the Error List window to view errors
  95. //   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
  96. //   6. In the future, to open this project again, go to File > Open > Project and select the .sln file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement