1

二分探索木に値を設定するメソッドを書こうとしていました。ツリーにノードを追加するための再帰の簡単な手法を実装しました。しかし、値を入力してコードを実行すると、セグメンテーション違反が発生しました。

struct Node
{
    int data;
    Node* leftN;
    Node* rightN;

};

typedef Node* Node_ptr;
Node_ptr head;

//INSERT_VALUE FUNCTION
Node* new_node(int key)
{
    Node* leaf = new Node;
    leaf->data = key;
    leaf->leftN = NULL;
    leaf->rightN = NULL;
}
Node* insert_value(Node_ptr leaf, int key)
{
    if(leaf == NULL)
        return(new_node(key));
    else
    {
        if(key <= leaf->data)
            leaf->leftN = insert_value(leaf->leftN, key);
        else
            leaf->rightN = insert_value(leaf->rightN, key);
        return(leaf);   
    }
}

//PRINT FUNCTION
void printTree(Node_ptr leaf)
{
    if(leaf == NULL)
        return;
    printTree(leaf->leftN);
    cout << "Data element: " << leaf->data << endl;
    printTree(leaf->rightN);
}

//MAIN
int main()
{
    Node_ptr root = NULL;
    Node_ptr tail;
    int i;
    int x;

    //initialize values
    for(i = 0; i < 20; i++)
    {
        x = rand() % 1000 + 1;
        tail = insert_value(root, x);
            root = head;
    }

    root = head;
    printTree(root);

    root = head;
    cout << "Head Node: " << root->data << endl;

    return 0;
}
4

1 に答える 1

1

頭を設定したことがないため、セグメンテーション違反が発生しています。

cout << "Head Node: " << root->data << endl;

ルート値はNULLになります(headによって設定されているため、NULLです)。

「ルート」(または「ヘッド」)ノードは通常、特殊なケースのシナリオです。そのノードがの上部に構築されているかどうかを確認し、構築されていinsert_valueない場合は、ノードノードを割り当てます。

new_nodeまた、値を返さないため、コードにエラーがあります。

于 2012-04-12T01:24:26.030 に答える