Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- struct element
- {
- int k;
- struct element *p;
- struct element *left;
- struct element *right;
- };
- struct element *bst_wstaw(struct element *t, struct element *z);
- void inorder(struct element *x);
- struct element *bst_szukaj(struct element *t, int kl);
- struct element *bst_min(struct element *t);
- struct element *bst_max(struct element *t);
- struct element *bst_poprzednik(struct element *t);
- struct element *bst_nastepnik(struct element *t);
- struct element *bst_usun(struct element *t, struct element *z);
- struct element *bst_zwolnij(struct element *t);
- main()
- {
- struct element *t=NULL, *el=NULL;
- char z;
- int liczba;
- while(1)
- {
- el=NULL;
- printf("\nco chcesz zrobic?");
- printf("\nd - dodac");
- printf("\ns - szukac");
- printf("\nu - usunac");
- printf("\nw - wyswietlic");
- printf("\nz - zwolnic");
- printf("\nq - wyjsc\n");
- fflush(stdin);
- z=getchar();
- switch(z)
- {
- case 'd':
- el=(struct element*)malloc(sizeof(struct element));
- printf("\npodaj wartosc elementu do wstawienia: ");
- scanf("%d",&liczba);
- el->k=liczba;
- el->left=NULL;
- el->right=NULL;
- t=bst_wstaw(t,el);
- break;
- case 'w': inorder(t);
- break;
- case 'q': return 0;
- case 's':
- if (t==NULL) printf("\ndrzewo puste ");
- else
- {printf("\npodaj wartosc do wyszukania: ");
- scanf("%d",&liczba);
- el=bst_szukaj(t,liczba);
- if (el==NULL) printf("\nnie znaleziono wartosci");
- else printf("\nwartosc znaleziono pod adresem %p",el);}
- break;
- case 'u':
- if (t==NULL) printf("\ndrzewo puste ");
- else
- {printf("\npodaj wartosc do usuniecia: ");
- scanf("%d",&liczba);
- el=bst_szukaj(t,liczba);
- if (el==NULL) printf("\nnie znaleziono elementu w drzewie");
- else {t=bst_usun(t,el);
- printf("\npoprawnie usunieto");}}
- break;
- case 'z':
- t=bst_zwolnij(t);
- break;
- }
- }
- }
- struct element *bst_wstaw(struct element *t, struct element *z)
- {
- struct element *y=NULL,*x=NULL;
- x=t;
- while(x!=NULL)
- {
- y=x;
- if (z->k<x->k) x=x->left;
- else x=x->right;
- }
- z->p=y;
- if (y==NULL) t=z;
- else
- {
- if (z->k < y->k) y->left=z;
- else y->right=z;
- }
- return t;
- }
- void inorder(struct element *x)
- {
- if (x!=NULL)
- {
- inorder(x->left);
- printf("%d ",x->k);
- inorder(x->right);
- }
- }
- struct element *bst_szukaj(struct element *t, int kl)
- {
- while(t!=NULL && kl!=t->k)
- {
- if (kl<t->k) t=t->left;
- else t=t->right;
- }
- return t;
- }
- struct element *bst_min(struct element *t)
- {
- while (t->left!=NULL)
- {
- t=t->left;
- }
- return t;
- }
- struct element *bst_max(struct element *t)
- {
- while (t->right!=NULL)
- {
- t=t->right;
- }
- return t;
- }
- struct element *bst_poprzednik(struct element *t)
- {
- struct element *y=NULL;
- if (t->left!=NULL) return bst_max(t->left);
- y=t->p;
- while (y!=NULL && t==y->left)
- {
- t=y;
- y=y->p;
- }
- return y;
- }
- struct element *bst_nastepnik(struct element *t)
- {
- struct element *y=NULL;
- if (t->right!=NULL) return bst_min(t->right);
- y=t->p;
- while (y!=NULL && t==y->right)
- {
- t=y;
- y=y->p;
- }
- return y;
- }
- struct element *bst_usun(struct element *t, struct element *z)
- {
- struct element *y=NULL,*x=NULL;
- int a;
- if (z->left==NULL || z->right==NULL) y=z;
- else y=bst_nastepnik(z);
- if (y->left!=NULL) x=y->left;
- else x=y->right;
- if (x!=NULL) x->p=y->p;
- if (y->p==NULL) t=x;
- else
- {
- if (y==y->p->left) y->p->left=x;
- else y->p->right=x;
- }
- if (y!=z)
- {
- a=y->k;
- y->k=z->k;
- z->k=a;
- }
- free(y);
- return t;
- }
- struct element *bst_zwolnij(struct element *t)
- {
- while(t!=NULL)
- {
- t=bst_usun(t,t);
- }
- return t;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement