I'm learning about binary trees. I was looking at Stanford website: http://cslibrary.stanford.edu/110/BinaryTrees.html There was a practice problem of making a tree by calling newNode() three times, and using three pointer variables. The struct and newNode were given. I was trying to print out the nodes.
struct node {
int data;
struct node* left;
struct node* right;
} ;
/*
Helper function that allocates a new node
with the given data and NULL left and right pointers.
*/
struct node* newNode(int data) {
struct node* node = new(struct node);
node->data = data;
node->left = NULL;
node->right = NULL;
return(node);
};
// call newNode() three times
struct node* build123a() {
struct node* root = newNode(2);
struct node* lChild = newNode(1);
struct node* rChild = newNode(3);
root->left = lChild;
root->right= rChild;
return(root);
}
int main() {
struct node* test = build123a();
cout << "root: " << test->data << endl;
cout << "left: " << test->left << endl;
cout << "right: " << test->right << endl;
return 0;
}
The issue is that this prints out only the integer in root. For the left and right nodes, it prints out address locations. My knowledge of pointers is still a little shaky. But it shouldn't matter that I've only returned root right? newNode is a pointer to a node right? Just looking for a simple fix to print out left and right nodes.