Skip to content

Spruce: Kaitlyn #52

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

Open
wants to merge 2 commits 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
129 changes: 110 additions & 19 deletions binary_search_tree/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,127 @@ class Tree:
def __init__(self):
self.root = None

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

Choose a reason for hiding this comment

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

🪐 You aren't creating any new data structures here or holding recursive calls in memory, so space complexity would be 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
node = TreeNode(key, value)
if self.root is None:
self.root = node
return self

current = self.root
while current:
if current.key > key:
# Go left
if current.left is None:
current.left = node
return self
else:
current = current.left
else:
if current.right is None:
current.right = node
return self
else:
current = current.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

# Time Complexity:
# Space Complexity:
if self.root is None:
return None
if self.root.key == key:
return self.root.value

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

# 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 iterative solution

pass
node_list = []
current = self.root
if current == None:
return node_list
while current:
node_list.insert(0, {"key": current.key, "value": current.value})
current = current.left
# reset
current = self.root.right
while current:
node_list.append({"key": current.key, "value": current.value})
current = current.right
return node_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
node_list = []
current = self.root
if current == None:
return node_list
while current:
node_list.append({"key": current.key, "value": current.value})
current = current.left

current = self.root.right
while current:
node_list.append({"key": current.key, "value": current.value})
current = current.right
return node_list

# 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
node_list = []
current = self.root
if current == None:
return node_list
self.postorder_helper(current, node_list)
return node_list

def postorder_helper(self, current, node_list):
if current:
self.postorder_helper(current.left, node_list)
self.postorder_helper(current.right, node_list)
node_list.append({"key": current.key, "value": current.value})
return node_list


# Time Complexity:
# Space Complexity:
# 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 is going to be O(n) here because your recursive calls will make a call for every node (every subtree) in the tree.

def height(self):

Choose a reason for hiding this comment

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

pass
current = self.root

if current is None:
return 0

return self.height_helper(current)

def height_helper(self, current):
if current is None:
return 0

left_height = self.height_helper(current.left)
right_height = self.height_helper(current.right)

return max(left_height, right_height) + 1


# # Optional Method
# # Time Complexity:
Expand Down