-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
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
Changes from 9 commits
2e49da2
7ca8aa7
f14a543
722f3a0
61aedb3
699b8f1
43f2849
ff71a5b
92ae8d6
7a93e2d
2380410
98b78e6
89568dd
761d38f
bc286bf
8808ece
517b0e4
3be7962
ed23746
91a6a1a
48c43e8
be90c94
664bc26
daed8d5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
""" | ||
Illustrate how to implement inorder traversal in binary search tree. | ||
Author: Gurneet Singh | ||
https://www.geeksforgeeks.org/tree-traversals-inorder-preorder-and-postorder/ | ||
""" | ||
|
||
|
||
class BinaryTreeNode: | ||
"""Defining the structure of BinaryTreeNode""" | ||
|
||
def __init__(self, data: int) -> None: | ||
self.data = data | ||
self.left_child = None | ||
self.right_child = None | ||
|
||
|
||
def insert(node: BinaryTreeNode, new_value: int) -> BinaryTreeNode: | ||
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. As there is no test file in this pull request nor any test function or class in the file 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. As there is no test file in this pull request nor any test function or class in the file 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. As there is no test file in this pull request nor any test function or class in the file 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. As there is no test file in this pull request nor any test function or class in the file |
||
# if binary search tree is empty, make a new node | ||
# and declare it as root | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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) -> BinaryTreeNode: # if node is None,return | ||
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. As there is no test file in this pull request nor any test function or class in the file 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. As there is no test file in this pull request nor any test function or class in the file |
||
""" | ||
>>> inorder(make()) | ||
6 | ||
10 | ||
14 | ||
15 | ||
20 | ||
25 | ||
60 | ||
""" | ||
|
||
if node is None: | ||
return None | ||
# 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) | ||
|
||
|
||
def make_tree() -> BinaryTreeNode | None: | ||
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. As there is no test file in this pull request nor any test function or class in the file 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. As there is no test file in this pull request nor any test function or class in the file 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. As there is no test file in this pull request nor any test function or class in the file 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. As there is no test file in this pull request nor any test function or class in the file 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. As there is no test file in this pull request nor any test function or class in the file 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. As there is no test file in this pull request nor any test function or class in the file 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. As there is no test file in this pull request nor any test function or class in the file 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. As there is no test file in this pull request nor any test function or class in the file 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. As there is no test file in this pull request nor any test function or class in the file 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. As there is no test file in this pull request nor any test function or class in the file 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. As there is no test file in this pull request nor any test function or class in the file 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. As there is no test file in this pull request nor any test function or class in the file 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. As there is no test file in this pull request nor any test function or class in the file |
||
|
||
root = insert(None, 15) | ||
insert(root, 10) | ||
insert(root, 25) | ||
insert(root, 6) | ||
insert(root, 14) | ||
insert(root, 20) | ||
insert(root, 60) | ||
return root | ||
|
||
|
||
def main(): | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# main function | ||
root = make_tree() | ||
print("Printing values of binary search tree in Inorder Traversal.") | ||
inorder(root) | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() | ||
main() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file
data_structures/binary_tree/inorder_tree_traversal_2022.py
, please provide doctest for the functioninsert
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://docs.python.org/3/library/doctest.html