Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 5.2.File abr.h
- #include<stdlib.h>
- #define ALBEROVUOTO NULL
- typedef short boolean;
- typedef struct nodoABR {
- tipobaseABR info;
- struct nodoABR * leftchild , * rightchild;
- } * abr;
- boolean EmptyABR(abr n){
- return(n==ALBEROVUOTO);
- }
- boolean FullABR(abr n){
- return(0);
- }
- abr LeftChild(abr n){
- return(n->leftchild);
- }
- abr RightChild(abr n){
- return(n->rightchild);
- }
- tipobaseABR Label(abr n){
- return(n->info);
- }
- void MakeNullABR(abr *p){
- *p=ALBEROVUOTO;
- }
- boolean Member(abr n, tipobaseABR x){
- if (EmptyABR(n)) return(0);
- if (!Confronta(n->info,x)) return(1);
- if (Confronta(n->info,x)>0) return(Member(n->leftchild,x));
- else return(Member(n->rightchild,x));
- }
- void InsertABR(abr *p, tipobaseABR x){
- if (!FullABR(*p)){
- if (EmptyABR(*p)) {
- *p=(struct nodoABR *)malloc(sizeof(struct nodoABR));
- (*p)->info=x;
- (*p)->leftchild=(*p)->rightchild=ALBEROVUOTO;
- } else if (Confronta((*p)->info,x)>0) InsertABR(&(*p)->leftchild,x);
- else if (Confronta((*p)->info,x)<0) InsertABR(&(*p)->rightchild,x);
- }
- }
- abr DelMin(abr *p){
- abr pmin;
- if ((*p)->leftchild==ALBEROVUOTO) {
- pmin=*p;
- *p=(*p)->rightchild;
- return(pmin);
- } else return(DelMin(&(*p)->leftchild));
- }
- void DeleteABR(abr *p, tipobaseABR x){
- abr tmp;
- if (!EmptyABR(*p)) {
- if (!Confronta((*p)->info,x)) {
- tmp=*p;
- if (EmptyABR((*p)->leftchild)) {
- *p=(*p)->rightchild;
- free (tmp);
- } else if (EmptyABR((*p)->rightchild)) {
- *p=(*p)->leftchild;
- free (tmp);
- } else {
- *p=DelMin(&(*p)->rightchild);
- (*p)->leftchild=tmp->leftchild;
- (*p)->rightchild=tmp->rightchild;
- free (tmp);
- }
- } else if (Confronta((*p)->info,x)>0) DeleteABR(&(*p)->leftchild,x);
- else DeleteABR(&(*p)->rightchild,x);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment