Skip to content

Rock: Libby Gerber #47

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 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
109 changes: 82 additions & 27 deletions binary_search_tree/tree.py
Original file line number Diff line number Diff line change
@@ -1,59 +1,114 @@
class TreeNode:
def __init__(self, key, val = None):
def __init__(self, key, val=None):
if val == None:
val = key

self.key = key
self.value = val
self.left = None
self.right = None



class Tree:
def __init__(self):
self.root = None

# Time Complexity:
# Space Complexity:
# Time Complexity: O(log n)
# Space Complexity: O(1)
def add_helper(self, current, key, value):
Comment on lines +14 to +16

Choose a reason for hiding this comment

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

👍 Since this is recursive the space complexity is O(log n) assuming the tree is balanced and O(n) otherwise.

if current is None:
return TreeNode(key, value)
if current.key >= key:
current.left = self.add_helper(current.left, key, value)
else:
current.right = self.add_helper(current.right, key, value)
return current

def add(self, key, value = None):
pass
if self.root == None:
self.root = TreeNode(key, value)
else:
self.add_helper(self.root, key, value)

# Time Complexity:
# Space Complexity:
# Time Complexity: O(log n)
# Space Complexity: O(1)
def find(self, key):
Comment on lines +31 to 33

Choose a reason for hiding this comment

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

👍

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

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

# Time Complexity:
# Space Complexity:
def inorder_helper(self, current, values_list):
Comment on lines +43 to +46

Choose a reason for hiding this comment

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

👍 , but since you're building a list of node values, the space complexity is O(n)

if not current:
return values_list
object = {"key": current.key, "value": current.value}

self.inorder_helper(current.left, values_list)
values_list.append(object)
self.inorder_helper(current.right, values_list)

return values_list

def inorder(self):
pass
values_list = []
return self.inorder_helper(self.root, values_list)

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

def preorder_helper(self, current_node, values_list):
Comment on lines +61 to +64

Choose a reason for hiding this comment

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

👍 Same issue with space complexity

if not current_node:
return values_list
object = {"key": current_node.key, "value": current_node.value}

values_list.append(object)
self.preorder_helper(current_node.left, values_list)
self.preorder_helper(current_node.right, values_list)
return values_list

# Time Complexity:
# Space Complexity:
def preorder(self):
pass
values_list = []
return self.preorder_helper(self.root, values_list)

# Time Complexity: O(n)
# Space Complexity: O(1)
def postorder_helper(self, current_node, values_list):
Comment on lines +78 to +80

Choose a reason for hiding this comment

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

👍 Same issue with space complexity

if not current_node:
return values_list
object = {"key": current_node.key, "value": current_node.value}

self.postorder_helper(current_node.left, values_list)
self.postorder_helper(current_node.right, values_list)
values_list.append(object)
return values_list

# Time Complexity:
# Space Complexity:
def postorder(self):
pass
values_list = []
return self.postorder_helper(self.root, values_list)


# Time Complexity:
# Space Complexity:
def height(self):
pass
# Time Complexity: O(n)
# Space Complexity: O(n)
def height_helper(self, current_node):
Comment on lines +95 to +97

Choose a reason for hiding this comment

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

👍 If the tree is balanced this is O(log n) for space complexity and O(n) otherwise.

if not current_node:
return 0

return max(self.height_helper(current_node.left), self.height_helper(current_node.right)) + 1
def height(self):
return self.height_helper(self.root)


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




# # Useful for printing
def to_s(self):
return f"{self.inorder()}"
return f"{self.inorder()}"
Binary file modified tests/__pycache__/__init__.cpython-39.pyc
Binary file not shown.