
Объясните, пожалуйста, как правильно написать код. Я никак не могу разобраться с этими указателями...
Думаю, здесь куча ошибок. Подскажите верное решение задачи. Необходимо реализовать бинарное дерево с помощью указателя.
Код: Выделить всё
#include <stdio.h>
#include <ctype.h>
#define LAMBDA NULL
typedef char labeltype;
typedef struct cell_tag {
labeltype label;
struct cell_tag *leftchild;
struct cell_tag *rightchild;
struct cell_tag *parent;
} celltype;
typedef celltype *node;
typedef celltype *BTREE;
void MAKE_NULL (BTREE *T) {
*T = NULL;
}
void CREATE (labeltype l, BTREE TL, BTREE TR, BTREE *T) {
*T = (celltype*) malloc (sizeof (celltype));
(*T)->label = l;
(*T)->parent = NULL;
(*T)->leftchild = TL;
(*T)->rightchild = TR;
}
void LEFT_SUBTREE (BTREE T, BTREE *TL) {
if (T->leftchild == NULL)
*TL = NULL;
*TL = (celltype*) malloc (sizeof (celltype));
(*TL)->label = T->leftchild->label;
(*TL)->leftchild = T->leftchild->leftchild;
(*TL)->rightchild = T->leftchild->rightchild;
(*TL)->parent = T;
}
void RIGHT_SUBTREE (BTREE T, BTREE *TR) {
if (T->rightchild == NULL)
*TR = NULL;
*TR = (celltype*) malloc (sizeof (celltype));
(*TR)->label = T->rightchild->label;
(*TR)->leftchild = T->rightchild->leftchild;
(*TR)->rightchild = T->rightchild->rightchild;
(*TR)->parent = T;
}
node INSERT_LEFT_CHILD(labeltype l, node i, BTREE *T){
node p;
p=malloc(sizeof(celltype));
p->label=l;
p->rightchild=NULL;
p->leftchild=NULL;
p->parent=i;
i->leftchild=p;
return (p);
}
node INSERT_RIGHT_CHILD(labeltype l, node i, BTREE *T){
node p;
p=malloc(sizeof(celltype));
p->label=l;
p->rightchild=NULL;
p->leftchild=NULL;
p->parent=i;
i->rightchild=p;
return(p);
}
void DELETE (node i, BTREE *T) {
node p = NULL;
if (i->leftchild != NULL || i->rightchild != NULL) {
printf("nije list");
}
else
free (i);
}
int EMPTY (BTREE T) {
if (T == NULL)
return 1;
return 0;
}
node ROOT(BTREE T) {
if(EMPTY(T)) return LAMBDA;
return(T);
}
node LEFT_CHILD (node i, BTREE T) {
if (i->rightchild == NULL)
return LAMBDA;
return i->leftchild;
}
node RIGHT_CHILD (node i, BTREE T) {
if (i->rightchild == NULL)
return LAMBDA;
return i->rightchild;
}
node PARENT (node i, BTREE T) {
return i->parent;
}
labeltype LABEL (node i, BTREE T) {
return i->label;
}
void CHANGE_LABEL (labeltype l, node i, BTREE *T) {
i->label = l;
}
Заранее спасибо!