I have recently been studying binary search tree and their implementation in java. However my question is more related to obj. oriented programming more than data structures. One of the methods of the class binary tree is implemented as follow:
protected BinaryNode<AnyType> findMin( BinaryNode<AnyType> t )
{
//BinaryNode<AnyType> k= new BinaryNode<AnyType>(x);
if( t != null )
while( t.left != null )
{
t=t.left;
}
return t;
}
Now if instead of "t" i put the "root" the minimimum element of the binary tree is returned, but at the end doesn't this method alter the value of the "root"? Actually I know that it doesn't change it, but i don't understand why.
You don't want to change the actual value of root, that is why you use t. And call the method passing root. Root remain unchanged and t does all the work.