views:

43

answers:

1

it would be nice if i could print the binary search trees i am writing onto python console ? any idea how to do it?

+2  A: 

You can use something like this:

def printTree(tree, depth = 0):
    if tree == None or len(tree) == 0:
        print "\t" * depth, "-"
    else:
        for key, val in tree.items():
            print "\t" * depth, key
            printTree(val, depth+1)

(Source: http://www.siafoo.net/snippet/91)

This method will yield:

n1
    n2
        n4
        n5
    n3
        n6
        n7

You can go along these lines and prettify as necessary.

Yuval A