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.
That's because 'left' & 'right' are pointers.
To print out the 'data' of the left or right, change the code as follows:
cout << "left: " << test->left->data << endl;
cout << "right: " << test->right->data << endl;
Note, however, if left or right are NULL (ie, zero) you'll likely get a memory access exception.
struct node {
int data; // the actual data contained inside this node
struct node* left; // a node pointer that points to the left child
struct node* right; // a node pointer that points to the right child
};
struct node* test; // is a node pointer
test->left; // is a node pointer that points to the left child of test
test->right; // is a node pointer that points to the right child of test
cout << test->data; // prints the integer contained within the test node
cout << test->left; // prints the address of the left child of test since it's a pointer
cout << test->right; // prints the address of the right child of test since it's a pointer
What you want to do is print the data contained within the left and right children.
cout << test->left->data;
cout << test->right->data;
test->left
is (*test).left
which is of type struct node*
.
To print the data in left
you need
cout << (test -> left -> data);
You can print "test->data" correctly because that's an int. The issue is that "test->left" and "test->right" are pointers, and pointers are basically numbers that refer to where another object is stored.
If you wanted to print the left node's data, you'd have to do this:
cout << "left: " << test->left->data << endl;
And then you'd have to do the same for the right node.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.