Inserting treeNodes from array causes “empty” nodes to be inserted
up vote
-1
down vote
favorite
The intent of my code is import treeNodes from an array. The problem with this is that even though I made sure to set root->left and root->right as NULL when I create new treeNodes.
When I traverse the tree and end up at the tree's leaves, the left and right members are still NOT NULL.
treeNode* import_treeNode(treeNode* root, int nodes, int curr_i, int size)
if (curr_i < size)
treeNode* newNode = new treeNode;
root = newNode;
root->value = nodes[curr_i];
if (2 * curr_i + 1 < size)
root->left = import_treeNode(root->left, nodes, 2 * curr_i + 1, size);
else
root->left = NULL;
if (2 * curr_i + 2 < size)
root->right = import_treeNode(root->right, nodes, 2 * curr_i + 2, size);
else
root->right = NULL;
return root;
else
return NULL;
c++ data-structures
add a comment |
up vote
-1
down vote
favorite
The intent of my code is import treeNodes from an array. The problem with this is that even though I made sure to set root->left and root->right as NULL when I create new treeNodes.
When I traverse the tree and end up at the tree's leaves, the left and right members are still NOT NULL.
treeNode* import_treeNode(treeNode* root, int nodes, int curr_i, int size)
if (curr_i < size)
treeNode* newNode = new treeNode;
root = newNode;
root->value = nodes[curr_i];
if (2 * curr_i + 1 < size)
root->left = import_treeNode(root->left, nodes, 2 * curr_i + 1, size);
else
root->left = NULL;
if (2 * curr_i + 2 < size)
root->right = import_treeNode(root->right, nodes, 2 * curr_i + 2, size);
else
root->right = NULL;
return root;
else
return NULL;
c++ data-structures
Looks like your default constructor doesn't initialize the values... Make sure all of your private variables are initialized... Especially when you are doing justtreeNode() = default;
for constructor declaration...
– Ruks
Nov 11 at 6:24
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
The intent of my code is import treeNodes from an array. The problem with this is that even though I made sure to set root->left and root->right as NULL when I create new treeNodes.
When I traverse the tree and end up at the tree's leaves, the left and right members are still NOT NULL.
treeNode* import_treeNode(treeNode* root, int nodes, int curr_i, int size)
if (curr_i < size)
treeNode* newNode = new treeNode;
root = newNode;
root->value = nodes[curr_i];
if (2 * curr_i + 1 < size)
root->left = import_treeNode(root->left, nodes, 2 * curr_i + 1, size);
else
root->left = NULL;
if (2 * curr_i + 2 < size)
root->right = import_treeNode(root->right, nodes, 2 * curr_i + 2, size);
else
root->right = NULL;
return root;
else
return NULL;
c++ data-structures
The intent of my code is import treeNodes from an array. The problem with this is that even though I made sure to set root->left and root->right as NULL when I create new treeNodes.
When I traverse the tree and end up at the tree's leaves, the left and right members are still NOT NULL.
treeNode* import_treeNode(treeNode* root, int nodes, int curr_i, int size)
if (curr_i < size)
treeNode* newNode = new treeNode;
root = newNode;
root->value = nodes[curr_i];
if (2 * curr_i + 1 < size)
root->left = import_treeNode(root->left, nodes, 2 * curr_i + 1, size);
else
root->left = NULL;
if (2 * curr_i + 2 < size)
root->right = import_treeNode(root->right, nodes, 2 * curr_i + 2, size);
else
root->right = NULL;
return root;
else
return NULL;
c++ data-structures
c++ data-structures
asked Nov 11 at 6:15
Timothy Huang
11
11
Looks like your default constructor doesn't initialize the values... Make sure all of your private variables are initialized... Especially when you are doing justtreeNode() = default;
for constructor declaration...
– Ruks
Nov 11 at 6:24
add a comment |
Looks like your default constructor doesn't initialize the values... Make sure all of your private variables are initialized... Especially when you are doing justtreeNode() = default;
for constructor declaration...
– Ruks
Nov 11 at 6:24
Looks like your default constructor doesn't initialize the values... Make sure all of your private variables are initialized... Especially when you are doing just
treeNode() = default;
for constructor declaration...– Ruks
Nov 11 at 6:24
Looks like your default constructor doesn't initialize the values... Make sure all of your private variables are initialized... Especially when you are doing just
treeNode() = default;
for constructor declaration...– Ruks
Nov 11 at 6:24
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
When you assign to the root:
root = newNode;
You are losing the root node that is passed to the function. So for example here:
root->left = import_treeNode(root->left, nodes, 2 * curr_i + 1, size);
The root-left that you pass to the recursive call in not valid.
Perhaps you can use the newNode
throughout the function instead of assigning it to the root and using the root.
I apologize. I should have been more clear in my description. My nodes are being successfully inserted but I get ghost nodes where there shouldn't be. These empty/ghost nodes would have its value set at 0, but nowhere in my constructor do I ever set the left/right nodes of a given node to take value 0. For example, I specifically do if (2 * curr_i + 1 < size)... to ensure that the potential left node would be valid, if not, set this field to NULL. But instead of NULL, it still returns a node with value 0.
– Timothy Huang
Nov 12 at 7:41
Actually your code works, maybe you have a bug in traversal code.
– perreal
Nov 13 at 3:17
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
When you assign to the root:
root = newNode;
You are losing the root node that is passed to the function. So for example here:
root->left = import_treeNode(root->left, nodes, 2 * curr_i + 1, size);
The root-left that you pass to the recursive call in not valid.
Perhaps you can use the newNode
throughout the function instead of assigning it to the root and using the root.
I apologize. I should have been more clear in my description. My nodes are being successfully inserted but I get ghost nodes where there shouldn't be. These empty/ghost nodes would have its value set at 0, but nowhere in my constructor do I ever set the left/right nodes of a given node to take value 0. For example, I specifically do if (2 * curr_i + 1 < size)... to ensure that the potential left node would be valid, if not, set this field to NULL. But instead of NULL, it still returns a node with value 0.
– Timothy Huang
Nov 12 at 7:41
Actually your code works, maybe you have a bug in traversal code.
– perreal
Nov 13 at 3:17
add a comment |
up vote
0
down vote
When you assign to the root:
root = newNode;
You are losing the root node that is passed to the function. So for example here:
root->left = import_treeNode(root->left, nodes, 2 * curr_i + 1, size);
The root-left that you pass to the recursive call in not valid.
Perhaps you can use the newNode
throughout the function instead of assigning it to the root and using the root.
I apologize. I should have been more clear in my description. My nodes are being successfully inserted but I get ghost nodes where there shouldn't be. These empty/ghost nodes would have its value set at 0, but nowhere in my constructor do I ever set the left/right nodes of a given node to take value 0. For example, I specifically do if (2 * curr_i + 1 < size)... to ensure that the potential left node would be valid, if not, set this field to NULL. But instead of NULL, it still returns a node with value 0.
– Timothy Huang
Nov 12 at 7:41
Actually your code works, maybe you have a bug in traversal code.
– perreal
Nov 13 at 3:17
add a comment |
up vote
0
down vote
up vote
0
down vote
When you assign to the root:
root = newNode;
You are losing the root node that is passed to the function. So for example here:
root->left = import_treeNode(root->left, nodes, 2 * curr_i + 1, size);
The root-left that you pass to the recursive call in not valid.
Perhaps you can use the newNode
throughout the function instead of assigning it to the root and using the root.
When you assign to the root:
root = newNode;
You are losing the root node that is passed to the function. So for example here:
root->left = import_treeNode(root->left, nodes, 2 * curr_i + 1, size);
The root-left that you pass to the recursive call in not valid.
Perhaps you can use the newNode
throughout the function instead of assigning it to the root and using the root.
answered Nov 11 at 6:45
perreal
71.4k9109137
71.4k9109137
I apologize. I should have been more clear in my description. My nodes are being successfully inserted but I get ghost nodes where there shouldn't be. These empty/ghost nodes would have its value set at 0, but nowhere in my constructor do I ever set the left/right nodes of a given node to take value 0. For example, I specifically do if (2 * curr_i + 1 < size)... to ensure that the potential left node would be valid, if not, set this field to NULL. But instead of NULL, it still returns a node with value 0.
– Timothy Huang
Nov 12 at 7:41
Actually your code works, maybe you have a bug in traversal code.
– perreal
Nov 13 at 3:17
add a comment |
I apologize. I should have been more clear in my description. My nodes are being successfully inserted but I get ghost nodes where there shouldn't be. These empty/ghost nodes would have its value set at 0, but nowhere in my constructor do I ever set the left/right nodes of a given node to take value 0. For example, I specifically do if (2 * curr_i + 1 < size)... to ensure that the potential left node would be valid, if not, set this field to NULL. But instead of NULL, it still returns a node with value 0.
– Timothy Huang
Nov 12 at 7:41
Actually your code works, maybe you have a bug in traversal code.
– perreal
Nov 13 at 3:17
I apologize. I should have been more clear in my description. My nodes are being successfully inserted but I get ghost nodes where there shouldn't be. These empty/ghost nodes would have its value set at 0, but nowhere in my constructor do I ever set the left/right nodes of a given node to take value 0. For example, I specifically do if (2 * curr_i + 1 < size)... to ensure that the potential left node would be valid, if not, set this field to NULL. But instead of NULL, it still returns a node with value 0.
– Timothy Huang
Nov 12 at 7:41
I apologize. I should have been more clear in my description. My nodes are being successfully inserted but I get ghost nodes where there shouldn't be. These empty/ghost nodes would have its value set at 0, but nowhere in my constructor do I ever set the left/right nodes of a given node to take value 0. For example, I specifically do if (2 * curr_i + 1 < size)... to ensure that the potential left node would be valid, if not, set this field to NULL. But instead of NULL, it still returns a node with value 0.
– Timothy Huang
Nov 12 at 7:41
Actually your code works, maybe you have a bug in traversal code.
– perreal
Nov 13 at 3:17
Actually your code works, maybe you have a bug in traversal code.
– perreal
Nov 13 at 3:17
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246337%2finserting-treenodes-from-array-causes-empty-nodes-to-be-inserted%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Looks like your default constructor doesn't initialize the values... Make sure all of your private variables are initialized... Especially when you are doing just
treeNode() = default;
for constructor declaration...– Ruks
Nov 11 at 6:24