Issue with tree node reference being overwritten in Java












0















I am currently building a ScapeGoatTree for a project. However I am having an issue getting my rebuild function to properly take hold of the scapegoat node it is building. In the below code you will see inside the 'if( height >alpha height)' statement 2 more if statements. The first if statement compares my scapegoat node (which I know with current test data should be the static root.left node) and it correctly says they are equal. However when I try to change the Placeholder node(Which should hopefully be an object reference to root.left) it merely overwrites my placeholder node. So thus the second if statement does not fire off, but I need the returned value of my FindScapeGoat to be the node being edited.



I am not going to lie I have always been a bit bad at understanding the pass by reference and pass by value differences in languages, but I really do need to figure out how I can properly apply changes to root.left without needing a specific call to it (since the function will not always choose root.left as the scapegoat node I need a way to call various nodes in my tree rooted at a static location).



public static void Insert(int key) {
height = dupflag = 0;
root = insertHelp(root, key);
if(dupflag == 0) MaxNodeCount++; //If inserted value wasn't duplicate increase max node count
double alphaHeight = ((Math.log(MaxNodeCount) / Math.log(1 / alpha)) + 1);
if (height > alphaHeight){
Node ToBeRebalanced = FindScapegoat(root, key); // Find scapegoat node
int sizeRoot = TreeSize(ToBeRebalanced, 0);
if(ToBeRebalanced == root.left) System.out.println("Scapegoat node == root.left");
ToBeRebalanced = RebuildTree(sizeRoot+1, ToBeRebalanced);
if(ToBeRebalanced == root.left) System.out.println("Scapegoat node == root.left");
Print(ToBeRebalanced);
Print(root);
}
}









share|improve this question























  • I'm like 90% of the way to understanding what you want. Is the method that's not doing what you want RebuildTree? If so, can you post the code? If not, can you try to clarify a little bit where things are going wrong? I understand the high-level problem you're having, but not where specifically that's manifesting in your code. Also, are Placeholder and ToBeRebalanced the same thing?

    – Chris Thompson
    Nov 26 '18 at 1:35













  • Essentially, I want this line: "Node ToBeRebalanced = FindScapegoat(root, key); " Or more importantly, the return of "FindScapeGoat(root, key); " to be the object that is edited. I know that in my current testing data FindScapeGoat will return 'root.left', and I set the placeholder Node ToBeRebalanced equal to 'root.left', however 3 lines down in the line: "ToBeRebalanced = RebuildTree(sizeRoot+1, ToBeRebalanced);" The ToBeRebalanced holder node overwrites itself, instead of writing directly to root.left like I want it too.

    – Kelith White
    Nov 26 '18 at 1:40


















0















I am currently building a ScapeGoatTree for a project. However I am having an issue getting my rebuild function to properly take hold of the scapegoat node it is building. In the below code you will see inside the 'if( height >alpha height)' statement 2 more if statements. The first if statement compares my scapegoat node (which I know with current test data should be the static root.left node) and it correctly says they are equal. However when I try to change the Placeholder node(Which should hopefully be an object reference to root.left) it merely overwrites my placeholder node. So thus the second if statement does not fire off, but I need the returned value of my FindScapeGoat to be the node being edited.



I am not going to lie I have always been a bit bad at understanding the pass by reference and pass by value differences in languages, but I really do need to figure out how I can properly apply changes to root.left without needing a specific call to it (since the function will not always choose root.left as the scapegoat node I need a way to call various nodes in my tree rooted at a static location).



public static void Insert(int key) {
height = dupflag = 0;
root = insertHelp(root, key);
if(dupflag == 0) MaxNodeCount++; //If inserted value wasn't duplicate increase max node count
double alphaHeight = ((Math.log(MaxNodeCount) / Math.log(1 / alpha)) + 1);
if (height > alphaHeight){
Node ToBeRebalanced = FindScapegoat(root, key); // Find scapegoat node
int sizeRoot = TreeSize(ToBeRebalanced, 0);
if(ToBeRebalanced == root.left) System.out.println("Scapegoat node == root.left");
ToBeRebalanced = RebuildTree(sizeRoot+1, ToBeRebalanced);
if(ToBeRebalanced == root.left) System.out.println("Scapegoat node == root.left");
Print(ToBeRebalanced);
Print(root);
}
}









share|improve this question























  • I'm like 90% of the way to understanding what you want. Is the method that's not doing what you want RebuildTree? If so, can you post the code? If not, can you try to clarify a little bit where things are going wrong? I understand the high-level problem you're having, but not where specifically that's manifesting in your code. Also, are Placeholder and ToBeRebalanced the same thing?

    – Chris Thompson
    Nov 26 '18 at 1:35













  • Essentially, I want this line: "Node ToBeRebalanced = FindScapegoat(root, key); " Or more importantly, the return of "FindScapeGoat(root, key); " to be the object that is edited. I know that in my current testing data FindScapeGoat will return 'root.left', and I set the placeholder Node ToBeRebalanced equal to 'root.left', however 3 lines down in the line: "ToBeRebalanced = RebuildTree(sizeRoot+1, ToBeRebalanced);" The ToBeRebalanced holder node overwrites itself, instead of writing directly to root.left like I want it too.

    – Kelith White
    Nov 26 '18 at 1:40
















0












0








0








I am currently building a ScapeGoatTree for a project. However I am having an issue getting my rebuild function to properly take hold of the scapegoat node it is building. In the below code you will see inside the 'if( height >alpha height)' statement 2 more if statements. The first if statement compares my scapegoat node (which I know with current test data should be the static root.left node) and it correctly says they are equal. However when I try to change the Placeholder node(Which should hopefully be an object reference to root.left) it merely overwrites my placeholder node. So thus the second if statement does not fire off, but I need the returned value of my FindScapeGoat to be the node being edited.



I am not going to lie I have always been a bit bad at understanding the pass by reference and pass by value differences in languages, but I really do need to figure out how I can properly apply changes to root.left without needing a specific call to it (since the function will not always choose root.left as the scapegoat node I need a way to call various nodes in my tree rooted at a static location).



public static void Insert(int key) {
height = dupflag = 0;
root = insertHelp(root, key);
if(dupflag == 0) MaxNodeCount++; //If inserted value wasn't duplicate increase max node count
double alphaHeight = ((Math.log(MaxNodeCount) / Math.log(1 / alpha)) + 1);
if (height > alphaHeight){
Node ToBeRebalanced = FindScapegoat(root, key); // Find scapegoat node
int sizeRoot = TreeSize(ToBeRebalanced, 0);
if(ToBeRebalanced == root.left) System.out.println("Scapegoat node == root.left");
ToBeRebalanced = RebuildTree(sizeRoot+1, ToBeRebalanced);
if(ToBeRebalanced == root.left) System.out.println("Scapegoat node == root.left");
Print(ToBeRebalanced);
Print(root);
}
}









share|improve this question














I am currently building a ScapeGoatTree for a project. However I am having an issue getting my rebuild function to properly take hold of the scapegoat node it is building. In the below code you will see inside the 'if( height >alpha height)' statement 2 more if statements. The first if statement compares my scapegoat node (which I know with current test data should be the static root.left node) and it correctly says they are equal. However when I try to change the Placeholder node(Which should hopefully be an object reference to root.left) it merely overwrites my placeholder node. So thus the second if statement does not fire off, but I need the returned value of my FindScapeGoat to be the node being edited.



I am not going to lie I have always been a bit bad at understanding the pass by reference and pass by value differences in languages, but I really do need to figure out how I can properly apply changes to root.left without needing a specific call to it (since the function will not always choose root.left as the scapegoat node I need a way to call various nodes in my tree rooted at a static location).



