Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct wezel{
- struct wezel *ojciec;
- struct wezel *lewy;
- struct wezel *prawy;
- int klucz;
- };
- char tab[100];
- int tab_iterator = 0;
- struct wezel *dodawanie(struct wezel *glowa,struct wezel *nowy){
- struct wezel *pomoc=glowa;
- if(nowy->klucz < pomoc->klucz){
- if(pomoc->lewy==NULL){
- pomoc->lewy=nowy;
- nowy->ojciec=pomoc;
- }else dodawanie(pomoc->lewy, nowy);
- }
- if(nowy->klucz>pomoc->klucz){
- if(pomoc->prawy==NULL){
- pomoc->prawy=nowy;
- nowy->ojciec=pomoc;
- }else dodawanie(pomoc->prawy,nowy);
- }
- return glowa;
- };
- void wypisywanie(struct wezel *glowa, int szukany){
- struct wezel *pomoc = glowa;
- if(glowa == NULL){
- tab[0] = 'A';
- }
- if(pomoc->klucz == szukany){
- tab[tab_iterator] = pomoc->klucz;
- return;
- }else if(pomoc->klucz > szukany && pomoc->lewy != NULL){
- tab[tab_iterator] = pomoc->klucz;
- tab_iterator++;
- wypisywanie(pomoc->lewy, szukany);
- }else if(pomoc->klucz < szukany && pomoc->prawy != NULL){
- tab[tab_iterator] = pomoc->klucz;
- tab_iterator++;
- wypisywanie(pomoc->prawy, szukany);
- }
- }
- int main() {
- struct wezel *glowa=NULL;
- int dzialanie;
- int wartosc;
- do{
- scanf("%d", &dzialanie);
- if (dzialanie == 0) {
- return 0;
- } else if (dzialanie == 1) {
- struct wezel *nowy;
- nowy = (struct wezel *) malloc(sizeof(struct wezel));
- scanf("%d", &wartosc);
- nowy->klucz = wartosc;
- nowy->lewy = NULL;
- nowy->prawy = NULL;
- nowy->ojciec=NULL;
- if (glowa == NULL) {
- glowa = nowy;
- }else {
- glowa = dodawanie(glowa, nowy);
- }
- } else if(dzialanie==2){
- int szukany;
- scanf("%d", &szukany);
- wypisywanie(glowa, szukany);
- if(tab[tab_iterator] != szukany){
- printf("A");
- }else{
- for(int i = 0; i <= tab_iterator; i++){
- printf("%d", (int)tab[i]);
- }
- }
- }
- }while(dzialanie!=0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment