I'm trying to write a simple binary search tree in C++, and I'm getting hung up on the insert function, more specifically, where the program checks to see if my node is NULL. The if statement evaluates as false and nothing gets inserted. Could anyone give me a hint as to what I'm doing wrong? Thanks!
Here's my code:
Here's my code:
Code:
#include <iostream>
using namespace std;
struct Node
{
int value;
Node *left;
Node *right;
};
void visit(Node *ptr)
{
if (ptr->left == NULL)
visit(ptr->left);
cout << ptr->value;
if (ptr->right == NULL)
visit(ptr->right);
}
void insert(int data, Node *ptr)
{
if (ptr == NULL)
{
ptr = new Node;
ptr->value = data;
ptr->left = NULL;
ptr->right = NULL;
}
else if (data < ptr->value)
insert(data, ptr->left);
else if (data > ptr->value)
insert(data, ptr->right);
}
int main()
{
Node *myTree = new Node;
insert (5, myTree);
cout << myTree->value << endl;
return 0;
}