gpt4 book ai didi

c++ - 删除 BST 中的节点时出现运行时错误

转载 作者:太空宇宙 更新时间:2023-11-04 13:18:25 25 4
gpt4 key购买 nike

This is the code for Insertion and Deletion in BST. A Problem occuring while deleting the node having both child and it gives run-time error.All others Functions are working Properly. I think something went wrong in delete_node function.What changes should I make so that it can properly delete the node?

#include<stdio.h>
#include<iostream>
#include<stdlib.h>
struct node* parent(struct node* root, int target);
using namespace std;

struct node{
int data;
struct node* left;
struct node* right;
};
struct node* head=NULL;
struct node* newnode(int value)
{
struct node* newnode=(struct node* )malloc(sizeof(struct node));
newnode->data=value;
newnode->left=NULL;
newnode->right=NULL;
return newnode;
}

void insert(struct node* root,int data)
{
struct node* temp=newnode(data);

if(head==NULL)
{
head=temp;
}
else if(root->data >= temp->data)
{
if(root->left != NULL)
{
insert(root->left,temp->data);
}
else//if(root->left==NULL)
{
root->left=temp;
}
}
else
{
if(root->data < temp->data)
{
if(root->right!=NULL)
{
insert(root->right,temp->data);
}
else
{
root->right=temp;
}
}
}
}
void delete_node(struct node* root,int data)
{
struct node* y=NULL;
struct node* adjust=NULL;
struct node* temp=NULL;

if(search_rec(root,data))
{
temp=search_rec(root,data);
}

if(temp->left == NULL && temp->right == NULL)
{
y=parent(root,data);
if(y->data < data)
y->right=NULL;
else
y->left=NULL;
}
else if(temp->left == NULL)
{
y=parent(root,data);
if(y->data < data)
y->right=temp->right;
else
y->left=temp->right;
delete(temp);
}
else if(temp->right == NULL)
{
y=parent(root,data);
if(y->data < data)
y->right=temp->left;
else
y->left=temp->left;
delete(temp);
}
else//(it has both left and right child)
{
adjust=min(temp->right);
// cout<<adjust->data;
temp->data=adjust->data;
delete_node(temp->right,adjust->data);
}
}

struct node* min(struct node* temp)
{
if(head==NULL)
{
cout<<"Tree is empty";
}
else
{
while(temp->left!=NULL)
{
temp=temp->left;
}
//cout<<"Key is="<<temp->data;
return temp;
}
}
struct node* parent(struct node* root, int target)
{
if(head == NULL || head->data == target)
return NULL;
if(root->left)
{
if(root->left->data == target)
{
//cout<<root->data;
return root;
}
}
if(root->right)
{
if(root->right->data == target)
{
//cout<<root->data;
return root;
}
}
if (root->data < target)
{
parent(root->right,target);
}
else // (root->data >= target)
{
parent(root->left,target);
}
}

int main()
{
int choice,number,subchoice;
struct node* add=NULL;
insert(head,2);
insert(head,4);
insert(head,5);
insert(head,6);
insert(head,65);
insert(head,24);
insert(head,56);
insert(head,78);
insert(head,100);
insert(head,1);
insert(head,0);
insert(head,57);
delete_node(head,65);
}

最佳答案

在这些行中:

if(temp->left == NULL && temp->right == NULL){ 
y=parent(root,data);

我猜你更想要 temp 的父级:

    y=parent(root,data);

关于c++ - 删除 BST 中的节点时出现运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36301876/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com