Skip to content

Instantly share code, notes, and snippets.

@drem-darios
Created January 15, 2017 06:45
Show Gist options
  • Save drem-darios/9a9213ff7710037e1de0e44d740874d8 to your computer and use it in GitHub Desktop.
Save drem-darios/9a9213ff7710037e1de0e44d740874d8 to your computer and use it in GitHub Desktop.
Breadth first traversal of a tree
def bf_traversal(input):
queue = []
queue.append(0) # Add root
index = 0
while len(queue) > 0 and index < len(queue):
node = queue[index]
print input[node],
left = (index * 2) + 1
right = (index * 2) + 2
if left < len(input) and input[left] is not None:
queue.append(left)
if right < len(input) and input[right] is not None:
queue.append(right)
index +=1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment