I have code below that consist of binary tree data structure:
#include <bits/stdc++.h>
#define DEFAULT_NODE_VALUE 0
using namespace std;
template <class T>
class node{
public:
T val;
node* right = 0;
node* left = 0;
node(T a):val(a){}
};
template <class T>
class tree{
public:
node<T>* root = new node<T>(DEFAULT_NODE_VALUE);
tree(T inp_val){
root->val = inp_val;
}
void inorder_traverse(node<T>* temp){
if (!temp)
return;
inorder_traverse(temp->left);
cout << temp->val << " -> ";
inorder_traverse(temp->right);
}
void inorder_traverse(){
inorder_traverse(root);
}
};
int main()
{
tree<string> my_tree("mantap");
my_tree.root->right = new node<string>("ok");
my_tree.root->left = new node<string>("haha");
my_tree.inorder_traverse();
return 0;
}
When I run it, it showed me an exeption like this below:
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_M_construct null not valid
Can anyone help me to fix this runtime error, please? Thanks in advance...