Skip to content

Nataliia Getlin - Rock - C15 #40

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 3 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,4 @@ dmypy.json
# Cython debug symbols
cython_debug/

tests/__pycache__/__init__.cpython-39.pyc
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Tree Exercise

In this exercise you will implement, in Ruby, several Tree methods.
In this exercise you will implement, in Python, several Tree methods.

Choose a reason for hiding this comment

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

Whoops


- `add(value)` - This method adds a key-value pair to the Binary Search Tree
- `find(value)` - This method returns the matching value for the given key if it is in the tree and `None` if the key is not found in the tree.
Expand Down
156 changes: 130 additions & 26 deletions binary_search_tree/tree.py
Original file line number Diff line number Diff line change
@@ -1,59 +1,163 @@
class TreeNode:
def __init__(self, key, val = None):
if val == None:
def __init__(self, key, val=None):
if val is None:
val = key

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


def to_json(self):
return {
"key": self.key,
"value": self.value
}


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

# Time Complexity:
# Space Complexity:
def add(self, key, value = None):
pass
# ----------- add ----------

# Time Complexity: O(log n) for balanced BST, O(n) for unbalanced
# Space Complexity: O(1)

def add_helper(self, current, key, value):
Comment on lines +24 to +27

Choose a reason for hiding this comment

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

👍 , however the space complexity is O(log n) if the tree is balanced.

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

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

# ----------- find ----------

# Time Complexity: O(log n) for balanced BST, O(n) for unbalanced
# Space Complexity: O(1)

def find_helper(self, current, key):
Comment on lines +44 to +47

Choose a reason for hiding this comment

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

👍 However due to the recursive stack, the space complexity is O(log n) for a balanced tree.

if current is None:
return None
if current.key == key:
return current.value
elif current.key >= key:
return self.find_helper(current.left, key)
elif current.key < key:
return self.find_helper(current.right, key)

# Time Complexity:
# Space Complexity:
def find(self, key):
pass
return self.find_helper(self.root, key)

# ----------- in-order ----------

# Time Complexity: O(n), because we visit each node once
# Space Complexity: O(depth of the recursion) or O(h), where h is height
# of the tree -> O(n) in the worst case

def inorder_helper(self, current, result):
Comment on lines +62 to +66

Choose a reason for hiding this comment

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

👍

if current:
self.inorder_helper(current.left, result)
result.append(current.to_json())
self.inorder_helper(current.right, result)

# Time Complexity:
# Space Complexity:
def inorder(self):
pass
if self.root is None:
return list()

result = list()
self.inorder_helper(self.root, result)
return result

# ----------- pre-order ----------

# Time Complexity: ^
# Space Complexity: ^

def preorder_helper(self, current, result):
if current:
result.append(current.to_json())
self.preorder_helper(current.left, result)
self.preorder_helper(current.right, result)
Comment on lines +82 to +89

Choose a reason for hiding this comment

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

👍


# Time Complexity:
# Space Complexity:
def preorder(self):
pass
if self.root is None:
return list()

result = list()
self.preorder_helper(self.root, result)
return result

# ----------- post-order ----------
# Time Complexity: ^
# Space Complexity: ^

def postorder_helper(self, current, result):
Comment on lines +99 to +103

Choose a reason for hiding this comment

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

👍 Time/space

if current:
self.postorder_helper(current.left, result)
self.postorder_helper(current.right, result)
result.append(current.to_json())

# Time Complexity:
# Space Complexity:
def postorder(self):
pass
if self.root is None:
return list()

result = list()
self.postorder_helper(self.root, result)
return result

# ----------- height ----------

# Time Complexity: O(log n) for balanced, O(n) for unbalanced
# Space Complexity: O(n)

def height_helper(self, current):
Comment on lines +119 to +122

Choose a reason for hiding this comment

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

👍

if current is None:
return 0
else:
return 1 + max(self.height_helper(current.left),
self.height_helper(current.right))

# Time Complexity:
# Space Complexity:
def height(self):
pass
if self.root is None:
return 0
else:
return self.height_helper(self.root)



# # Optional Method
# # Time Complexity:
# # Space Complexity:
# # Time Complexity: O(n)
# # Space Complexity:O(n)

def bfs(self):
Comment on lines +138 to 141

Choose a reason for hiding this comment

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

👍

pass
result = list()


if self.root is None:
return result

queue = list()
queue.append(self.root)

while len(queue) > 0:
current = queue.pop(0)
if current.left:
queue.append(current.left)
if current.right:
queue.append(current.right)
result.append(current.to_json())

return result

# # Useful for printing

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