Skip to content

Instantly share code, notes, and snippets.

@saml
Created April 24, 2013 21:16
Show Gist options
  • Save saml/5455652 to your computer and use it in GitHub Desktop.
Save saml/5455652 to your computer and use it in GitHub Desktop.
def pprint(d, level=0):
value = d.get('value', None)
left = d.get('left', None)
right = d.get('right', None)
if value is not None:
print('%s%s' % (' ' * level, value))
if left is not None:
pprint(left, level+4)
if right is not None:
pprint(right, level+4)
from pprint import pprint
def node(value, left=None, right=None):
d = {'value': value}
if left is not None:
d['left'] = left
if right is not None:
d['right'] = right
return d
def from_sorted(l, start, end):
middle = (start + end) / 2
print(start, middle, end)
if start == middle:
return node(l[middle])
return node(l[middle], from_sorted(l, start, middle), from_sorted(l, middle, end))
def build_tree(l):
if len(l) == 0:
return {}
return from_sorted(l, 0, len(l))
pprint(build_tree([1,2]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment