Skip to content

Instantly share code, notes, and snippets.

@alexsparrow
Created November 7, 2012 12:19
Show Gist options
  • Save alexsparrow/4031179 to your computer and use it in GitHub Desktop.
Save alexsparrow/4031179 to your computer and use it in GitHub Desktop.
class Node:
def __init__(self, name, left, right):
self.name = name
self.left = left
self.right = right
class Tree:
def __init__(self):
self.root = Node('1', Node('2', Node('4', None, None), Node('5', None, None)), Node('3', Node('6', None, None), Node('7', None, None)))
def walk_breadth(self,f):
q = []
cur = self.root
while cur:
f(cur)
if cur.left: q.append(cur.left)
if cur.right: q.append(cur.right)
if len(q):
cur = q[0]
del q[0]
else: cur = None
if __name__ == "__main__":
t = Tree()
def sz(x):
print x.name
t.walk_breadth(sz)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment