Advertisement
Alchemik96

AISD DRZEWA PLIKI

Apr 26th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. #include <conio.h>
  2. #include <stdio.h>
  3. #include <malloc.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6.  
  7. struct ElementDrzewa
  8. {
  9. double Liczba;
  10. ElementDrzewa *Lewy, *Prawy;
  11. };
  12.  
  13. ElementDrzewa* NowyElement(double liczba)
  14. {
  15. ElementDrzewa *Nowy=(ElementDrzewa*)malloc(sizeof(ElementDrzewa));
  16. Nowy->Liczba=liczba;
  17. Nowy->Lewy=Nowy->Prawy=NULL;
  18. return Nowy;
  19. }
  20.  
  21. void ZwolnijDrzewo(ElementDrzewa* Korzen)
  22. {
  23. if(Korzen->Lewy) ZwolnijDrzewo(Korzen->Lewy);
  24. if(Korzen->Prawy) ZwolnijDrzewo(Korzen->Prawy);
  25. free(Korzen);
  26. }
  27.  
  28. void WstawElement(ElementDrzewa* Korzen, double liczba)
  29. {
  30. for(;;)
  31. {
  32. if(liczba<Korzen->Liczba)
  33. {
  34. if(Korzen->Lewy) Korzen=Korzen->Lewy;
  35. else { Korzen->Lewy=NowyElement(liczba); break; }
  36. }
  37. else
  38. {
  39. if(Korzen->Prawy) Korzen=Korzen->Prawy;
  40. else { Korzen->Prawy=NowyElement(liczba); break; }
  41. }
  42. }
  43. }
  44.  
  45. ElementDrzewa* WczytajDrzewo(char *Nazwapliku)
  46. {
  47. unsigned int ilosc;
  48. double liczba;
  49. ElementDrzewa *Drzewo=0;
  50.  
  51. FILE *plik;
  52. plik=fopen(Nazwapliku, "r");
  53. if(plik)
  54. {
  55. fscanf(plik, "%u", &ilosc);
  56. fscanf(plik, "%lf", &liczba);
  57. Drzewo=NowyElement(liczba);
  58. for(int i=1; i<ilosc; i++)
  59. {
  60. fscanf(plik, "%lf", &liczba);
  61. WstawElement(Drzewo, liczba);
  62. }
  63. fclose(plik);
  64. }
  65. else printf("Blad wczytywania pliku!");
  66. return Drzewo;
  67. }
  68.  
  69. unsigned int GlebokoscDrzewa(ElementDrzewa* Korzen)
  70. {
  71. unsigned int l1=0, l2=0;
  72. if(Korzen->Lewy) l1=GlebokoscDrzewa(Korzen->Lewy);
  73. if(Korzen->Prawy) l2=GlebokoscDrzewa(Korzen->Prawy);
  74. if(l1>l2) return ++l1;
  75. else return ++l2;
  76. }
  77.  
  78. unsigned int Powtorzenia(ElementDrzewa* Korzen, double liczba)
  79. {
  80. unsigned int ile=0;
  81. if(Korzen->Lewy) ile+=Powtorzenia(Korzen->Lewy, liczba);
  82. if(Korzen->Prawy) ile+=Powtorzenia(Korzen->Prawy, liczba);
  83. if(Korzen->Liczba==liczba) ile++;
  84. return ile;
  85. }
  86.  
  87. unsigned int SzerokoscDrzewa(ElementDrzewa* Korzen, unsigned int Poziom)
  88. {
  89. unsigned int licz=0;
  90. if(Poziom>0)
  91. {
  92. Poziom--;
  93. if(Korzen->Lewy==NULL && Korzen->Prawy==NULL) return 0;
  94. if(Korzen->Lewy!=NULL)
  95. licz+=SzerokoscDrzewa(Korzen->Lewy, Poziom);
  96. if(Korzen->Prawy!=NULL)
  97. licz+=SzerokoscDrzewa(Korzen->Prawy, Poziom);
  98. }
  99. else if(Poziom==0) licz++;
  100. return licz;
  101. }
  102.  
  103. void Przedzial(ElementDrzewa* Korzen, double min, double max)
  104. {
  105. if((Korzen->Liczba>=min) && (Korzen->Liczba<=max))
  106. {
  107. printf("%lf\n", Korzen->Liczba);
  108. if(Korzen->Lewy) Przedzial(Korzen->Lewy, min, max);
  109. if(Korzen->Prawy) Przedzial(Korzen->Prawy, min, max);
  110. }
  111. if(Korzen->Liczba<min)
  112. if(Korzen->Prawy) Przedzial(Korzen->Prawy, min, max);
  113. if(Korzen->Liczba>max)
  114. if(Korzen->Lewy) Przedzial(Korzen->Lewy, min, max);
  115. }
  116.  
  117. int main()
  118. {
  119. /*srand(time(NULL));
  120. double tablica[5000];
  121. for(int i=0; i<2500 ;i++) tablica[i] = rand()%5000;
  122. for(int i=2500;i<5000;i++) tablica[i] = rand()%5000-5000;
  123. FILE *plik;
  124. plik=fopen("plik.txt", "w");
  125. int x=5000;
  126. fprintf(plik, "%d\n", x);
  127. for(int i=0; i<5000; i++)
  128. {
  129. fprintf(plik, "%lf\n", tablica[i]);
  130. }
  131. fclose(plik);*/
  132.  
  133. ElementDrzewa *Drzewo;
  134. Drzewo=WczytajDrzewo("plik.txt");
  135. printf("Glebokosc drzewa to: %u", GlebokoscDrzewa(Drzewo));
  136. printf("\nWystepujace powtorzenia liczby 460.000000: %u", Powtorzenia(Drzewo, 460.000000));
  137. printf("\nSzerokosc na poziomie 6 to %u", SzerokoscDrzewa(Drzewo, 6));
  138. printf("\nLiczby z przedzialu 0-20 to:\n"); Przedzial(Drzewo, 0, 20);
  139.  
  140. ZwolnijDrzewo(Drzewo);
  141. _getch();
  142. return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement