1.被删除节点没有子树的情况,直接删除,并修改对应父节点的指针为空。
2.对于只有一个子树的情况,考虑将其子树作为其父节点的子树,关于是左还是右,根据被删除的节点确定。
3.最复杂的是有两个子数的情况,可以考虑两种方法,都是同样的思想:用被删除节点A的左子树的最右节点或者A的右子树的最左节点作为替代A的节点,并修改相应的最左或最右节点的父节点的指针,修改方法类似2 ,不做细致讨论
/*************二叉树的节点的删除***********/
/*********该程序描述了从递归建立二叉树到查找*********/
/*********以及删除的全过程,也算是对前面几个*********/
/*********程序的联习与总结***************************/
#include<>
#include<>
#define Maxsize 16
//定义二叉树节点
struct treenode
{
int data;
struct treenode* left;
struct treenode* right;
};
typedef treenode Node;
typedef Node* btree;
//递归建立二叉树
btree create_btree(int* nodelist,int postion)
{
btree newnode;
if(nodelist[postion]==0||postion>=Maxsize)
return NULL;
else
{
newnode=(btree) malloc(sizeof(Node));
newnode->data=nodelist[postion];
newnode->left=create_btree(nodelist,postion*2);
newnode->right=create_btree(nodelist,postion*2+1);
return newnode;
}
}
/********查找二叉树的节点********/
btree search(btree root,int node)
{
btree point;
point=root;
if(point==NULL)
return root;
else
if(point->data==node)
return point;
else
if(point->data>node)
search(point->left,node);
else
search(point->right,node);
}
/********第二种查找方法,记录其父节点的值********/
btree binary_search(btree point,int node,int *postion)
{
btree parent;
parent=point;
*postion=0;
while(point!=NULL)
{
if(point->data==node)
return parent;
else
{
parent=point;
if(point->data>node)
{
point=point->left;
*postion=-1;
}
else
{
point=point->right;
*postion=1;
}
}
}
return NULL;
}
/**********删除二叉树节点的操作***********/
btree deletenode(btree root,int node)
{
btree parent;
btree point;
btree child;
int postion;
parent=binary_search(root,node,&postion);
//二叉树为空的情况
if(parent==NULL)
return root;
else
{
switch(postion)
{ case -1:point=parent->left;break;
case 1 :point=parent->right;break;
case 0 :point=parent;break;
}
if(point->left==NULL&&point->right==NULL)
{
switch(postion)
{
case -1:parent->left=NULL;break;
case 1:parent->right=NULL;break;
case 0:parent=NULL;break;
}
free(point);
return root;
}
if(point->left==NULL&&point->right!=NULL)
{
if(postion==-1)
parent->left=point->right;
else
if(postion==1)
parent->right=point->right;
else
root=root->right;
free(point);
return root;
}
if(point->left!=NULL&&point->right==NULL)
{
if(postion==-1)
parent->left=point->left;
else
if(postion==1)
parent->right=point->left;
else
root=root->left;
return root;
}
if(point->left!=NULL&& point->right!=NULL)
{
parent=point;
child=point->left;
while(child->right!=NULL)
{
parent=child;
child=child->right;
}
point->data=child->data;
if(parent->left=child)
parent->left=child->left;
else
parent->right=child->left;
free(child);
return root;
}
}
}
/*********中序遍历二叉树*************/
void Inorder(btree point)
{
if(point!=NULL)
{
Inorder(point->left);
printf("[%2d]",point->data);
Inorder(point->right);
}
}
/*********主程序测试函数***********/
void main()
{
btree root=NULL;
int deletetree;
int nodelist[16]={0,5,4,6,2,0,0,8,1,3,0,0,0,0,7,9};
root=create_btree(nodelist,1);
printf("/n the original tree is");
Inorder(root);
printf("/n");
printf("Input the value of the number /n");
int test;
scanf("%d",&test);
root=deletenode(root,test);
printf("/n the deleted tree is /n");
Inorder(root);
printf("/n");
}