Adam Chalkley wrote: I understand all that but this is the part that confuses me with this particular algorithm so the stack in memory for this program now has 3 methods on top of each other the main,then insert with the object value 8,then insert with null,so I don't get what happens next first the newly created object with value 9 is returned then,the object with the value 8 is returned,so if the object with the value 8 is returned wouldn't that mean that tree.right = the object with the value 8,in turn tree is returned and newNode is now altered so that means newNode.right equals the object with the value 8,but clearly that is wrong because if that was the case the tree wouldn't work and it does work when I use the search function so it has been implemented correctly but how wrong am I?
Henry Wong wrote:
Adam Chalkley wrote: I understand all that but this is the part that confuses me with this particular algorithm so the stack in memory for this program now has 3 methods on top of each other the main,then insert with the object value 8,then insert with null,so I don't get what happens next first the newly created object with value 9 is returned then,the object with the value 8 is returned,so if the object with the value 8 is returned wouldn't that mean that tree.right = the object with the value 8,in turn tree is returned and newNode is now altered so that means newNode.right equals the object with the value 8,but clearly that is wrong because if that was the case the tree wouldn't work and it does work when I use the search function so it has been implemented correctly but how wrong am I?
I think you may be overthinking it .... the main task of the return is just to backtrack (ie. to unroll the stack).
The only significant change is the node 9, which is assigned to the right reference of node 8, after it is returned. All the other returns are moot. They return the original value, that was passed in, and assigned to the original reference. This means that all of the other assignments do not change anything.
Henry
Adam Chalkley wrote: I understand that now but now return is called again and it returns tree,that stack is now taken off the stack frame,but now the next frame this is the one that is confusing me tree is returned from the previous stack frame so wouldn't tree.right = the returned value(which is tree,the original node that was passed in) then tree is returned again and the original node is returned to the main method?
Don't get me started about those stupid light bulbs. |