public static void Insert(int key) {
height = dupflag = 0;
root = insertHelp(root, key);
if(dupflag == 0) MaxNodeCount++; //If inserted value wasn't duplicate increase max node count
double alphaHeight = ((Math.log(MaxNodeCount) / Math.log(1 / alpha)) + 1);
if (height > alphaHeight){
Node ToBeRebalanced = FindScapegoat(root, key); // Find scapegoat node
int sizeRoot = TreeSize(ToBeRebalanced, 0);
if(ToBeRebalanced == root.left) System.out.println("Scapegoat node == root.left");
ToBeRebalanced = RebuildTree(sizeRoot+1, ToBeRebalanced);
if(ToBeRebalanced == root.left) System.out.println("Scapegoat node == root.left");
Print(ToBeRebalanced);
Print(root);
}
}






java binary-tree






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 26 '18 at 1:31









Kelith WhiteKelith White

31




31













  • I'm like 90% of the way to understanding what you want. Is the method that's not doing what you want RebuildTree? If so, can you post the code? If not, can you try to clarify a little bit where things are going wrong? I understand the high-level problem you're having, but not where specifically that's manifesting in your code. Also, are Placeholder and ToBeRebalanced the same thing?

    – Chris Thompson
    Nov 26 '18 at 1:35













  • Essentially, I want this line: "Node ToBeRebalanced = FindScapegoat(root, key); " Or more importantly, the return of "FindScapeGoat(root, key); " to be the object that is edited. I know that in my current testing data FindScapeGoat will return 'root.left', and I set the placeholder Node ToBeRebalanced equal to 'root.left', however 3 lines down in the line: "ToBeRebalanced = RebuildTree(sizeRoot+1, ToBeRebalanced);" The ToBeRebalanced holder node overwrites itself, instead of writing directly to root.left like I want it too.

    – Kelith White
    Nov 26 '18 at 1:40





















  • I'm like 90% of the way to understanding what you want. Is the method that's not doing what you want RebuildTree? If so, can you post the code? If not, can you try to clarify a little bit where things are going wrong? I understand the high-level problem you're having, but not where specifically that's manifesting in your code. Also, are Placeholder and ToBeRebalanced the same thing?

    – Chris Thompson
    Nov 26 '18 at 1:35













  • Essentially, I want this line: "Node ToBeRebalanced = FindScapegoat(root, key); " Or more importantly, the return of "FindScapeGoat(root, key); " to be the object that is edited. I know that in my current testing data FindScapeGoat will return 'root.left', and I set the placeholder Node ToBeRebalanced equal to 'root.left', however 3 lines down in the line: "ToBeRebalanced = RebuildTree(sizeRoot+1, ToBeRebalanced);" The ToBeRebalanced holder node overwrites itself, instead of writing directly to root.left like I want it too.

    – Kelith White
    Nov 26 '18 at 1:40



















I'm like 90% of the way to understanding what you want. Is the method that's not doing what you want RebuildTree? If so, can you post the code? If not, can you try to clarify a little bit where things are going wrong? I understand the high-level problem you're having, but not where specifically that's manifesting in your code. Also, are Placeholder and ToBeRebalanced the same thing?

– Chris Thompson
Nov 26 '18 at 1:35







I'm like 90% of the way to understanding what you want. Is the method that's not doing what you want RebuildTree? If so, can you post the code? If not, can you try to clarify a little bit where things are going wrong? I understand the high-level problem you're having, but not where specifically that's manifesting in your code. Also, are Placeholder and ToBeRebalanced the same thing?

– Chris Thompson
Nov 26 '18 at 1:35















Essentially, I want this line: "Node ToBeRebalanced = FindScapegoat(root, key); " Or more importantly, the return of "FindScapeGoat(root, key); " to be the object that is edited. I know that in my current testing data FindScapeGoat will return 'root.left', and I set the placeholder Node ToBeRebalanced equal to 'root.left', however 3 lines down in the line: "ToBeRebalanced = RebuildTree(sizeRoot+1, ToBeRebalanced);" The ToBeRebalanced holder node overwrites itself, instead of writing directly to root.left like I want it too.

– Kelith White
Nov 26 '18 at 1:40







Essentially, I want this line: "Node ToBeRebalanced = FindScapegoat(root, key); " Or more importantly, the return of "FindScapeGoat(root, key); " to be the object that is edited. I know that in my current testing data FindScapeGoat will return 'root.left', and I set the placeholder Node ToBeRebalanced equal to 'root.left', however 3 lines down in the line: "ToBeRebalanced = RebuildTree(sizeRoot+1, ToBeRebalanced);" The ToBeRebalanced holder node overwrites itself, instead of writing directly to root.left like I want it too.

