Code:
#include<conio.h>
#include<alloc.h>
#include<string.h>
struct node
{ int info;
struct node *l;
struct node *r;
} *node;
typedef struct node *pt;
void freenode( pt p)
{ freenode(p);}
pt getnode()
{ pt p;
p=(struct node*)malloc(sizeof(struct node));
return(p);
}
void ini (pt *proot)
{ proot=NULL;}
pt makenode(int item)
{ pt p=getnode();
p->info=item;
p->l=NULL;
p->r=NULL;
return (p);
}
void makeleft (pt p,int item)
{ if(p==NULL)
printf("node p is NULL");
else
{ if(p->l==NULL)
p->l=makenode(item);
else
printf("node p has left son node");
}
}
void makeright(pt p,int item)
{ if(p==NULL)
printf("node p is NULL");
else
{ if(p->r==NULL)
p->r=makenode(item);
else
printf("node p has right son node");
}
}
void insert(pt proot, int item)
{ pt p;
if(proot==NULL)
{ p=getnode();
p->info=item;
p->l=NULL;
p->r=NULL;
proot=p;
}
else
{ if(item==p->info)
printf("it has already");
else if (p->info>item)
insert(p->l,item);
else
insert(p->r,item);
}
}
pt del(pt p)
{ pt px,f;
if (p==NULL)
return (NULL);
else if (p->l==NULL&&p->r==NULL)
px=NULL;
else if(p->l==NULL)
px=p->r;
else if(p->r==NULL)
px=p->l;
else
{ f=p;
px=p->r;
while(p->l!=NULL)
{ f=px;
px=px->l;
}
}
if(f!=p)
{ f->l=px->r;
px->r;p->r;
px->l;p->l;
}
else
px->l=p->l;
freenode(p);
return(px);
}
void display (pt *tree,int level)
{ int i;
if(tree!=NULL)
{ display(tree->r,level+1);
printf("\n");
for (i=0;i<level;i++)
printf(" ");
printf("%d",tree->item);
display(tree->l,level+1);
}
}
Comment