Created
November 2, 2010 19:11
-
-
Save mtholder/660123 to your computer and use it in GitHub Desktop.
produces a balanced tree with 2^{$1} leaves when $1 is the first command line argument
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
def balanced_tree(n, level): | |
if level == 0: | |
return n + 1, 't%d' % n | |
n, leftsub = balanced_tree(n, level - 1) | |
n, rightsub = balanced_tree(n, level - 1) | |
return n, '(%s,%s)' % (leftsub, rightsub) | |
if __name__ == '__main__': | |
import sys | |
print balanced_tree(1, int(sys.argv[1]))[1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment