I'm coding a decision tree and I decided a nice implementation would be a Class/Object structure of the branches (at least in C++). For the sake of simplicity I'm gonna break it down to the necessary parts:
class DecisionTreeBranch:
def __init__(self):
parent = None
leftBranch = None
rightBranch = None
where parent
declares the way to the super decisions and leftBranch
, rightBranch
defines the decisions below.
What I'm trying to do:
Now going downwards ain't a problem, but moving upwards: Let's say this tree is defined by the left branches first, so if I end up in a final leaf. Im going one step above and take the right branch and continue with the tree building (if I go above, right and left is defined, I'm going another step above, and so on).
The aim is to iterate through the tree by the three parameters: parent
(up) and leftBranch
, rightBranch
(down)
The problem
If I'd take e.g. C++ then this wouldn't be a problem, I'd define parent with a call-by-reference. But I recently started programming classes in Python, unfortunately I always end up in a call-by-value expression, where for sure all the information of the above branches are lost.
Basically I'm looking for something like:
[...]
# define way to former branch
&curDecision.parent = &oldDecision
oldDecision = curDecision
# continue with the next/new decision on the current level
curDecision = DecisionTreeBranch()
[...]
Can anyone help me out ?
Kind Regards