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;












share|improve this question





















  • 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















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;












share|improve this question





















  • 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













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;












share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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
















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













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.






share|improve this answer




















  • 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










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















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

























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.






share|improve this answer




















  • 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














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.






share|improve this answer




















  • 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












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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










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
















  • 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

















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

政党

天津地下鉄3号線