删除数据
struct Person* removes(struct Person* p,struct Person*data,int(*compare)(struct Person* p1,struct Person* p2))
{
if (NULL == p)
{
return p;
}
if (NULL == data)
{
return p;
}
int m = compare(p, data);
if (m<0)
{
p->rchild=removes(p->rchild,data,compare);
return p;
}else
if (m > 0)
{
p->lchild= removes(p->lchild, data, compare);
return p;
}else
if (m == 0)
{
struct Person* myPrient = p->parent;
if (NULL == p->lchild)
{
if (p->rchild)
{
p->rchild->parent = myPrient;
}
return p->rchild;
}
if (NULL == p->rchild)
{
if (p->lchild)
{
p->lchild->parent = myPrient;
}
return p->lchild;
}
int l = getHight(p->lchild);
int r = getHight(p->rchild);
struct Person* pCurror;
if (l >= r)
{
pCurror = p->lchild;
如果指针指向节点的右节点为空
if (NULL == pCurror->rchild)
{
pCurror->parent = myPrient;
pCurror->rchild = p->rchild;
pCurror->rchild->parent = pCurror;
return pCurror;
}
while (pCurror->rchild)
{
pCurror = pCurror->rchild;
}
p->parent = pCurror->parent;
pCurror->parent = myPrient;
struct Person* oldpc = pCurror->lchild;
pCurror->rchild = p->rchild;
pCurror->rchild->parent = pCurror;
pCurror->lchild = p->lchild;
pCurror->lchild->parent = pCurror;
p->parent->rchild = oldpc;
if (oldpc)
{
oldpc->parent = p->parent;
}
return pCurror;
}
else {
pCurror = p->rchild;
if (NULL == pCurror->lchild)
{
pCurror->parent = myPrient;
pCurror->lchild = p->lchild;
pCurror->lchild->parent = pCurror;
return pCurror;
}
while (pCurror->lchild)
{
pCurror = pCurror->lchild;
}
p->parent = pCurror->parent;
pCurror->parent = myPrient;
struct Person* newrr = pCurror->rchild;
pCurror->rchild = p->rchild;
pCurror->rchild->parent = pCurror;
pCurror->lchild = p->lchild;
pCurror->lchild->parent = pCurror;
p->parent->lchild = newrr;
if (newrr)
{
newrr->parent = p->parent;
}
return pCurror;
}
}
return p;
}
void removes_tree(struct ABLtree* tree,struct Person* data,int(*compare)(struct Person* d1,struct Person* d2))
{
if (NULL == tree)
{
return;
}
if (NULL == data)
{
return;
}
if (NULL == compare)
{
return;
}
mytree->root=removes(tree->root, data, compare);
mytree->root=Adjtree(tree->root);
}
遍历数据
void myprint(struct Person* p)
{
if (!p)
{
return;
}
myprint(p->lchild);
printf("%d \n", p->m);
myprint(p->rchild);
}
void foregr(struct ABLtree* tree,void(*myprint)(struct Person* p))
{
if (!tree)
{
return;
}
myprint(tree->root);
}
销毁
if(tree)
{
free(tree);
}