我做了一棵二叉树。它插入元素,删除它们并打印输出。代码中的删除功能有些问题。删除一些元素后会给出分段错误。请帮我找到错误删除代码。
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int info;
struct Node *right;
struct Node *left;
struct Node *parent;
};
typedef struct Node node;
node *createnode(node *temp,int number)
{
temp=(node*)malloc(sizeof(node));
temp->info=number;
temp->right=NULL;
temp->left=NULL;
temp->parent=NULL;
return temp;
}
node *insert(node *head,int number)
{
node *temp;
temp=createnode(temp,number);
node *traverse=head;
if(head==NULL)
{
head=temp;
temp->parent=NULL;
return head;
}
else
{
while(traverse!=NULL)
{
if(number < traverse->info)
{
if(traverse->left==NULL)
{
traverse->left=temp;
temp->parent=traverse;
break;
}
traverse=traverse->left;
}
else if(number>traverse->info)
{
if(traverse->right==NULL)
{
traverse->right=temp;
temp->parent=traverse;
break;
}
traverse=traverse->right;
}
else
break;
}
}
return head;
}
node *find(node *head,int number)
{
node *traverse=head;
if(traverse==NULL)
return NULL;
else
{
while(traverse!=NULL)
{
if(number<traverse->info)
{
if(traverse->left==NULL)
{
printf("Number not found\n");
return NULL;
}
traverse=traverse->left;
}
else if(number>traverse->info)
{
if(traverse->right==NULL)
{
printf("Number not found\n");
return NULL;
}
traverse=traverse->right;
}
else
break;
}
}
return traverse;
}
int find_children(node *temp)
{
int count=0;
if(temp->left!=NULL)
count+=1;
if(temp->right!=NULL)
count+=1;
return count;
}
node *delete(node *head,int number)
{
int children;
node *temp=head;
temp=find(temp,number);
if(temp==NULL)
return head;
else
{
children=find_children(temp);
if(children==0)
{
if(temp->parent->left==temp)
{
temp->parent->left=NULL;
}
else if(temp->parent->right==temp)
{
temp->parent->right=NULL;
}
}
if(children==1)
{
if(temp->parent==NULL)
{
if(temp->right!=NULL)
{
temp->right->parent=NULL;
head=temp->right;
}
else if(temp->left!=NULL)
{
temp->left->parent=NULL;
head=temp->left;
}
}
else
{
if(temp->parent->left==temp)
{
if(temp->right!=NULL)
{
temp->right->parent=temp->parent;
temp->parent->left=temp->right;
}
else if(temp->left!=NULL)
{
temp->left->parent=temp->parent;
temp->parent->left=temp->left;
}
}
if(temp->parent->right==temp)
{
if(temp->right!=NULL)
{
temp->right->parent=temp->parent;
temp->parent->right=temp->right;
}
else if(temp->left!=NULL)
{
temp->left->parent=temp->parent;
temp->parent->right=temp->left;
}
}
}
}
if(children==2)
{
node *temp1;
node *temp2;
temp1=temp;
temp2=temp->right;
while(temp2->left!=NULL)
{
temp1=temp2;
temp2=temp2->left;
}
temp->info=temp2->info;
if(temp1->left==temp2)
{
temp1->left=temp2->left;
}
else
{
temp1->right=temp2->right;
}
}
}
return head;
}
void printing(node *head)
{
if(head==NULL)
return ;
printing(head->left);
printf("%d ",head->info);
printing(head->right);
}
int main()
{
node *head;
int number,choice;
printf("Choose one of the option\n");
while(1)
{
printf("\n1.Enter a element\n2.Delete an element\n3.print tree\n4Reverse the tree\n5.Sort\n6.Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
scanf("%d",&number);
head=insert(head,number);
break;
}
case 2:
{
scanf("%d",&number);
head=delete(head,number);
break;
}
case 3:
{
printing(head);
break;
}
/* case 4:
{
reverse();
}
case 5:
{
sort();
break;
}*/
case 6:
{
exit(0);
break;
}
}
}
return 0;
}
答案 0 :(得分:0)
删除BST中的元素(二进制搜索树):
要删除二进制搜索树中的元素,我们首先需要查看它的子元素,并根据该元素确定删除节点的方法。基本上,删除节点有三种奇怪的情况。
节点没有子节点(换句话说,它是叶子节点)。 该节点具有左或右子节点。 该节点有两个孩子。
public object DeleteNode (object data)
{
TNode tempDelete = this.GetNode(data);
if (tempDelete != null)
{
if ((tempDelete.Left == null ) &&(tempDelete.Right == null)) //Its a Leaf node
{
tempParent = tempDelete.Parent;
if(tempDelete == tempParent.Left) //Justremove by making it null
tempParent.Left = null;
else
tempParent.Right = null;
}
else if ((tempDelete.Left == null ) ||(tempDelete.Right == null)) //It has either Left orRight child
{
tempChild = tempDelete.Left == null? tempDelete.Right : tempDelete.Left; //Get the child
tempParent = tempDelete.Parent; //Getthe parent
if(tempDelete == tempParent.Left) //Makeparent points to it's child so it will automatically deleted like Linked list
tempParent.Left = tempChild;
else
tempParent.Right = tempChild;
}
else if ((tempDelete.Left != null) ||(tempDelete.Right != null)) //It has both Left andRight child
{
TNodepredNode = this.GetNode(this.TreePredecessor_Ite(data)); //Findit's predecessor
if(predNode.Left != null) // Predecessor node canhave no or left child. Do below two steps only if it has left child
{
tempChild = predNode.Left;
predNode.Parent.Right = tempChild; //Assignleft child of predecessor to it's Parent's right.
}
tempDelete.Data = predNode.Data; //Replace the value of predecessor nodeto the value of to be deleted node
//predNode = null; //Remove predecessornode as it's no longer required.
}
return data + " Deleted";
}
else
return "Please enter the valid tree element!";
}
答案 1 :(得分:0)
您的情况是删除树中没有子节点的最后一个节点 - 并且没有父节点:
if (children==0)
{
if (temp->parent->left == temp)
{
temp->parent->left=NULL;
}
else if (temp->parent->right == temp)
{
temp->parent->right=NULL;
}
}
当您取消引用temp->parent
NULL
时,会发生细分违规。
顺便说一下,重新调整节点之间的连接指针是不够的。您还应该free
已删除的节点。
我认为将一个指向头部指针的指针传递给te函数而不是返回新的头部指针,这对于你的函数来说是一个更好的设计。您不仅确定调用者不会忘记实际存储新头,还可以保存许多if (temp->parent->left == temp)
分支,因为指向节点指针的指针已经是正确的&#34;何处&#34;指针:head
指针,父指针left
指针或父指针right
。