[Tutor] Binary Tree Printing Woes

Alan Gauld alan.gauld@blueyonder.co.uk
Thu Jun 5 19:22:35 2003


> end. It works beautifully in the python shell, just not when I run
the
> app in my term.

I think you ate running the unctins in the shell,
not printing them....

>     print "Printing  A:", t.print_tree(a)
>     print "PreOrder  A:", t.print_preorder(a)
>     print "PostOrder A:", t.print_postorder(a)

Here you print the string then the return value from
the print functions. But the print functiond=s do their
own printing and don't retirn anything. When a function
doesn't explicitly return anything Python provides a
default of None.

Thus:

>>> def f(): pass
...
>>> print f()
None
>>>

You are seeing the None coming back from the functions.

> Bizarre, innit?

Not really :-)

> Oh, and while we're at it: how do you find the parent of a given
node?

You need to store a reference in each child(as Tkinter does
with its widgets) or alternatively wroite a function that
searches the tree for a node which has your node as a child!
I recommend storing the parent as an attribute of the Node!

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld