c++ - 我的 bst 实现有什么问题吗?

标签 c++ c algorithm containers binary-search-tree

我有一些学校作业,我应该实现 BST 来解决其中两个问题。 所以我开始了,我只需要删除、插入和搜索功能。 但每次我运行我的代码时,我都会遇到段错误。谁能帮我?? 这是我的代码:

#include <iostream>
#include <stack>
#include <vector>
#include <algorithm>
#include <set>
#include <cstdio>
#define MP make_pair
#define pb push_back
#define X first
#define Y second

using namespace std;

typedef unsigned long long int ulli;


ulli n;


using namespace std;

struct BST{ // Binary Search Tree
    struct node{ // Node
        node *left; // Left Child
        node *par; // Parent node
        node *right; // Right child
        int val; // the value

        node(){
            left = par = right = NULL;
            val = 0;
        }

    };
    node *root; // Root of the tree

    BST(){
        root = NULL;
    }
    bool insert( int value ){
        node *tmp = new node;

        tmp->val = value;

        if ( root == NULL ){
            root = tmp;
            return 0;
        }

        node *cur = root;
        while ( tmp->par != cur ){
            if ( tmp->val < cur->val ){
                if ( cur->left == NULL ){
                    tmp->par = cur;
                    cur->left = tmp;
                    break;
                }
                cur = cur->left;
            }else{
                if ( cur->right == NULL ){
                    tmp->par = cur;
                    cur->right = tmp;
                    break;
                }
                cur = cur->right;
            }
        }
        return 0;
    }

    node *search( int val ){
        node *tmp = root;
        while( tmp->val != val ){
            if ( val < tmp->val )
                tmp = tmp->left;
            else
                tmp = tmp->right;
            if ( tmp == NULL ){
                return NULL;
            }
        }
        return tmp;
    }

    bool erase( int val ){
        node *tmp = search( val ), *k;
        k = tmp;
        if ( tmp == root ){
            if ( tmp->left == NULL && tmp->right == NULL ){
                root = NULL;
                delete tmp;
            }
            if ( tmp->left != NULL && tmp->right == NULL ){
                root = root->left;
                delete tmp;
                return 0;
            }
            if ( tmp->left == NULL && tmp->right != NULL ){
                root = root->right;
                delete tmp;
                return 0;
            }
            tmp = tmp->right;
            while( tmp->left != NULL )
                tmp = tmp->left;
            if ( tmp->right != NULL )
                tmp->par->left = tmp->right;
            else
                tmp->par->left = NULL;
            tmp->par = NULL;
            tmp->right = k->right;
            tmp->left = k->left;
            root = tmp;
            return 0;
        }
        if ( tmp->left == NULL && tmp->right == NULL ){
            if ( tmp->par->left == tmp )
                tmp->par->left = NULL;
            else
                tmp->par->right = NULL;
            delete tmp;
        }
        if ( tmp->left != NULL && tmp->right == NULL ){
            tmp->left->par = tmp->par;
            if ( tmp->par->left == tmp )
                tmp->par->left = tmp->left;
            else
                tmp->par->right = tmp->left;
            delete tmp;
            return 0;
        }
        if ( tmp->left == NULL && tmp->right != NULL ){
            tmp->right->par = tmp->par;
            if ( tmp->par->left == tmp )
                tmp->par->left = tmp->right;
            else
                tmp->par->right = tmp->right;
            delete tmp;
            return 0;
        }
        tmp = tmp->right;
        while( tmp->left != NULL )
            tmp = tmp->left;
        if ( tmp->right != NULL )
            tmp->par->left = tmp->right;
        else
            tmp->par->left = NULL;
        tmp->par = k->par;
        if ( k->par->right == k )
            k->par->right = tmp;
        else
            k->par->left = tmp;
        tmp->right = k->right;
        tmp->left = k->left;
    }




};

BST b;

vector<int> v;

int main () {
    cin >> n;
    int k;
    for ( int i = 0; i < n; i++ ){
        cin >> k;
        v.pb( k );
        b.insert( k );
    }
    for ( int i = 0; i < n; i++ )
        b.erase( v[i] );


    return 0;
}

最佳答案

我想您正在解除对悬空指针的引用,因为您缺少 return 0; 语句

   bool erase( int val ){
        node *tmp = search( val ), *k;
        k = tmp;
        if ( tmp == root ){
            if ( tmp->left == NULL && tmp->right == NULL ){
                root = NULL;
                delete tmp; // tmp is deleted, should no longer be used afterwards
                // return 0; // Missing return 0
            }
            if ( tmp->left != NULL && tmp->right == NULL ){
                ...
            }
            if ( tmp->left == NULL && tmp->right != NULL ){
                ...
            }
            tmp = tmp->right; // tmp is dereferenced
            while( tmp->left != NULL )
                tmp = tmp->left;

此外:如果 search(val) 返回 NULL,则不会进行错误检查,尽管这是可能的:

node *search( int val ){
    node *tmp = root;
    while( tmp->val != val ){
        if ( val < tmp->val )
            tmp = tmp->left;
        else
            tmp = tmp->right;
        if ( tmp == NULL ){
            return NULL; <---
        }
    }
    return tmp;
}

关于c++ - 我的 bst 实现有什么问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26337501/

相关文章:

C编程,由于scanf导致的错误循环?

algorithm - 用颜色填充封闭的不规则形状的方法是什么?

c++ - 在 Windows 上编写 C++,但通过 Linux 模拟器使用 Linux 系统调用

c++ - 如何使用 C++ 中的源代码扩展 .lib?

C语言 : Referencing char arrays in nested struct

char *str 并为 str 分配内存,仍然得到 SEGFAULT

c++ - system()并继承lxc功能

c++ - 存储一组序列化的 protobuf 对象

algorithm - 如何在给定一些美元值(value)时找到所有硬币组合

algorithm - 应该在遗传算法的哪一步应用适应度共享?