访问BST子节点时出现分段错误。为什么?

时间:2015-03-31 01:11:56

标签: c++ segmentation-fault binary-search-tree

当我尝试在下面的定义中访问(x->左)时,函数tdeleteLeft(x)中发生分段错误:

void BST::tdeleteLeft(Node* x){
    if (x->left == NULL){
        tdelete(x);
        }
        else{
        Node* y;
        y = max(x->left);
        tdelete(x->left);
        x = y;}
}

这是完整的程序:

#include <iostream>
#include <ctime>  
#include <cstdlib>
#include <iostream>

using namespace std;


class BST {
    public:
    typedef struct node {
              int value;         
              struct node *left;  
              struct node *right; 

              node() {
                left = NULL;
                right = NULL;
                }
       }Node;

    Node *root;
    Node* i;
    Node* y;
    int z;
    static int c;


    int count();
    void inc();
    BST(int n);
    void tdelete(Node* x);
    Node* max(Node* x);
    void remove_node(Node* x);
    Node* min(Node* x);
    void tdeleteLeft(Node* x);
    void insert(Node* tree, int val);

};




int main ()
{  
  // create BST tree with n nodes
  BST *tree = new BST(10);

  clock_t t;
  t = clock();
  tree->remove_node(tree->root);
  t = clock() - t;
  cout << t << " clicks " <<  ((float)t)/CLOCKS_PER_SEC << "seconds).\n" << endl;

  return 0;
}




BST::BST(int n){
    c = 1;
    z = 0;
    Node *root = NULL;
    for (int i=0; i<n; i++){
        int rando = rand() % 10;
        insert(root, rando);
        }
    cout << "created " << n << "-node BST" << endl; 
}

int BST::c;

int BST::count(){
    return c;
    }

void BST::inc(){
    c++;
    }


BST::Node* BST::max(Node* x){
    if (x->right == NULL){
        return x;
        }
    else
        return max(x->right);   
    }

BST::Node* BST::min(Node* x){
    Node* i = x;
    while (i->left !=NULL){
        i = i->left;
        }
    return i    ;
    }


void BST::tdelete(Node* x){
    if (x->right!=NULL){
        tdelete(x->right);
        }
    if  (x->left!=NULL){
        tdelete(x->left);
        }
    delete(x);
    }

void BST::tdeleteLeft(Node* x){
    if (x->left == NULL){
        tdelete(x);
        }
        else{
        Node* y;
        y = max(x->left);
        tdelete(x->left);
        x = y;}
}

void BST::remove_node(Node* x){
    tdeleteLeft(x);
}



void BST::insert(Node *tree, int val){

               if(tree==NULL){
                    BST::Node* tree = new BST::Node();
                    tree->left = NULL;
                    tree->right = NULL;
                    tree->value = val;
                    c++;
                    }
               else{
                    if(c%2==0){
                        insert(tree->left, val);}
                    else
                    insert(tree->right, val);}

}

1 个答案:

答案 0 :(得分:0)

首先,root

中的局部变量BST::BST(int)的以下声明
BST::BST(int n){
    c = 1;
    z = 0;
    Node *root = NULL;  // <--- This defines a local variable which shadows BST::root
    for (int i=0; i<n; i++){
    ...

shadows同名成员。相应地,tree->root中的main在调用构造函数后仍未初始化。 结果tree->remove_node(tree->root)尝试删除未初始化指针的“左”节点,这会导致您正在观察的分段错误。要解决该问题,您需要删除root变量的本地声明:

BST::BST(int n){
    c = 1;
    z = 0;
    for (int i=0; i<n; i++){
    ...

然后你应该注意insert(root, rando)不会更新调用者上下文中的root变量,因为指针是按值传递的,因此分配的树节点变得无法访问。为防止这种情况,您可以使用以下命令返回更新的根节点:

Node* BST::insert(Node* tree, int val){

    if(tree==NULL){
        tree = new BST::Node(); // careful about shadowing the "tree" argument
        ...
    }
    else{
        if(c%2==0){
            tree->left  = insert(tree->left, val);
        }
        else{
            tree->right = insert(tree->right, val);
        }
    }
    return tree;
}

你在构造函数中调用它是这样的:

BST::BST(int n){
    c = 1;
    z = 0;
    for (int i=0; i<n; i++){
        int rando = rand() % 10;
        root = insert(root, rando);
    }
}

或通过引用传递指针:

void BST::insert(Node*& tree, int val){
   ...
}