I'm supposed to do a dictionary impplementation using a binary tree that's shown by pointers, this is what I did:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef char elementtype[30];
typedef struct cell_tag{
elementtype element;
struct cell_tag *leftchild;
struct cell_tag *rightchild;
} celltype;
typedef celltype *DICTIONARY;
void MAKE_NULL(DICTIONARY *Ap){
*Ap=NULL;
return;
}
int MEMBER (elementtype x, DICTIONARY A){
if (A == NULL) return 0;
else if (strcmp(x, A->element)==0) return 1;
else if (strcmp(x, A->element)<0) return (MEMBER(x,A->leftchild));
else return (MEMBER(x,A->rightchild));
}
void INSERT (elementtype x, DICTIONARY *Ap){
if (*Ap == NULL){
*Ap = (celltype*)malloc(sizeof(celltype));
strcpy((*Ap)->element, x);
(*Ap)->leftchild = (*Ap)->rightchild = NULL;
}
else if (strcmp(x, (*Ap)->element)<0) INSERT(x, &((*Ap)->leftchild));
else if (strcmp(x, (*Ap)->element)>0) INSERT(x, &((*Ap)->rightchild));
}
elementtype DELETE_MIN(DICTIONARY *Ap){
celltype *temp;
elementtype minel;
if (((*Ap)->leftchild) == NULL){
strcpy(minel, (*Ap)->element);
temp = (*Ap);
(*Ap) = (*Ap)->rightchild;
free(temp);
}
else strcpy(minel, DELETE_MIN(&((*Ap)->leftchild)));
return minel;
}
void DELETE (elementtype x, DICTIONARY *Ap){
celltype *temp;
if (*Ap != NULL)
if (strcmp(x, (*Ap)->element)<0)
DELETE(x, &((*Ap)->leftchild));
else if (strcmp(x, (*Ap)->element)>0)
DELETE(x, &((*Ap)->rightchild));
else if( ((*Ap)->leftchild==NULL) && ((*Ap)->rightchild == NULL)){
free(*Ap);
*Ap=NULL;
}
else if( (*Ap)->leftchild == NULL){
temp=*Ap;
*Ap=(*Ap)->rightchild;
free(temp);
}
else strcpy((*Ap)->element, DELETE_MIN( &((*Ap)->rightchild)));
}
this doesn't work, the problem is in the DELETE_MIN function, but I don't know how to fix it...Any help at all would be great...