如何删除树中的节点? 我试图用指针实现C++中的简单二叉树。< /P>

如何删除树中的节点? 我试图用指针实现C++中的简单二叉树。< /P>,c++,algorithm,pointers,data-structures,tree,C++,Algorithm,Pointers,Data Structures,Tree,我已成功实现了插入和遍历,但在尝试删除节点时遇到了一些问题: 我的主要职能是: main(){ node* root=createNode(1); root->left=createNode(2); root->right=createNode(3); root->left->left=createNode(4); root->left->right=createNode(5); root->right->left=createNode(6); r

我已成功实现了插入和遍历,但在尝试删除节点时遇到了一些问题:

我的主要职能是:

main(){
node* root=createNode(1);
root->left=createNode(2);
root->right=createNode(3);
root->left->left=createNode(4);
root->left->right=createNode(5);
root->right->left=createNode(6);
root->right->right=createNode(7);
inorder(root);
//till here it works fine

delete root->left->left;     //problem starts here
cout<<"\n";
inorder(root);               //exception is thrown here...
return 0;
main(){
node*root=createNode(1);
根->左=创建节点(2);
根->右=创建节点(3);
根->左->左=创建节点(4);
根->左->右=创建节点(5);
根->右->左=创建节点(6);
根->右->右=创建节点(7);
顺序(根);
//在这里,一切都很好
删除根->左->左;//问题从这里开始

cout在
删除之后,尝试访问已删除的指针将导致此问题。您可能要添加

root->left->left = 0

在删除行之后。

您必须将
根->左->左
指针设置为
,也就是说,它指向零

在你的代码中

main()
{
   node* root=createNode(1);
   root->left=createNode(2);
   root->right=createNode(3);
   root->left->left=createNode(4);
   root->left->right=createNode(5);
   root->right->left=createNode(6);
   root->right->right=createNode(7);
   inorder(root);
   //Now, Complete program will work

   delete root->left->left =  0;     //problem solved
   cout<<"\n";
   inorder(root);               // Do you still face any problem ?
   return 0;
}
main()
{
node*root=createNode(1);
根->左=创建节点(2);
根->右=创建节点(3);
根->左->左=创建节点(4);
根->左->右=创建节点(5);
根->右->左=创建节点(6);
根->右->右=创建节点(7);
顺序(根);
//现在,完整的程序将起作用
删除根->左->左=0;//问题已解决

cout删除节点后,需要将父节点的左指针重置为零。
不这样做可能会导致不可预测的结果,具体取决于内存管理的实现方式。一些系统对释放内存的引用产生异常

引发了什么异常?您确定这是异常吗?
createNode
是否将
指针设置为
?@JosephMansfield,I我打赌是sigsegv使应用程序崩溃。
Your trying to delete node which has not null nodes in both right and left side, so you to delete node like this ..

/* deletes a node from the binary search tree */
void delete ( struct btreenode **root, int num )
{
    int found ;
    struct btreenode *parent, *x, *xsucc ;

    /* if tree is empty */
if ( *root == NULL )
    {
        printf ( "\nTree is empty" ) ;
        return ;
    }

    parent = x = NULL ;

    /* call to search function to find the node to be deleted */

    search ( root, num, &parent, &x, &found ) ;

    /* if the node to deleted is not found */
if ( found == FALSE )
    {
        printf ( "\nData to be deleted, not found" ) ;
        return ;
    }

    /* if the node to be deleted has two children */
if ( x -> leftchild != NULL && x -> rightchild != NULL )
    {
        parent = x ;
        xsucc = x -> rightchild ;

        while ( xsucc -> leftchild != NULL )
        {
            parent = xsucc ;
            xsucc = xsucc -> leftchild ;
        }

        x -> data = xsucc -> data ;
        x = xsucc ;
    }

    /* if the node to be deleted has no child */
if ( x -> leftchild == NULL && x -> rightchild == NULL )
    {
        if ( parent -> rightchild == x )
            parent -> rightchild = NULL ;
        else
            parent -> leftchild = NULL ;

        free ( x ) ;
        return ;
    }

    /* if the node to be deleted has only rightchild */
if ( x -> leftchild == NULL && x -> rightchild != NULL )
    {
        if ( parent -> leftchild == x )
            parent -> leftchild = x -> rightchild ;
        else
            parent -> rightchild = x -> rightchild ;

        free ( x ) ;
        return ;
    }

    /* if the node to be deleted has only left child */
if ( x -> leftchild != NULL && x -> rightchild == NULL )
    {
        if ( parent -> leftchild == x )
            parent -> leftchild = x -> leftchild ;
        else
            parent -> rightchild = x -> leftchild ;

        free ( x ) ;
        return ;
    }
}
Your trying to delete node which has not null nodes in both right and left side, so you to delete node like this ..

/* deletes a node from the binary search tree */
void delete ( struct btreenode **root, int num )
{
    int found ;
    struct btreenode *parent, *x, *xsucc ;

    /* if tree is empty */
if ( *root == NULL )
    {
        printf ( "\nTree is empty" ) ;
        return ;
    }

    parent = x = NULL ;

    /* call to search function to find the node to be deleted */

    search ( root, num, &parent, &x, &found ) ;

    /* if the node to deleted is not found */
if ( found == FALSE )
    {
        printf ( "\nData to be deleted, not found" ) ;
        return ;
    }

    /* if the node to be deleted has two children */
if ( x -> leftchild != NULL && x -> rightchild != NULL )
    {
        parent = x ;
        xsucc = x -> rightchild ;

        while ( xsucc -> leftchild != NULL )
        {
            parent = xsucc ;
            xsucc = xsucc -> leftchild ;
        }

        x -> data = xsucc -> data ;
        x = xsucc ;
    }

    /* if the node to be deleted has no child */
if ( x -> leftchild == NULL && x -> rightchild == NULL )
    {
        if ( parent -> rightchild == x )
            parent -> rightchild = NULL ;
        else
            parent -> leftchild = NULL ;

        free ( x ) ;
        return ;
    }

    /* if the node to be deleted has only rightchild */
if ( x -> leftchild == NULL && x -> rightchild != NULL )
    {
        if ( parent -> leftchild == x )
            parent -> leftchild = x -> rightchild ;
        else
            parent -> rightchild = x -> rightchild ;

        free ( x ) ;
        return ;
    }

    /* if the node to be deleted has only left child */
if ( x -> leftchild != NULL && x -> rightchild == NULL )
    {
        if ( parent -> leftchild == x )
            parent -> leftchild = x -> leftchild ;
        else
            parent -> rightchild = x -> leftchild ;

        free ( x ) ;
        return ;
    }
}