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

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

pass
cur = self.root
if cur == None:
self.root = TreeNode(key, value)
return
while True:
if key < cur.key:
if cur.left == None:
cur.left = TreeNode(key, value)
return
else:
cur = cur.left
else:
if cur.right == None:
cur.right = TreeNode(key, value)
return
else:
cur = cur.right


# Time Complexity:
# Space Complexity:

# Time Complexity: O(log n)
# Space Complexity: O(1)
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
cur = self.root
if cur == None:
return None
while cur != None and cur.key != key:
if key < cur.key:
cur = cur.left
else:
cur = cur.right
if cur == None:
return None
return cur.value

# Time Complexity:
# Space Complexity:
# 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.

✨ Nice inner function

pass
result = []
def inorder_helper(node):
if node == None:
return
if node.left != None:
inorder_helper(node.left)
result.append({
"key": node.key,
"value": node.value
})
if node.right != None:
inorder_helper(node.right)
inorder_helper(self.root)
return result




# 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
result = []
def preorder_helper(node):
if node == None:
return
result.append({
"key": node.key,
"value": node.value
})
if node.left != None:
preorder_helper(node.left)
if node.right != None:
preorder_helper(node.right)
preorder_helper(self.root)
return result

# Time Complexity:
# Space Complexity:
# 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

# Time Complexity:
# Space Complexity:
result = []
def postorder_helper(node):
if node == None:
return
if node.left != None:
postorder_helper(node.left)
if node.right != None:
postorder_helper(node.right)
result.append({
"key": node.key,
"value": node.value
})
postorder_helper(self.root)
return result

# Time Complexity: O(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 be O(n) because you make a recursive call on each subtree (each node can be considered a subtree)

def height(self):
pass

if self.root == None:
return 0
def height_helper(node, height):
if node == None:
return
left_best = -1
right_best = -1
if node.left != None:
left_best = height_helper(node.left, height + 1)
if node.right != None:
right_best = height_helper(node.right, height + 1)
return max([left_best, right_best, height])
return height_helper(self.root, 1)

# # 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.

🌟 Very nice!

pass
cur = self.root
if cur == None:
return []

res = [{
"key": self.root.key,
"value": self.root.value
}]
parents = [self.root]

while len(parents) > 0:
new_parents = []
for parent in parents:
if parent.left != None:
res.append({
"key": parent.left.key,
"value": parent.left.value
})
new_parents.append(parent.left)
if parent.right != None:
res.append({
"key": parent.right.key,
"value": parent.right.value

})
new_parents.append(parent.right)
parents = new_parents
return res





Expand Down