Skip to content

Rock - Juliana Tree exercise complete #49

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 4 commits 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
101 changes: 77 additions & 24 deletions binary_search_tree/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,98 @@ def __init__(self, key, val = None):
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(self, key, value = None):
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.

👍

pass
n_node = TreeNode(key, value)
if not self.root:
self.root = n_node
return None

current = self.root

while True:
if current.key > key and current.left is None:
current.left = n_node
return None
elif current.key <= key and current.right is None:
current.right = n_node
return None
elif current.key > key:
current = current.left
else:
current = current.right


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

👍

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

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

Choose a reason for hiding this comment

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

👍

pass
trees = []
return self.awesome_helper_inorder(self.root, trees)

Choose a reason for hiding this comment

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

Glad you have an awesome helper


def awesome_helper_inorder(self, current, trees):
if not current:
return trees
curr_storage = {"key": current.key, "value": current.value}

self.awesome_helper_inorder(current.left, trees)
trees.append(curr_storage)
self.awesome_helper_inorder(current.right, trees)

return trees

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity:O(n)
def preorder(self):
pass
if not self.root:
return []
left = Tree()
left.root = self.root.left
right = Tree()
right.root = self.root.right
Comment on lines +71 to +74

Choose a reason for hiding this comment

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

Why do you make a new Tree here? That seems excessive, but it does work.


return [{"key": self.root.key, "value": self.root.value}] + left.preorder() + right.preorder()

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

👍 With similar issues with preorder

pass
if not self.root:
return []

left = Tree()
left.root = self.root.left
right = Tree()
right.root = self.root.right

return left.postorder() + right.postorder() + [{"key": self.root.key, "value": self.root.value}]

# Time Complexity:
# Space Complexity:
# Time Complexity: O(log n)
# Space Complexity: O(n)
def height(self):
Comment on lines +91 to 93

Choose a reason for hiding this comment

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

👍 The time complexity here is O(n) because you visit each node.

pass
if not self.root:
return 0
left = Tree()
left.root = self.root.left
right = Tree()
right.root = self.root.right

return max(left.height(),right.height()) +1


# # Optional Method
Expand All @@ -51,9 +107,6 @@ def height(self):
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.