– Kelith White
Nov 26 '18 at 1:40














1 Answer
1






active

oldest

votes


















0














To address the value/reference issue: non-primitive variables in Java behave (mostly, big caveat that's way out of scope for this answer) like pointers to memory locations. When you say ToBeReplaced you're changing the memory address that ToBeReplaced points to.



To your more specific issue, there are a couple of ways to handle this. The way that I would handle it would be to change the return from FindScapegoat to indicate whether the node is left or right. It seems like it's examining only the immediate children, so there's no need to return a reference to the node itself.



Something like this:



public enum Side {
LEFT,
RIGHT
}
//...
Side ToBeRebalanced = FindScapegoat(root, key); // Find scapegoat node
if (ToBeRebalanced == Side.Left){
int sizeRoot = TreeSize(root.left, 0);
root.left = RebuildTree(sizeRoot+1, root.left);
} else {
int sizeRoot = TreeSize(root.right, 0);
root.right = RebuildTree(sizeRoot+1, root.right);
}


You then could move the TreeSize call into the RebuildTree method to avoid the duplicated code.






share|improve this answer



















  • 1





    More or less led me in right path of solution. Since my tree is more than height 2 I had to do a bit more then just call root.left / root.right there. But I was able to use the general idea to have FindScapegoat return parent to actual scapegoat node, and then have my references be to the returned nodes children which fixed my issues. Thank you very much!

    – Kelith White
    Nov 26 '18 at 2:38











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',
autoActivateHeartbeat: false,
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%2f53473710%2fissue-with-tree-node-reference-being-overwritten-in-java%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









0














To address the value/reference issue: non-primitive variables in Java behave (mostly, big caveat that's way out of scope for this answer) like pointers to memory locations. When you say ToBeReplaced you're changing the memory address that ToBeReplaced points to.



To your more specific issue, there are a couple of ways to handle this. The way that I would handle it would be to change the return from FindScapegoat to indicate whether the node is left or right. It seems like it's examining only the immediate children, so there's no need to return a reference to the node itself.



Something like this:



public enum Side {
LEFT,
RIGHT
}
//...
Side ToBeRebalanced = FindScapegoat(root, key); // Find scapegoat node
if (ToBeRebalanced == Side.Left){
int sizeRoot = TreeSize(root.left, 0);
root.left = RebuildTree(sizeRoot+1, root.left);
} else {
int sizeRoot = TreeSize(root.right, 0);
root.right = RebuildTree(sizeRoot+1, root.right);
}


You then could move the TreeSize call into the RebuildTree method to avoid the duplicated code.






share|improve this answer



















  • 1





    More or less led me in right path of solution. Since my tree is more than height 2 I had to do a bit more then just call root.left / root.right there. But I was able to use the general idea to have FindScapegoat return parent to actual scapegoat node, and then have my references be to the returned nodes children which fixed my issues. Thank you very much!

    – Kelith White
    Nov 26 '18 at 2:38
















0














To address the value/reference issue: non-primitive variables in Java behave (mostly, big caveat that's way out of scope for this answer) like pointers to memory locations. When you say ToBeReplaced you're changing the memory address that ToBeReplaced points to.



To your more specific issue, there are a couple of ways to handle this. The way that I would handle it would be to change the return from FindScapegoat to indicate whether the node is left or right. It seems like it's examining only the immediate children, so there's no need to return a reference to the node itself.



Something like this:



public enum Side {
LEFT,
RIGHT
}
//...
Side ToBeRebalanced = FindScapegoat(root, key); // Find scapegoat node
if (ToBeRebalanced == Side.Left){
int sizeRoot = TreeSize(root.left, 0);
root.left = RebuildTree(sizeRoot+1, root.left);
} else {
int sizeRoot = TreeSize(root.right, 0);
root.right = RebuildTree(sizeRoot+1, root.right);
}


You then could move the TreeSize call into the RebuildTree method to avoid the duplicated code.






share|improve this answer



















  • 1





    More or less led me in right path of solution. Since my tree is more than height 2 I had to do a bit more then just call root.left / root.right there. But I was able to use the general idea to have FindScapegoat return parent to actual scapegoat node, and then have my references be to the returned nodes children which fixed my issues. Thank you very much!

    – Kelith White
    Nov 26 '18 at 2:38














0












0








0







To address the value/reference issue: non-primitive variables in Java behave (mostly, big caveat that's way out of scope for this answer) like pointers to memory locations. When you say ToBeReplaced you're changing the memory address that ToBeReplaced points to.



To your more specific issue, there are a couple of ways to handle this. The way that I would handle it would be to change the return from FindScapegoat to indicate whether the node is left or right. It seems like it's examining only the immediate children, so there's no need to return a reference to the node itself.



Something like this:



public enum Side {
LEFT,
RIGHT
}
//...
Side ToBeRebalanced = FindScapegoat(root, key); // Find scapegoat node
if (ToBeRebalanced == Side.Left){
int sizeRoot = TreeSize(root.left, 0);
root.left = RebuildTree(sizeRoot+1, root.left);
} else {
int sizeRoot = TreeSize(root.right, 0);
root.right = RebuildTree(sizeRoot+1, root.right);
}


You then could move the TreeSize call into the RebuildTree method to avoid the duplicated code.






share|improve this answer













To address the value/reference issue: non-primitive variables in Java behave (mostly, big caveat that's way out of scope for this answer) like pointers to memory locations. When you say ToBeReplaced you're changing the memory address that ToBeReplaced points to.



To your more specific issue, there are a couple of ways to handle this. The way that I would handle it would be to change the return from FindScapegoat to indicate whether the node is left or right. It seems like it's examining only the immediate children, so there's no need to return a reference to the node itself.



Something like this:



public enum Side {
LEFT,
RIGHT
}
//...
Side ToBeRebalanced = FindScapegoat(root, key); // Find scapegoat node
if (ToBeRebalanced == Side.Left){
int sizeRoot = TreeSize(root.left, 0);
root.left = RebuildTree(sizeRoot+1, root.left);
} else {
int sizeRoot = TreeSize(root.right, 0);
root.right = RebuildTree(sizeRoot+1, root.right);
}


You then could move the TreeSize call into the RebuildTree method to avoid the duplicated code.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 26 '18 at 1:47









Chris ThompsonChris Thompson

29.1k96999




29.1k96999








  • 1





    More or less led me in right path of solution. Since my tree is more than height 2 I had to do a bit more then just call root.left / root.right there. But I was able to use the general idea to have FindScapegoat return parent to actual scapegoat node, and then have my references be to the returned nodes children which fixed my issues. Thank you very much!

    – Kelith White
    Nov 26 '18 at 2:38














  • 1





    More or less led me in right path of solution. Since my tree is more than height 2 I had to do a bit more then just call root.left / root.right there. But I was able to use the general idea to have FindScapegoat return parent to actual scapegoat node, and then have my references be to the returned nodes children which fixed my issues. Thank you very much!

    – Kelith White
    Nov 26 '18 at 2:38








1




1





More or less led me in right path of solution. Since my tree is more than height 2 I had to do a bit more then just call root.left / root.right there. But I was able to use the general idea to have FindScapegoat return parent to actual scapegoat node, and then have my references be to the returned nodes children which fixed my issues. Thank you very much!

– Kelith White
Nov 26 '18 at 2:38





More or less led me in right path of solution. Since my tree is more than height 2 I had to do a bit more then just call root.left / root.right there. But I was able to use the general idea to have FindScapegoat return parent to actual scapegoat node, and then have my references be to the returned nodes children which fixed my issues. Thank you very much!

– Kelith White
Nov 26 '18 at 2:38




















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53473710%2fissue-with-tree-node-reference-being-overwritten-in-java%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

Costa Masnaga

Fotorealismo

Create new schema in PostgreSQL using DBeaver