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

Rock - Hannah L. #43

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
137 changes: 111 additions & 26 deletions binary_search_tree/tree.py
Original file line number Diff line number Diff line change
@@ -1,59 +1,144 @@
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:
def add(self, key, value = None):
pass
def add_helper(self, current_node, key, value):
if current_node == None:
return TreeNode(key, value)
elif key <= current_node:

Choose a reason for hiding this comment

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

This is why some tests are failing.

Suggested change
elif key <= current_node:
elif 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)

# Time Complexity: O(log n)
# Space Complexity: O(log n)
def add(self, key, value=None):
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 +33 to 35

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 key < current.key: # if there is a left node keep searching
current = current.left
else:
current = current.right
return None

# Time Complexity:
# Space Complexity:
def inorder_helper(self, root, elements):
if root == None:
return

# if there is left, traverse
self.inorder_helper(root.left, elements)
# append the last left node that becomes the "root"
elements.append({"key": root.key, "value": root.value})
# if there is right, traverse (it will start from left recursive)
self.inorder_helper(root.right, elements)
return

# Time Complexity: O(n)
# Space Complexity: O(n)
def inorder(self):
Comment on lines +59 to 61

Choose a reason for hiding this comment

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

👍

pass
# order: left, root, right
# if there are no nodes, return empty list
if self.root == None:
return []

# Time Complexity:
# Space Complexity:
elements = []
self.inorder_helper(self.root, elements)

return elements

def preorder_helper(self, root, elements):
if root == None:
return

# if there is root, add that
elements.append({"key": root.key, "value": root.value})
# if there is a left, traverse that
self.preorder_helper(root.left, elements)
# then traverse right
self.preorder_helper(root.right, elements)
return

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

Choose a reason for hiding this comment

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

👍

pass
# order: root, left, right
if self.root == None:
return []

elements = []
self.preorder_helper(self.root, elements)

# Time Complexity:
# Space Complexity:
def postorder_helper(self, root, elements):
if root == None:
return

self.postorder_helper(root.left, elements)
self.postorder_helper(root.right, elements)
elements.append({"key": root.key, "value": root.value})

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

Choose a reason for hiding this comment

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

👍

pass
# order: left, right, current
if self.root == None:
return []

# Time Complexity:
# Space Complexity:
def height(self):
pass
elements = []
self.postorder_helper(self.root, elements)

def max_height_helper(self, root, height):
if root == None:
return height

# else return +1 of left and right subtress
left_height = 1 + self.max_height_helper(root.left, height)
right_height = 1 + self.max_height_helper(root.right, height)

# compare to find max
if left_height < right_height:
return right_height
else:
return left_height
Comment on lines +117 to +124

Choose a reason for hiding this comment

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

You can also do this with the max function

Suggested change
left_height = 1 + self.max_height_helper(root.left, height)
right_height = 1 + self.max_height_helper(root.right, height)
# compare to find max
if left_height < right_height:
return right_height
else:
return left_height
left_height = 1 + self.max_height_helper(root.left, height)
right_height = 1 + self.max_height_helper(root.right, height)
return max(right_height, left_height)


# Time Complexity: O(n)
# Space Complexity: O(log n)
def height(self):
if self.root:
return self.max_height_helper(self.root, 0)
return 0

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