Skip to content

Small Corrections for Tree Traversal in Python #337

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 1 commit into from
Aug 5, 2018
Merged
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
15 changes: 11 additions & 4 deletions contents/tree_traversal/code/python/Tree_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def DFS_recursive(node):

def DFS_recursive_postorder(node):
for child in node.children:
DFS_recursive(child)
DFS_recursive_postorder(child)

if node.data != None:
print(node.data)
Expand All @@ -34,11 +34,11 @@ def DFS_recursive_postorder(node):
# This assumes only 2 children, but accounts for other possibilities
def DFS_recursive_inorder_btree(node):
if (len(node.children) == 2):
DFS_recursive_inorder_btree(node.children[1])
DFS_recursive_inorder_btree(node.children[0])
print(node.data)
DFS_recursive_inorder_btree(node.children[2])
elif (len(node.children) == 1):
DFS_recursive_inorder_btree(node.children[1])
elif (len(node.children) == 1):
DFS_recursive_inorder_btree(node.children[0])
print(node.data)
elif (len(node.children) == 0):
print(node.data)
Expand Down Expand Up @@ -80,12 +80,19 @@ def main():
print("Recursive:")
DFS_recursive(tree)

print("Recursive Postorder:")
DFS_recursive_postorder(tree)

print("Stack:")
DFS_stack(tree)

print("Queue:")
BFS_queue(tree)

binaryTree = create_tree(Node(), 3, 2)

print("Recursive Inorder Binary Tree:")
DFS_recursive_inorder_btree(binaryTree)

if __name__ == '__main__':
main()
Expand Down