[Tutor] Tree that doesn't grow
sigurd at 12move.de
Mon Mar 8 15:09:04 EST 2004
On 4 Mar 2004, Md. Zahar Othman <- zahar at upsi.edu.my wrote:
> I'm a newbie trying to convert my old Pascal tree program - pointer
> implementation to python OOP
And here is the problem: python has call by value semantics whereas
Pascal has IIRC call by reference or call by value (you can choose).
> Why the method does not work ?
Because of the different semantic.
> class binary_tree:
> def __init__(self):
> def insert (self,key):
> if self.tree:
> self.tree = node(key)
> def _insert (self,tree,key):
> if tree==None:
> tree = node(key)
In Pascal this could be a pointer or a reference to an address but in
Python this won't work that way.
You could change your code a bit:
def insert (self,key):
self.tree = node(key)
def _insert (self,tree,key):
if key < tree.key:
else: tree.left = node(key)
elif key > tree.key:
else: tree.right = node(key)
>>> p = binary_tree()
Please do *not* send copies of replies to me.
I read the list
More information about the Tutor