Skip to content
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
120 changes: 99 additions & 21 deletions binary_search_tree/tree.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from queue import Queue


class TreeNode:
def __init__(self, key, val = None):
if val == None:
Expand All @@ -14,42 +17,117 @@ class Tree:
def __init__(self):
self.root = None

# Time Complexity:
# Space Complexity:
# Time Complexity: O(log(n))
# Space Complexity: O(1)

Choose a reason for hiding this comment

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

🪐 Space complexity will actually be O(log(n)) here because of the recursive call stack

def add(self, key, value = None):

Choose a reason for hiding this comment

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

✨ Nice inner function

pass
def add_helper(node, key, value = None):
if key < node.key:
if node.left is None:
node.left = TreeNode(key, value)
return
else:
add_helper(node.left, key, value)
return
if key > node.key:
if node.right is None:
node.right = TreeNode(key, value)
return
else:
add_helper(node.right, key, value)
return

if self.root is None:
self.root = TreeNode(key, value)
return
add_helper(self.root, key, value)

# Time Complexity:
# Space Complexity:
# Time Complexity: O(log(n))
# Space Complexity: O(log(n))
def find(self, key):

Choose a reason for hiding this comment

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

pass
def find_helper(node, key):
if node is None:
return None
elif node.key == key:
return node.value
elif key < node.key:
return find_helper(node.left, key)
elif key > node.key:
return find_helper(node.right, key)

# Time Complexity:
# Space Complexity:
return find_helper(self.root, key)

# Time Complexity: O(n)
# Space Complexity: O(n)
def inorder(self):

Choose a reason for hiding this comment

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

pass
def inorder_helper(inorder_list, node):
if node is None:
return
inorder_helper(inorder_list, node.left)
inorder_list.append({"key": node.key, "value": node.value})
inorder_helper(inorder_list, node.right)

inorder_list = []
inorder_helper(inorder_list, self.root)
return inorder_list

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(n)
def preorder(self):

Choose a reason for hiding this comment

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

pass
def preorder_helper(inorder_list, node):
if node is None:
return
preorder_list.append({"key": node.key, "value": node.value})
preorder_helper(inorder_list, node.left)
preorder_helper(inorder_list, node.right)

# Time Complexity:
# Space Complexity:
preorder_list = []
preorder_helper(preorder_list, self.root)
return preorder_list

# Time Complexity: O(n)
# Space Complexity: O(n)
def postorder(self):

Choose a reason for hiding this comment

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

pass
def postorder_helper(inorder_list, node):
if node is None:
return
postorder_helper(inorder_list, node.left)
postorder_helper(inorder_list, node.right)
postorder_list.append({"key": node.key, "value": node.value})

postorder_list = []
postorder_helper(postorder_list, self.root)
return postorder_list

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(n)
def height(self):

Choose a reason for hiding this comment

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

pass
def height_helper(node):
if node is None:
return 0
return 1 + max(height_helper(node.left), height_helper(node.right))
return height_helper(self.root)


# # Optional Method
# # Time Complexity:
# # Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(n)
def bfs(self):

Choose a reason for hiding this comment

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

✨🌟 Excellent!

pass
if self.root is None:
return []


bfs_result = []
bfs_queue = Queue()
bfs_queue.put(self.root)
while not bfs_queue.empty():
node = bfs_queue.get()
bfs_result.append({"key": node.key, "value": node.value})
if node.left is not None:
bfs_queue.put(node.left)
if node.right is not None:
bfs_queue.put(node.right)

return bfs_result



Expand Down