-
-
Notifications
You must be signed in to change notification settings - Fork 47.1k
add binary_tree_traversals.py to data_structures #3297
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
cclauss
merged 31 commits into
TheAlgorithms:master
from
rajansh87:add_binary_tree_traversals
Oct 15, 2020
Merged
Changes from 2 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
90b8c32
add binary_tree_traversals.py to data_structures
rajansh87 6193d6e
Fixed error
rajansh87 5b6f8d8
Update data_structures/binary_tree/binary_tree_traversals.py
rajansh87 f15601c
Update binary_tree_traversals.py
rajansh87 b5285c1
Update binary_tree_traversals.py
rajansh87 2c9050c
Update binary_tree_traversals.py
rajansh87 4140522
Update data_structures/binary_tree/binary_tree_traversals.py
rajansh87 85738c3
Update binary_tree_traversals.py
rajansh87 27d854f
Update binary_tree_traversals.py
rajansh87 5f4e7ce
Update binary_tree_traversals.py
rajansh87 854fbd5
Update binary_tree_traversals.py
rajansh87 c0dc50d
Update binary_tree_traversals.py
rajansh87 48e97aa
Update binary_tree_traversals.py
rajansh87 cf0f9e4
Update binary_tree_traversals.py
rajansh87 6c43555
Update binary_tree_traversals.py
rajansh87 449f686
Update binary_tree_traversals.py
rajansh87 5dc5534
Update binary_tree_traversals.py
rajansh87 655657d
Update data_structures/binary_tree/binary_tree_traversals.py
rajansh87 d5a0dba
Update binary_tree_traversals.py
rajansh87 cd5c08c
Update binary_tree_traversals.py
rajansh87 b266c71
Update data_structures/binary_tree/binary_tree_traversals.py
rajansh87 fe91a5a
Update data_structures/binary_tree/binary_tree_traversals.py
rajansh87 ef6871e
Update binary_tree_traversals.py
rajansh87 f1c3bfe
Update data_structures/binary_tree/binary_tree_traversals.py
rajansh87 6ae25e6
Update binary_tree_traversals.py
rajansh87 41a7dc1
Doctests and type hints
cclauss baa9eab
Add spaces
cclauss ce35469
Update binary_tree_traversals.py
rajansh87 15c812d
black exclude data_structures/binary_tree/binary_tree_traversals.py
cclauss 80409c5
Add spaces again
cclauss b7ea116
Update binary_tree_traversals.py
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
class Node: | ||
""" | ||
A Node has data variable and pointers toits left and right nodes. | ||
""" | ||
def __init__(self,data): | ||
self.left=None | ||
self.right=None | ||
self.data=data | ||
|
||
def preorder(root): | ||
""" | ||
PreOrder traversal: visit root node then its left subtree followed by right subtree. | ||
""" | ||
rajansh87 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if(root!=None): | ||
rajansh87 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
print(root.data,end=" ") | ||
rajansh87 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
preorder(root.left) | ||
preorder(root.right) | ||
|
||
def postorder(root): | ||
""" | ||
PostOrder traversal: visit left subtree followed by right subtree and then root node. | ||
""" | ||
if(root!=None): | ||
postorder(root.left) | ||
postorder(root.right) | ||
print(root.data,end=" ") | ||
|
||
def inorder(root): | ||
""" | ||
InOrder traversal: visit its left subtree followed by root node and then right subtree. | ||
""" | ||
if(root!=None): | ||
inorder(root.left) | ||
print(root.data,end=" ") | ||
inorder(root.right) | ||
|
||
def height(root): | ||
""" | ||
Recursive function for calculating height of the binary tree. | ||
""" | ||
if(root==None): | ||
return 0 | ||
leftHeight=height(root.left) | ||
rightHeight=height(root.right) | ||
if leftHeight>rightHeight: | ||
return leftHeight+1 | ||
else: | ||
return rightHeight+1 | ||
rajansh87 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def levelorder1(root): | ||
""" | ||
Print whole binary tree in Level Order Traverse. | ||
Level Order traverse: Visit nodes of the tree level-by-level. | ||
""" | ||
if root==None: | ||
return | ||
temp=root | ||
que=[temp] | ||
while(len(que)>0): | ||
print(que[0].data,end=" ") | ||
temp=que.pop(0) | ||
if temp.left!=None: | ||
que.append(temp.left) | ||
if temp.right!=None: | ||
que.append(temp.right) | ||
|
||
def levelorder2(root,level): | ||
""" | ||
Level-wise traversal: | ||
Print all nodes present at the given level of the binary tree. | ||
""" | ||
if root==None: | ||
rajansh87 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return root | ||
if level==1: | ||
print(root.data,end=" ") | ||
elif level>1: | ||
levelorder2(root.left,level-1) | ||
levelorder2(root.right,level-1) | ||
|
||
def printlefttoright(root,level): | ||
""" | ||
Print elements on particular level from left to right direction of the binary tree. | ||
""" | ||
if root==None: | ||
return | ||
if level==1: | ||
print(root.data,end=" ") | ||
elif level>1: | ||
printlefttoright(root.left,level-1) | ||
printlefttoright(root.right,level-1) | ||
|
||
def printrighttoleft(root,level): | ||
rajansh87 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Print elements on particular level from right to left direction of the binary tree. | ||
""" | ||
if root==None: | ||
return | ||
if level==1: | ||
print(root.data,end=" ") | ||
elif level>1: | ||
printrighttoleft(root.right,level-1) | ||
printrighttoleft(root.left,level-1) | ||
|
||
def zigzag(root): | ||
""" | ||
ZigZag traverse: Print node left to right and right to left, alternatively. | ||
""" | ||
flag=0 | ||
height_tree=height(root) | ||
for h in range(1,height_tree+1): | ||
if flag==0: | ||
printlefttoright(root,h) | ||
flag=1 | ||
else: | ||
printrighttoleft(root,h) | ||
flag=0 | ||
|
||
def main(): # Main function for testing. | ||
""" | ||
Create binary tree. | ||
""" | ||
root=Node(1) | ||
root.left=Node(2) | ||
root.right=Node(3) | ||
root.left.left=Node(4) | ||
root.left.right=Node(5) | ||
|
||
""" | ||
All Traversals of the binary are as follows: | ||
""" | ||
print("In order Traversal is : ") | ||
inorder(root) | ||
print("\nPre order Traversal is : ") | ||
preorder(root) | ||
print("\nPost order Traversal is : ") | ||
postorder(root) | ||
print("\nHeight of Tree is : ") | ||
height_tree=height(root) | ||
print(height_tree) | ||
print("\nComplete Level Order Traversal is : ") | ||
levelorder1(root) | ||
print("\nLevel-wise order Traversal is : ") | ||
for h in range(1,height_tree+1): | ||
levelorder2(root,h) | ||
print("\nZigZag order Traversal is : ") | ||
zigzag(root) | ||
|
||
if __name__ == "__main__": | ||
main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.