在BST中删除节点时出现运行时错误

时间:2016-03-30 07:05:48

标签: c++ binary-search-tree

  

这是BST中插入和删除的代码。删除具有子节点的节点时发生问题会产生运行时错误。所有其他功能正常工作。我认为delete_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);
}

1 个答案:

答案 0 :(得分:0)

在这些方面:

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

我想你更想要临时父母:

    y=parent(root,data);