Created
January 15, 2017 06:45
-
-
Save drem-darios/9a9213ff7710037e1de0e44d740874d8 to your computer and use it in GitHub Desktop.
Breadth first traversal of a tree
This file contains 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
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