Advertisement
patryk

Untitled

Apr 3rd, 2014
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #include<conio.h>
  3. #include<iostream>
  4. #include<fstream>
  5. #include<string>
  6. #include<windows.h>
  7. using namespace std;
  8.  
  9. int tab[800000];
  10.  
  11. struct studenci{
  12.     struct studenci *up, *left, *right;
  13.     string imie, nazwisko;
  14.     int indeks;
  15. };
  16.  
  17. //---------------DODAWANIE-ELEMENTOW-DO-DRZEWA--------------
  18. void add_new_student(studenci *&root, string imie, string nazwisko, int indeks){
  19.     studenci *newone, *y=NULL, *x=root;
  20.     newone = new studenci;
  21.     newone->indeks = indeks;
  22.     newone->imie = imie;
  23.     newone->nazwisko = nazwisko;
  24.     newone->left = newone->right = NULL;
  25.  
  26.     while(x){
  27.         y = x;
  28.         x = (newone->indeks < x->indeks) ? x->left : x->right;
  29.     }
  30.     newone->up = y;
  31.     if(!y) root = newone;
  32.     else if (newone->indeks < y->indeks){
  33.         y->left = newone;
  34.     }
  35.     else y->right = newone;
  36. }
  37. //-----------------------------
  38.  
  39. //---------------PRZEGLADANIE-DRZEWA------------------------
  40. void przejdz(studenci *&root){
  41.     if(root)
  42.     {
  43.         przejdz(root->left);
  44.         cout << root->imie << "\t" << root->nazwisko << "\t" << root->indeks << endl;
  45.         przejdz(root->right);
  46.     }
  47. }
  48. //----------------------------
  49.  
  50.  
  51. studenci *search_student(studenci *root, int n){
  52.     studenci *to_delete = root;
  53.     while( (to_delete) && (to_delete->indeks != n)){
  54.         to_delete = (n < to_delete->indeks) ? to_delete->left : to_delete->right;
  55.     }
  56.     return to_delete;
  57. }
  58.  
  59. studenci *find_min(studenci *root, studenci *x){
  60.     x = root;
  61.     while(x->left) x = x->left;
  62.     return x;
  63. }
  64.  
  65. studenci *find_max(studenci *root, studenci *x){
  66.     x = root;
  67.     while(x->right) x = x->right;
  68.     return x;
  69. }
  70.  
  71. studenci *pred(studenci *root, studenci *x){
  72.     if(x->left) return find_max(root, x->left);
  73.  
  74.     studenci *y;
  75.     do{
  76.         y = x;
  77.         x = x->up;
  78.     }while(x && (x->right != y));
  79.     return x;
  80. }
  81.  
  82. studenci *succ(studenci *root, studenci *x){
  83.     if(x->right) return find_min(root, x->right);
  84.  
  85.     studenci *y;
  86.     do{
  87.         y = x;
  88.         x = x->up;
  89.     }while(x && (x->left != y));
  90.  
  91.     return x;
  92. }
  93.  
  94. studenci *remove_student(studenci *root, studenci *to_delete){
  95.     studenci *y = to_delete->up, *z;
  96.  
  97.     if((to_delete->left) && (to_delete->right)){
  98.         z = (rand() % 2) ? remove_student(root, pred(root, to_delete)) : remove_student(root, succ(root, to_delete));
  99.         z->left = to_delete->left; if(z->left) z->left->up = z;
  100.         z->right = to_delete->right; if(z->right) z->right->up = z;
  101.     }else z = (to_delete->left) ? to_delete->left : to_delete->right;
  102.  
  103.     if(z) z->up = y;
  104.     if(!y) root = z;
  105.     else if(y->left == to_delete) y->left = z;
  106.     else y->right = z;
  107.     return to_delete;
  108. }
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120. /*studenci *find_min(studenci *p){
  121.     if(p) while(p->left) p=p->left;
  122.     return p;
  123. }
  124.  
  125. studenci *find_next(studenci *p){
  126.     studenci *temp;
  127.  
  128.     if(p){
  129.         if(p->right) return find_min(p->right);
  130.         else{
  131.             temp = p->up;
  132.             while(temp && (p == temp->right)){
  133.                 p = temp;
  134.                 temp = temp->up;
  135.             }
  136.             return temp;
  137.         }
  138.     }
  139.     return p;
  140. }
  141.  
  142. //-------------USUWANIE-ELEMENTU-Z-DRZEWA-------------------
  143. void delete_student(studenci *root, studenci *to_delete){
  144.     studenci *y, *z;
  145.  
  146.     if(to_delete){
  147.         y = !to_delete->left || !to_delete->right ? to_delete : find_next(to_delete);
  148.         z = y->left ? y->left : y->right;
  149.         if(z) z->up = y->up;
  150.  
  151.         if(!y->up) root = z;
  152.         else if(y == y->up->left) y->up->left = z;
  153.         else    y->up->right = z;
  154.  
  155.         if(y != to_delete) to_delete->indeks = y->indeks;
  156.         delete y;
  157.     }
  158. }
  159. //----------------------------
  160. */
  161.  
  162. double GetTime()
  163. {
  164.   long long f,t;
  165.   QueryPerformanceFrequency((PLARGE_INTEGER)&f);
  166.   QueryPerformanceCounter((PLARGE_INTEGER)&t);
  167.   return (double)t/(double)f;
  168. }
  169.  
  170. int main()
  171. {
  172.     string imie, nazwisko, nazwa;
  173.     int indeks, num_elements=0, i;
  174.     studenci *root = NULL;
  175.  
  176. //-------------------_DODAWANIE-DO-DRZEWA_---------------------
  177.     fstream plik;
  178.     cout << "Podaj nazwe pliku: ";
  179.     cin >>nazwa;
  180.     double one = GetTime();
  181.     plik.open(nazwa.c_str(), ios::in);
  182.     if(plik.good() == true){
  183.         while(!plik.eof()){
  184.             plik >> imie;
  185.             plik >> nazwisko;
  186.             plik >> indeks;
  187.             tab[num_elements] = indeks;
  188.             num_elements++;
  189.             add_new_student(root, imie, nazwisko, indeks);
  190.         }
  191.         plik.close();
  192.     }
  193.     double two = GetTime();
  194.     printf("DODAWANIE\t%d, czas:\t%lf\n", num_elements, (double)(two-one));
  195.  
  196.     plik.open("pomiary.txt", ios::out | ios::app);
  197.     if(plik.good() == true){
  198.         plik << "DODAWANIE:\t" << num_elements << ", czas:\t" << (double)(two-one) << endl;
  199.         plik.close();
  200.     }
  201.  
  202.  
  203. //--------------------WYSZUKIWANIE_W_DRZEWIE---------------------------
  204.     studenci *find_student;
  205.     one = GetTime();
  206.     for(i=0; i<num_elements; i++){
  207.         find_student = root;
  208.         while((find_student) && (find_student->indeks != tab[i])){
  209.             find_student = (tab[i] < find_student->indeks) ? find_student->left : find_student->right;
  210.         }
  211.     }
  212.     two = GetTime();
  213.     printf("WYSZUKIWANIE\t%d, czas:\t%lf\n", num_elements, (double)(two-one));
  214.  
  215.     plik.open("pomiary.txt", ios::out | ios::app);
  216.     if(plik.good() == true){
  217.         plik << "WYSZUKIWANIE:\t" << num_elements << ", czas:\t" << (double)((two-one) / num_elements) << endl ;
  218.         plik.close();
  219.     }
  220.  
  221.  
  222.  
  223. //---------------------USUWANIE-DRZEWA---------------------------------
  224.     studenci *to_delete;
  225.     one = GetTime();
  226.     for(i=0; i<num_elements; i++){
  227.         remove_student(root, search_student(root, tab[i]));
  228.     }
  229.     //przejdz(root);
  230.     getch();
  231.     return 0;
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement