May be I am just confused over nothing but here goes:
There are many cases that we have a recursive algorithm that values that we need are modified as we move from one recursion to the next.
Examples could be min
max
maxNoOfNodes
etc.
In C++ one can pass the various parameters as references and all works ok.
In Java though this can not work since everything is copied by value and so one must create an extra class for the argument as a holder in order to modify it inside a function.
So this in C++:
int findLargestSeq(Tree *p, int &min, int &max,Tree *& seqTree)
could not be "translated" as is in Java but would have to be like:
int findLargestSeq(Tree p, Params p)
where Params
would encapsulate the min
max
etc to be updated.
I was wondering is this the only way to go?
Is there a more "clean" approach or standard pattern in Java for this kind of algorithms?
I am thinking that perhaps the fact that we modify the arguments that are passed as reference in C++ is a habbit left-over from C programming while in Java which is pure OO I am also stuck thinking this in this "procedural" manner and can not see that I should somehow deal with these kind of problems differently.
Any input is highly welcome