Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- struct element_drzewa
- {
- int klucz;
- char wart;
- struct element_drzewa *lewy_potomek, *prawy_potomek;
- }* lisc;
- void dodaj_element(struct element_drzewa **lisc, int klucz, char wart)
- {
- while(*lisc!=NULL)
- {
- if((*lisc)->klucz >= klucz)
- lisc = &(*lisc)->lewy_potomek;
- else
- lisc = &(*lisc)->prawy_potomek;
- }
- *lisc = (struct element_drzewa *)malloc(sizeof(struct element_drzewa));
- if(*lisc)
- {
- (*lisc)->klucz = klucz;
- (*lisc)->wart = wart;
- (*lisc)->lewy_potomek = (*lisc)->prawy_potomek = NULL;
- }
- }
- void postorder(struct element_drzewa *lisc)
- {
- if(lisc)
- {
- postorder(lisc->lewy_potomek);
- postorder(lisc->prawy_potomek);
- printf("klucz: %d , wartosc: %c\n", lisc->klucz, lisc->wart);
- }
- }
- void preorder(struct element_drzewa *lisc)
- {
- if(lisc)
- {
- printf("klucz: %d , wartosc: %c\n", lisc->klucz, lisc->wart);
- preorder(lisc->lewy_potomek);
- preorder(lisc->prawy_potomek);
- }
- }
- void inorder(struct element_drzewa *lisc)
- {
- if(lisc)
- {
- inorder(lisc->lewy_potomek);
- printf("klucz: %d , wartosc: %c\n", lisc->klucz, lisc->wart);
- inorder(lisc->prawy_potomek);
- }
- }
- int main()
- {
- srand(time(NULL));
- int i;
- for(i=0;i<10;i++)
- {
- int klucz=0;
- char wart=0;
- wart = 'a'+rand()%('z'-'a'+1);
- klucz = (int) wart;
- dodaj_element(&lisc,klucz,wart);
- }
- puts("wyswietlanie preorder:");
- preorder(lisc);
- puts("wyswietlania postorder:");
- postorder(lisc);
- puts("wyswietlanie inorder:");
- inorder(lisc);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement