Skip to content

Instantly share code, notes, and snippets.

@kachayev
Created October 26, 2013 09:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kachayev/7167046 to your computer and use it in GitHub Desktop.
Save kachayev/7167046 to your computer and use it in GitHub Desktop.
def heap(el=None):
return (el, None, None)
def union((el1, left1, right1), (el2, left2, right2)):
if el1 is None: return (el2, left2, right2)
if el2 is None: return (el1, left1, right1)
if el1 < el2:
return (el1, union((el2, left2, right2), right1), left1)
else:
return (el2, union((el1, left1, right1), right2), left2)
def insert(el, h):
return union(heap(el), h)
def extract((el, left, right)):
if el is None: return None, heap()
return el, union(left, right)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment