-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Binary Search Tree Inorder Traversal Algorithm #6840
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
Changes from 3 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
2e49da2
Binary Search Tree Inorder Traversal
SinghGurneet21 7ca8aa7
updating DIRECTORY.md
f14a543
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 722f3a0
Binary Search Tree Inorder Traversal v2
SinghGurneet21 61aedb3
Binary Search Tree Inorder Traversal
SinghGurneet21 699b8f1
Binary Search Tree Inorder Traversal
SinghGurneet21 43f2849
Binary Search Tree Inorder Traversal
SinghGurneet21 ff71a5b
Update inorder_tree_traversal_2022.py
SinghGurneet21 92ae8d6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 7a93e2d
Update inorder_tree_traversal_2022.py
SinghGurneet21 2380410
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 98b78e6
Update inorder_tree_traversal_2022.py
SinghGurneet21 89568dd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 761d38f
Update data_structures/binary_tree/inorder_tree_traversal_2022.py
cclauss bc286bf
Update data_structures/binary_tree/inorder_tree_traversal_2022.py
cclauss 8808ece
Updated
SinghGurneet21 517b0e4
Updated
SinghGurneet21 3be7962
Update inorder_tree_traversal_2022.py
SinghGurneet21 ed23746
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 91a6a1a
Update inorder_tree_traversal_2022.py
SinghGurneet21 48c43e8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] be90c94
Update data_structures/binary_tree/inorder_tree_traversal_2022.py
SinghGurneet21 664bc26
Updated and removed print statement
SinghGurneet21 daed8d5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 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
77 changes: 77 additions & 0 deletions
77
data_structures/binary_tree/inorder_tree_traversal_2022.py
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,77 @@ | ||
""" | ||
Illustrate how to implement inorder traversal in binary search tree. | ||
Author: Gurneet Singh | ||
https://www.geeksforgeeks.org/tree-traversals-inorder-preorder-and-postorder/ | ||
""" | ||
|
||
""" | ||
5 items had no tests: | ||
__main__ | ||
__main__.BinaryTreeNode | ||
__main__.BinaryTreeNode.__init__ | ||
__main__.inorder | ||
__main__.insert | ||
0 tests in 5 items. | ||
0 passed and 0 failed. | ||
Test passed. | ||
Printing values of binary search tree in Inorder Traversal. | ||
6 | ||
10 | ||
14 | ||
15 | ||
20 | ||
25 | ||
60 | ||
""" | ||
|
||
|
||
class BinaryTreeNode: | ||
"""Defining the structure of BinaryTreeNode""" | ||
|
||
def __init__(self, data: int) -> None: | ||
self.data = data | ||
self.left_Child = None | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.right_Child = None | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
SinghGurneet21 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def insert( | ||
SinghGurneet21 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
node: None, new_Value: int | ||
SinghGurneet21 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) -> BinaryTreeNode: # if binary search tree is empty, make a new node and declare it as root | ||
if node is None: | ||
node = BinaryTreeNode(new_Value) | ||
return node | ||
|
||
# binary search tree is not empty, so we will insert it into the tree | ||
# if new_Value is less than value of data in node, add it to left subtree and proceed recursively | ||
if new_Value < node.data: | ||
node.left_Child = insert(node.left_Child, new_Value) | ||
else: | ||
# if new_Value is greater than value of data in node, add it to right subtree and proceed recursively | ||
node.right_Child = insert(node.right_Child, new_Value) | ||
return node | ||
|
||
|
||
def inorder(node: None) -> None: # if node is None,return | ||
SinghGurneet21 marked this conversation as resolved.
Show resolved
Hide resolved
SinghGurneet21 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if node == None: | ||
return | ||
# traverse left subtree | ||
inorder(node.left_Child) | ||
# traverse current node | ||
print(node.data) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According the |
||
# traverse right subtree | ||
inorder(node.right_Child) | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() | ||
root = insert(None, 15) | ||
insert(root, 10) | ||
insert(root, 25) | ||
insert(root, 6) | ||
insert(root, 14) | ||
insert(root, 20) | ||
insert(root, 60) | ||
print("Printing values of binary search tree in Inorder Traversal.") | ||
inorder(root) |
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.