Skip to content

Commit 7862cc3

Browse files
committed
Depth first search operational
1 parent 93ead3b commit 7862cc3

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

search/binary_search_tree.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,20 @@ def __init__(self, value):
55
self.right = None
66

77
def depth_first_for_each(self, cb):
8+
s = Queue()
9+
s.enqueue(self.value)
10+
811
def rec(current_node):
12+
cb(s.dequeue())
913
if current_node.left is None and current_node.right is None:
10-
return current_node.value
11-
if current_node.left is not None:
12-
cb(current_node.left.value)
13-
if current_node.right is not None:
14-
cb(current_node.right.value)
15-
14+
return
1615
if current_node.left is not None:
16+
s.enqueue(current_node.left.value)
1717
rec(current_node.left)
1818
if current_node.right is not None:
19+
s.enqueue(current_node.right.value)
1920
rec(current_node.right)
21+
2022
rec(self)
2123

2224
def breadth_first_for_each(self, cb):

0 commit comments

Comments
 (0)