I'm currently trying to write a program that uses 2-3-4 trees and I'm having issues with the insert function. Here's the relevant code..
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
It always breaks at line 19 with a memory address error. I've tried debugging it and it breaks at line 2245 within the string file (if that helps at all). That information didn't really help me very much, so maybe someone can help me with exactly what's wrong here?
There are multiple problems. Fix following stuff and see if it works then...
cpp, insert:
Your if condition is an assignment.
cpp, initNode:
The function is changing a copy of the passed pointer.
The caller won´t get anything of the allocated object.
Use a reference to a pointer (&*) or a pointer to a pointer
(with matching function call and content) as argument..
Ie,
void tree234::initNode(Node *& node)