Skip to content

Updated tree traversal chapter #979

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 27, 2021
Merged
Changes from 2 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
35 changes: 12 additions & 23 deletions contents/tree_traversal/code/python/tree_traversal.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,31 +46,20 @@ def dfs_recursive_inorder_btree(node):


def dfs_stack(node):
stack = []
stack.append(node)

temp = None

while len(stack) > 0:
print(stack[-1].data, end=' ')
temp = stack.pop()

for child in temp.children:
stack.append(child)

stack = [node]
while stack:
node = stack.pop()
if node.children:
stack += node.children
print(node.data, end=' ')

def bfs_queue(node):
queue = []
queue.append(node)

temp = None

while len(queue) > 0:
print(queue[0].data, end=' ')
temp = queue.pop(0)

for child in temp.children:
queue.append(child)
queue = [node]
while queue:
node = queue.pop(0)
if node.children:
queue += node.children
print(node.data)


def main():
Expand Down