Consider I have object A which is holding reference to B object type, but this time initilized to null.
A->B( == null)
I want to replace the null with object of type B which hold reference to type C.
(B->C)
.
So I will get A->B->C
.
Why isn't it possible to link them by giving the reference of B object(which holds null but probably linked to specific memory address behind the scenes and assign to it the C object instead of null so afterward it will be A->B->C?
Why I must forward the A object for being able to do this manipulation?
This question is asked for trying to understand why in the following code: insertion of new node as a child of specific Node do not work. The code is :
public void InsertNodeToTreeLDR(TreeNode newNode)
{
var currRoot = Root;
InsertNodeToTreeHelper(ref currRoot, newNode);
}
private void InsertNodeToTreeHelper(ref TreeNode currTreeRoot, TreeNode newNode)
{
if (currTreeRoot == null)
{
currTreeRoot = newNode;
return;
}
else if (newNode.Data.CompareTo(currTreeRoot.Data) >= 0)
{
var currRootLeftChild = currTreeRoot.Leftchild;
InsertNodeToTreeHelper(ref currRootLeftChild, newNode);
}
else
{
var currRootRightChild = currTreeRoot.RightChild;
InsertNodeToTreeHelper(ref currRootRightChild, newNode);
}
}
Note:
I didn't want to include all code here so, this function is part of Tree Class which hold root of type TreeNode.
Think that you already have Tree with root with data == 2 (int), and want to add new left child as with data == 1.
In my implementation the linking between the Node and its child do not work.
You seem to be confused about what passing by reference does and how that interacts with reference types in C#. This code does nothing:
To see why it does nothing, let's step through this one line at a time.
This line defines a variable called
currRoot
that is then told to reference the sameTreeNode
that's referenced byRoot
. So nowcurrRoot
andRoot
are referring to the sameTreeNode
instance.Here we're passing the variable
currRoot
by reference. This means that theInsertNodeToTreeHelper
method is allowed to modify the value ofcurrRoot
. But remember that the value ofcurrRoot
is a reference to aTreeNode
, so if the value ofcurrRoot
is modified, you're simply telling that specific variable to point somewhere else, it won't do anything to the value ofRoot
(which is also a reference to someTreeNode
instance).If that's not clear, let me try to illustrate this with a simpler example:
This code example is really the same as just writing this:
Hopefully this makes it more clear why your pass by reference doesn't do anything.
Now, for your actual problem, as best I can guess, what you actually want is something like this:
This can also be done without recursion (which I would recommend, I think it would simplify the code immensely and remove the need for a helper method).