Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from data_structures_and_algorithms.data_structures.stacks_and_queues.stacks_and_queues import Queue Node


def breadth_first_traversal(tree, action=print):
if tree.root is None:
return False
action(tree.root.val)
qu = Queue()
if tree.root.left:
qu.enqueue(tree.root.left)
if tree.root.right:
qu.enqueue(tree.root.right)
top = qu.front
while top:
node = top.val
action(node.val)
if node.left:
qu.enqueue(node.left)
if node.right:
qu.enqueue(node.right)
top = top.next



29 changes: 29 additions & 0 deletions data_structures_and_algorithms/challenges/BinaryTree/read.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# code challenge 16


# code challenge 17

Write a breadth first traversal method which takes a Binary Tree as its unique input.
Without utilizing any of the built-in methods available to your language,
traverse the input tree using a Breadth-first approach,
and return a list of the values in the tree in the order they were encountered.

# Algorithm
1- visit the root of the tree and print it

2- if the root have childes print the childe on the left then on the right

3- go to the level below and repeat the above for each node
1
2 3
9 5 6 7


# Beusedo code

input :🌴tree
output : elemnt of tree

# Big O🌴 :
O(n)
helping link :[https://www.educative.io/edpresso/how-to-implement-a-breadth-first-search-in-python]
Loading