Skip to content

Instantly share code, notes, and snippets.

@mtholder
Created November 2, 2010 19:11
Show Gist options
  • Save mtholder/660123 to your computer and use it in GitHub Desktop.
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
#!/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