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

Maria M. - Scissors #44

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

# Time Complexity:
# Space Complexity:
# Time Complexity: if tree is balanced, O(log n), if unbalanced, O(n)
# Space Complexity: O(long n)
def add(self, key, value = None):
Comment on lines +17 to 19

Choose a reason for hiding this comment

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

👍 Space complexity is the same as time complexity.

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

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

# Time Complexity:
# Space Complexity:
if key <= current_node.key:
current_node.left = self.add_helper(
current_node.left, key, value)
else:
current_node.right = self.add_helper(
current_node.right, key, value)
return current_node

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

Choose a reason for hiding this comment

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

👍 Space complexity is the same as time complexity.

pass
if self.root == None:
return None

# Time Complexity:
# Space Complexity:
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: O(n)
# Space Complexity: O(n)
def inorder(self):
Comment on lines +53 to 55

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
return self.inorder_helper(self.root, result)

def inorder_helper(self, root, result):
if root:
self.inorder_helper(root.left, result)
result.append({'key': root.key, 'value': root.value})
self.inorder_helper(root.right, result)
return result

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(n)
def preorder(self):
Comment on lines +68 to 70

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
return self.preorder_helper(self.root, result)

def preorder_helper(self, root, result):
if root:
result.append({'key': root.key, 'value': root.value})
self.preorder_helper(root.left, result)
self.preorder_helper(root.right, result)
return result

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(n)
def postorder(self):
Comment on lines +83 to 85

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
return self.postorder_helper(self.root, result)

# Time Complexity:
# Space Complexity:
def height(self):
pass
def postorder_helper(self, root, result):
if root:
self.postorder_helper(root.left, result)
self.postorder_helper(root.right, result)
result.append({'key': root.key, 'value': root.value})
return result

# Time Complexity: O(n)
# Space Complexity: O(1)
def height(self):
Comment on lines +98 to +100

Choose a reason for hiding this comment

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

👍 The space complexity is O( log n) if the tree is balanced and O(n) otherwise.

if self.root == None:
return 0
return self.height_helper(self.root)

def height_helper(self, root):
if root == None:
return 0

left_height = self.height_helper(root.left)
right_height = self.height_helper(root.right)
tree_height = max(left_height, right_height) + 1
return tree_height

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




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