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
119 changes: 97 additions & 22 deletions binary_search_tree/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,44 +14,119 @@ class Tree:
def __init__(self):
self.root = None

# Time Complexity:
# Space Complexity:
# Time Complexity: O(log N) or O(n)
# Space Complexity: O(n)

Choose a reason for hiding this comment

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

Space complexity would be same as time complexity based on the recursive call stack: O(log n) for balanced trees and O(n) for unbalanced trees.


def insert_node_helper(self, current_node, key, value):
if current_node == None:
return TreeNode(key, value)

if key < current_node.key:
current_node.left = self.insert_node_helper(current_node.left, key, value)
else:
current_node.right = self.insert_node_helper(current_node.right, key, value)
return current_node

def add(self, key, value = None):
pass
# BST is empty
if self.root is None:
self.root = TreeNode(key, value)

# Time Complexity:
# Space Complexity:
# BST is not empty
self.insert_node_helper(self.root, key, value)

Choose a reason for hiding this comment

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

Without the else you add the root twice to the list. The tests for add don't fully cover this case, but it reveals itself with the remaining functions where you print out the tree or try and find the height because there's an extra node in the tree. Making this change should allow you to pass all the tests.

Suggested change
self.insert_node_helper(self.root, key, value)
else:
self.insert_node_helper(self.root, key, value)



# Time Complexity: O(log N) or O(n)
# Space Complexity: O(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.

✨ However space complexity would be O(1) here since you did this iteratively and aren't creating any additional data structures whose size is proportional to the size of the tree.

pass
if self.root == None:
return None
current = self.root
while current != None:
if current.key == key:
return current.value
elif current.key < key:
current = current.right
else:
current = current.left
return None


# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(n)
def inorder_helper(self, current, result):

Choose a reason for hiding this comment

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

if current is None:
return result
else:
self.inorder_helper(current.left, result)
result.append({'key': current.key, 'value': current.value})
self.inorder_helper(current.right, result)
return result

def inorder(self):
pass
result = []
# result_value = self.inorder_helper(self.root, result)
return self.inorder_helper(self.root, result)

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(n)
def preorder_helper(self, current, result):

Choose a reason for hiding this comment

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

if current:
result.append({"key": current.key, "value": current.value})
self.preorder_helper(current.left, result)
self.preorder_helper(current.right, result)
return result
def preorder(self):
pass
result = []
# self.preorder_helper(self.root, result)
return self.preorder_helper(self.root, result)

# Time Complexity:
# Space Complexity:

# Time Complexity: O(n)
# Space Complexity: O(n)
def postorder_helper(self, current, result):

Choose a reason for hiding this comment

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

if current:
self.postorder_helper(current.left, result)
self.postorder_helper(current.right, result)
result.append({"key": current.key, "value": current.value})
def postorder(self):
pass
result = []
self.postorder_helper(self.root, result)
return result

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(1)
def height_helper(self, current):

Choose a reason for hiding this comment

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

Because of the recursive call stack, space complexity would also be O(n) here

if current == None:
return 0
if current:
height_left = self.height_helper(current.left)
height_right = self.height_helper(current.right)
return max(height_left, height_right)+1
def height(self):
pass

return self.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.

pass
result = []


if self.root == None:
return result

queue = [self.root]
while queue:
current = queue.pop(0)
result.append({"key": current.key, "value": current.value})
if current.left:
queue.append(current.left)
if current.right:
queue.append(current.right)
return result


# # Useful for printing
Expand Down