[Tutor] Tree that doesn't grow
Danny Yoo
dyoo at hkn.eecs.berkeley.edu
Mon Mar 8 14:09:23 EST 2004
On Thu, 4 Mar 2004, Md. Zahar Othman wrote:
> class binary_tree:
>
> def __init__(self):
> self.tree=None
>
> def insert (self,key):
> if self.tree:
> self._insert(self.tree,key)
> else:
> self.tree = node(key)
>
> def _insert (self,tree,key):
> if tree == None:
> tree = node(key)
> elif key < tree.key:
> self._insert(tree.left,key)
> elif key > tree.key:
> self._insert(tree.right,key)
> Why the method does not work ?
Hi Zahar,
Let's take a detour for a moment. Take a look at the following code:
###
def makeListEmpty(L):
"""Tries to empty out L. Buggy."""
L = []
L = [1, 2, 3, 4, 5]
makeListEmpty(L)
print L
###
What do you expect to see here? Does it do what you expect?
If you understand why this 'makeListEmpty()' function doesn't work, then
that should help make it easier to see why binary_tree._insert() isn't
having the effect you want.
Please feel free to ask questions. Good luck to you!
More information about the Tutor
mailing list