CosminVarlan

Arbore prin vectori de tati

Apr 22nd, 2026
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.00 KB | None | 0 0
  1. // cnstruieste arbore aleator si il exporta ca vector de tati.
  2. // numarul de noduri este in define si la fel daca se doreste exportarea fin data.txt
  3.  
  4. #include <iostream>
  5. #include <fstream>
  6. #define N 100
  7. #define USE_FILE 1
  8. // output file: data.txt
  9.  
  10. using namespace std;
  11.  
  12. ofstream fout("data.txt");
  13.  
  14. int t[N+1];
  15. int folosite[N+1], cntf;
  16. int ramase[N+1], cntr;
  17.  
  18.  
  19. void pickNew(){
  20.     int poz_in_ramase = rand()%cntr;
  21.     int nod_ales = ramase[poz_in_ramase];
  22.     //cout << poz_in_ramase << ' ' << nod_ales << endl;
  23.  
  24.     int parinte = folosite[rand()%cntf];
  25.     folosite[cntf++] = nod_ales;
  26.     for(int i=poz_in_ramase+1; i<=cntr; i++)
  27.         ramase[i-1] = ramase[i];
  28.     cntr--;
  29.     t[nod_ales] = parinte;
  30.   /*
  31.     cout << "Folosite: ";
  32.     for(int i=0; i<cntf; i++)
  33.         cout << folosite[i]<<' ';
  34.     cout << "\nRamase:   ";
  35.     for(int i=0; i<cntr; i++)
  36.         cout << ramase[i]<<' ';
  37.     cout << "\nTati    : ";
  38.     for(int i=1; i<=N; i++)
  39.         cout << t[i]<<' ';
  40.     cout << "\n-----------------\n";
  41.     */
  42. }
  43.  
  44.  
  45.  
  46. int main()
  47. {
  48.     srand(time(0));
  49.  
  50.     for(int i=0; i<N; i++)
  51.         ramase[i] = i+1;
  52.     cntr = N;
  53.  
  54.     int poz_in_ramase = rand()%cntr;
  55.     folosite[0] = ramase[poz_in_ramase];
  56.     t[ramase[poz_in_ramase]] = 0; /// tatal
  57.     cntf=1;
  58.     for(int i=poz_in_ramase; i<cntr; i++)
  59.         ramase[i] = ramase[i+1];
  60.     cntr--;
  61. /*
  62.     cout << "Folosite: ";
  63.     for(int i=0; i<cntf; i++)
  64.         cout << folosite[i]<<' ';
  65.     cout << "\nRamase:   ";
  66.     for(int i=0; i<cntr; i++)
  67.         cout << ramase[i]<<' ';
  68.     cout << "\nTati    : ";
  69.     for(int i=1; i<=N; i++)
  70.         cout << t[i]<<' ';
  71.     cout << "\n-----------------\n\n\n";
  72. */
  73.  
  74.     for(int i=1; i<N; i++)
  75.         pickNew();
  76.  
  77.  
  78.     if (USE_FILE)
  79.     {
  80.         fout << N << endl;
  81.         for(int i=1; i<=N; i++)
  82.             fout << t[i]<<' ';
  83.     }
  84.     else
  85.     {
  86.         cout << N << endl;
  87.         for(int i=1; i<=N; i++)
  88.             cout << t[i]<<' ';
  89.     }
  90.  
  91.  
  92.     return 0;
  93. }
  94.  
Tags: Tree Builder
Advertisement
Add Comment
Please, Sign In to add comment