Skip to content

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 24 commits into from
Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
2e49da2
Binary Search Tree Inorder Traversal
SinghGurneet21 Oct 8, 2022
7ca8aa7
updating DIRECTORY.md
Oct 8, 2022
f14a543
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 8, 2022
722f3a0
Binary Search Tree Inorder Traversal v2
SinghGurneet21 Oct 8, 2022
61aedb3
Binary Search Tree Inorder Traversal
SinghGurneet21 Oct 8, 2022
699b8f1
Binary Search Tree Inorder Traversal
SinghGurneet21 Oct 8, 2022
43f2849
Binary Search Tree Inorder Traversal
SinghGurneet21 Oct 8, 2022
ff71a5b
Update inorder_tree_traversal_2022.py
SinghGurneet21 Oct 13, 2022
92ae8d6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 13, 2022
7a93e2d
Update inorder_tree_traversal_2022.py
SinghGurneet21 Oct 13, 2022
2380410
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 13, 2022
98b78e6
Update inorder_tree_traversal_2022.py
SinghGurneet21 Oct 13, 2022
89568dd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 13, 2022
761d38f
Update data_structures/binary_tree/inorder_tree_traversal_2022.py
cclauss Oct 13, 2022
bc286bf
Update data_structures/binary_tree/inorder_tree_traversal_2022.py
cclauss Oct 13, 2022
8808ece
Updated
SinghGurneet21 Oct 13, 2022
517b0e4
Updated
SinghGurneet21 Oct 13, 2022
3be7962
Update inorder_tree_traversal_2022.py
SinghGurneet21 Oct 13, 2022
ed23746
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 13, 2022
91a6a1a
Update inorder_tree_traversal_2022.py
SinghGurneet21 Oct 13, 2022
48c43e8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 13, 2022
be90c94
Update data_structures/binary_tree/inorder_tree_traversal_2022.py
SinghGurneet21 Oct 13, 2022
664bc26
Updated and removed print statement
SinghGurneet21 Oct 13, 2022
daed8d5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 13, 2022
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
4 changes: 3 additions & 1 deletion DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@
* [Binary Tree Mirror](data_structures/binary_tree/binary_tree_mirror.py)
* [Binary Tree Traversals](data_structures/binary_tree/binary_tree_traversals.py)
* [Fenwick Tree](data_structures/binary_tree/fenwick_tree.py)
* [Inorder Tree Traversal 2022](data_structures/binary_tree/inorder_tree_traversal_2022.py)
* [Lazy Segment Tree](data_structures/binary_tree/lazy_segment_tree.py)
* [Lowest Common Ancestor](data_structures/binary_tree/lowest_common_ancestor.py)
* [Maximum Fenwick Tree](data_structures/binary_tree/maximum_fenwick_tree.py)
Expand Down Expand Up @@ -632,8 +633,9 @@

## Physics
* [Horizontal Projectile Motion](physics/horizontal_projectile_motion.py)
* [Lorenz Transformation Four Vector](physics/lorenz_transformation_four_vector.py)
* [Lorentz Transformation Four Vector](physics/lorentz_transformation_four_vector.py)
* [N Body Simulation](physics/n_body_simulation.py)
* [Newtons Law Of Gravitation](physics/newtons_law_of_gravitation.py)
* [Newtons Second Law Of Motion](physics/newtons_second_law_of_motion.py)

## Project Euler
Expand Down
93 changes: 93 additions & 0 deletions data_structures/binary_tree/inorder_tree_traversal_2022.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
"""
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 | None, new_value: int) -> BinaryTreeNode | None:
"""
If the binary search tree is empty, make a new node and declare it as root.
>>> node_a = BinaryTreeNode(12345)
>>> node_b = insert(node_a, 67890)
>>> node_a.left_child == node_b.left_child
True
>>> node_a.right_child == node_b.right_child
True
>>> node_a.data == node_b.data
True
"""
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,
) -> BinaryTreeNode | None: # if node is None,return
"""
>>> inorder(make_tree())
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According the CONTRIBUTING.md, algorithmic functions should not print(). See our other tree algorithms for ways to avoid printing in the function.

# traverse right subtree
inorder(node.right_child)


def make_tree() -> BinaryTreeNode | None:

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 function make_tree

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 function make_tree

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 function make_tree

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 function make_tree

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 function make_tree

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 function make_tree

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 function make_tree

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 function make_tree

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 function make_tree

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 function make_tree

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 function make_tree

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 function make_tree

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 function make_tree


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() -> None:

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 function main

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 function main

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 function main

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 function main

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 function main

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 function main

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 function main

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 function main

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 function main

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 function main

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 function main

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 function main

# 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()