Skip to content
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

more simplified code #46

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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: 5 additions & 10 deletions BinaryTreeImplementation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ def __init__(self):
self.root = None

def insert(self, value):
if self.root is None:
if not self.root:
self.root = TreeNode(value)
else:
self._insert_recursively(self.root, value)

def _insert_recursively(self, node, value):
if value < node.value:
if node.left is None:
if not node.left:
node.left = TreeNode(value)
else:
self._insert_recursively(node.left, value)
else:
if node.right is None:
if not node.right:
node.right = TreeNode(value)
else:
self._insert_recursively(node.right, value)
Expand All @@ -47,13 +47,8 @@ def post_order_traversal(self, node):
# Example Usage
if __name__ == "__main__":
bt = BinaryTree()
bt.insert(5)
bt.insert(3)
bt.insert(7)
bt.insert(2)
bt.insert(4)
bt.insert(6)
bt.insert(8)
for value in [5, 3, 7, 2, 4, 6, 8]:
bt.insert(value)

print("In-order Traversal: ", end='')
bt.in_order_traversal(bt.root) # Output: 2 3 4 5 6 7 8
Expand Down