Created
November 7, 2012 12:19
-
-
Save alexsparrow/4031179 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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