线索二叉树看看为什么不能删除头节点???
Bitree DeleteNode(Bitree t){
Bitree child,pre;
char ch;
printf("which data you want delete:");
ch=getchar();
ch=getchar();
child=SearchChild(t,ch);
printf("find child:%c\n",child->data);
pre=SearchPre(t,child);
printf("find pre:%c\n",pre->data);
if(NULL==child)
{
printf("I have not find this data: (T_T)");
return(t);
}
if(child==pre->lchild ) /*当结点为前驱的左孩子*/
{
if(1==child->ltag&&1==child->rtag) /*ye zi jie dian*/
{
pre->lchild=child->lchild;
pre->ltag=1;
free(child);
}
else if(1!=child->ltag&&1==child->rtag)/*当结点有左孩子,没有右孩子时*/
{
pre->lchild=child->lchild;
free(child);
}
else if(1==child->ltag&&1!=child->rtag)/*当结点有右孩子,没有左孩子时*/
{
pre->lchild=child->rchild;
free(child);
}
else if(1!=child->ltag&&1!=child->rtag) /*当结点既有左孩子又有右孩子时*/
{
pre->lchild=child->lchild;
child->lchild->rchild=child->rchild;
child->lchild->rtag=0;
free(child);
}
}
if(child==pre->rchild) /*当结点为前驱的有孩子*/
{
if(1==child->ltag&&1==child->rtag) /*ye zi jie dian*/
{
pre->rchild=child->rchild;
pre->rtag=1;
free(child);
}
else if(1!=child->ltag&&1==child->rtag)/*当结点有左孩子没有右孩子*/
{
pre->rchild=child->lchild;
free(child);
}
else if(1==child->ltag&&1!=child->rtag)/*当结点没左孩子又右孩子*/
{
pre->rchild=child->rchild;
free(child);
}
else if(1!=child->ltag&&1!=child->rtag)/*当结点既有左孩子又有右孩子*/
{
pre->rchild=child->rchild;
child->rchild->lchild=child->lchild;
child->rchild->ltag=0;
free(child);
}
}
printf("\n\tall right\n\n");
printf("pre :");Preorder(T);printf("\n");//先需遍历
printf(" in :");Inorder(T);printf("\n");//中序遍历
printf("post:");Postorder(T);//后序
}