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

Spruce - Angela Fan #56

Open
wants to merge 1 commit into
base: master
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
139 changes: 117 additions & 22 deletions binary_search_tree/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,44 +14,139 @@ class Tree:
def __init__(self):
self.root = None

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

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(1) here because you aren't creating a call stack or any new data structures that vary with the size of the input (the size of your tree)

def add(self, key, value = None):
pass
if self.root == None:
self.root = TreeNode(key, value)
return
current = self.root
while current != None:
if current.key == key:
current.value = value
return
elif current.key > key:
if current.left == None:
current.left = TreeNode(key, value)
return
current = current.left
else:
if current.right == None:
current.right = TreeNode(key, value)
return
current = current.right


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

Choose a reason for hiding this comment

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

🪐⬆️ Same thing as add with the space complexity here

def find(self, key):
pass

# Time Complexity:
# Space Complexity:
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.left
else:
current = current.right
return None

def inorder_helper(self, current_node, items):
if current_node is None:
return []

self.inorder_helper(current_node.left, items)
items.append({"key": current_node.key, "value": current_node.value})
self.inorder_helper(current_node.right, items)


# 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

# Time Complexity:
# Space Complexity:
if self.root == None:
return []

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

def preorder_helper(self, current_node, items):
if current_node is None:
return []

items.append({"key": current_node.key, "value": current_node.value})
self.preorder_helper(current_node.left, items)
self.preorder_helper(current_node.right, items)

# Time Complexity: O(log n)
# Space Complexity: O(log 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
if self.root == None:
return []

items = []
self.preorder_helper(self.root, items)
return items

# Time Complexity:
# Space Complexity:
def postorder_helper(self, current_node, items):
if current_node is None:
return []

self.postorder_helper(current_node.left, items)
self.postorder_helper(current_node.right, items)
items.append({"key": current_node.key, "value": current_node.value})


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

Choose a reason for hiding this comment

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

⏱🪐 This is going to be O(n) space complexity like preorder and inorder because you are creating a list of all n nodes in the tree and to do that you are creating a recursive call for each of the n nodes.

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

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


def height_helper(self, current_node):
if current_node is None:
return 0
left_height = self.height_helper(current_node.left)
right_height = self.height_helper(current_node.right)
return max(left_height, right_height) + 1

# Time Complexity:
# Space Complexity:

# Time Complexity: O(log n)
# Space Complexity: O(log n)
Comment on lines +123 to +124

Choose a reason for hiding this comment

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

⏱🪐 Time and space complexity is going to be O(log n) because you recursively call height_helper on each node in the list.

def height(self):
pass
if self.root == None:
return 0

return self.height_helper(self.root)


def bfs_helper(self, current_node, items):
if current_node is None:
return []

items.append({"key": current_node.key, "value": current_node.value})
self.bfs_helper(current_node.left, items)
self.bfs_helper(current_node.right, items)

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

With breadth first search you want to traverse the tree starting from its root, then visit each of it's children (from left to right), before visiting the children's children and so forth. In other words, you want to traverse the tree level by level.

With your bfs_helper, you are adding the root node of the current tree, but then your recursive call through the left subtree will finish before you ever look at the root of the right subtree.

(Hint: this may be easier to think about iteratively)

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


items = []
self.bfs_helper(self.root, items)
return items


# # Useful for printing
Expand Down