0

現在、2-3-4 ツリーを使用するプログラムを作成しようとしていますが、挿入機能に問題があります。ここに関連するコードがあります..

int main () {

    tree234 myTree;
    myTree.insert("hello");

    myTree.printTree();

    return 0;
}

//-------------tree234.cpp-------------
#include "tree234.h"
#include <string>
#include <iostream>

void tree234::insert(string input){
    int index;

    if (nullRoot == true) {
        //insert root
        initNode(root);

        root->data[0] = input;

        nullRoot = false;

        return;
    }
}

void tree234::initNode(Node* node) {
    node = new Node();
    node->pointer[0] = NULL;
    node->pointer[1] = NULL;
    node->pointer[2] = NULL;
    node->pointer[3] = NULL;
    node->data[0] = "";
    node->data[1] = "";
    node->data[2] = "";
}

//-----------tree234.h--------------
#ifndef TREE_234_H_
#define TREE_234_H_

using namespace std;
#include <iostream>

class tree234{
private:
    struct Node {
    public:
        string data[3];
        Node* pointer[4];
    };

    Node* curr;
    Node* root;
    Node* right;
    Node* newRoot;
    bool nullRoot = true;

public:
    void insert(string data);
    void initNode(Node* node);
};

#endif

常に 19 行目で中断し、メモリ アドレス エラーが発生します。私はそれをデバッグしようとしましたが、文字列ファイル内の 2245 行で壊れています (それがまったく役立つ場合)。その情報はあまり役に立たなかったので、誰かがここで何が間違っているのか正確に教えてくれるでしょうか?

4

2 に答える 2

0

1つは、次のとおりです。

if (nullRoot = true) {

おそらく必要なのは「==」です。エラーだけでなく、コンパイラの警告も確認する必要があります。それが役立つことを願っています。:)

「==」は比較 (結果のブール値) ですが、「=」は代入であるため、nullRoot を true に設定しています。

于 2014-02-12T23:14:23.883 に答える