Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <conio.h>
- #include <stdio.h>
- #include <malloc.h>
- #include <stdlib.h>
- #include <time.h>
- struct ElementDrzewa
- {
- double Liczba;
- ElementDrzewa *Lewy, *Prawy;
- };
- ElementDrzewa* NowyElement(double liczba)
- {
- ElementDrzewa *Nowy=(ElementDrzewa*)malloc(sizeof(ElementDrzewa));
- Nowy->Liczba=liczba;
- Nowy->Lewy=Nowy->Prawy=NULL;
- return Nowy;
- }
- void ZwolnijDrzewo(ElementDrzewa* Korzen)
- {
- if(Korzen->Lewy) ZwolnijDrzewo(Korzen->Lewy);
- if(Korzen->Prawy) ZwolnijDrzewo(Korzen->Prawy);
- free(Korzen);
- }
- void WstawElement(ElementDrzewa* Korzen, double liczba)
- {
- for(;;)
- {
- if(liczba<Korzen->Liczba)
- {
- if(Korzen->Lewy) Korzen=Korzen->Lewy;
- else { Korzen->Lewy=NowyElement(liczba); break; }
- }
- else
- {
- if(Korzen->Prawy) Korzen=Korzen->Prawy;
- else { Korzen->Prawy=NowyElement(liczba); break; }
- }
- }
- }
- ElementDrzewa* WczytajDrzewo(char *Nazwapliku)
- {
- unsigned int ilosc;
- double liczba;
- ElementDrzewa *Drzewo=0;
- FILE *plik;
- plik=fopen(Nazwapliku, "r");
- if(plik)
- {
- fscanf(plik, "%u", &ilosc);
- fscanf(plik, "%lf", &liczba);
- Drzewo=NowyElement(liczba);
- for(int i=1; i<ilosc; i++)
- {
- fscanf(plik, "%lf", &liczba);
- WstawElement(Drzewo, liczba);
- }
- fclose(plik);
- }
- else printf("Blad wczytywania pliku!");
- return Drzewo;
- }
- unsigned int GlebokoscDrzewa(ElementDrzewa* Korzen)
- {
- unsigned int l1=0, l2=0;
- if(Korzen->Lewy) l1=GlebokoscDrzewa(Korzen->Lewy);
- if(Korzen->Prawy) l2=GlebokoscDrzewa(Korzen->Prawy);
- if(l1>l2) return ++l1;
- else return ++l2;
- }
- unsigned int Powtorzenia(ElementDrzewa* Korzen, double liczba)
- {
- unsigned int ile=0;
- if(Korzen->Lewy) ile+=Powtorzenia(Korzen->Lewy, liczba);
- if(Korzen->Prawy) ile+=Powtorzenia(Korzen->Prawy, liczba);
- if(Korzen->Liczba==liczba) ile++;
- return ile;
- }
- unsigned int SzerokoscDrzewa(ElementDrzewa* Korzen, unsigned int Poziom)
- {
- unsigned int licz=0;
- if(Poziom>0)
- {
- Poziom--;
- if(Korzen->Lewy==NULL && Korzen->Prawy==NULL) return 0;
- if(Korzen->Lewy!=NULL)
- licz+=SzerokoscDrzewa(Korzen->Lewy, Poziom);
- if(Korzen->Prawy!=NULL)
- licz+=SzerokoscDrzewa(Korzen->Prawy, Poziom);
- }
- else if(Poziom==0) licz++;
- return licz;
- }
- void Przedzial(ElementDrzewa* Korzen, double min, double max)
- {
- if((Korzen->Liczba>=min) && (Korzen->Liczba<=max))
- {
- printf("%lf\n", Korzen->Liczba);
- if(Korzen->Lewy) Przedzial(Korzen->Lewy, min, max);
- if(Korzen->Prawy) Przedzial(Korzen->Prawy, min, max);
- }
- if(Korzen->Liczba<min)
- if(Korzen->Prawy) Przedzial(Korzen->Prawy, min, max);
- if(Korzen->Liczba>max)
- if(Korzen->Lewy) Przedzial(Korzen->Lewy, min, max);
- }
- int main()
- {
- /*srand(time(NULL));
- double tablica[5000];
- for(int i=0; i<2500 ;i++) tablica[i] = rand()%5000;
- for(int i=2500;i<5000;i++) tablica[i] = rand()%5000-5000;
- FILE *plik;
- plik=fopen("plik.txt", "w");
- int x=5000;
- fprintf(plik, "%d\n", x);
- for(int i=0; i<5000; i++)
- {
- fprintf(plik, "%lf\n", tablica[i]);
- }
- fclose(plik);*/
- ElementDrzewa *Drzewo;
- Drzewo=WczytajDrzewo("plik.txt");
- printf("Glebokosc drzewa to: %u", GlebokoscDrzewa(Drzewo));
- printf("\nWystepujace powtorzenia liczby 460.000000: %u", Powtorzenia(Drzewo, 460.000000));
- printf("\nSzerokosc na poziomie 6 to %u", SzerokoscDrzewa(Drzewo, 6));
- printf("\nLiczby z przedzialu 0-20 to:\n"); Przedzial(Drzewo, 0, 20);
- ZwolnijDrzewo(Drzewo);
- _getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement