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 - lin #52

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
122 changes: 90 additions & 32 deletions binary_search_tree/tree.py
Original file line number Diff line number Diff line change
@@ -1,59 +1,117 @@
class TreeNode:
def __init__(self, key, val = None):
if val == None:
val = key
def __init__(self, key, value=None):
if value == None:
value = key

self.key = key
self.value = val
self.value = value
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
# Time Complexity: O(log(n))*assumes all balanced tree
# Space Complexity: O(1)
def add(self, key, value=None):
Comment on lines +16 to +18

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 call stack this is O(log n) for space complexity (if the tree is balanced and O(n) otherwise).

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

# Time Complexity:
# Space Complexity:
def find(self, key):
pass
def add_helper(self, current_node, key, value):
if current_node == None:
return TreeNode(key, value)
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, value):
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.

👍 Similar issues with space complexity.

return self.find_helper(self.root, value)

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

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

Choose a reason for hiding this comment

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

👍 Since you hit each node and make a list the of all the nodes in the tree the time and space complexity is O(n)

pass
array = []
if self.root == None:
return array
return self.inorder_helper(self.root, array)

def inorder_helper(self, current, array):
if current == None:
return
self.inorder_helper(current.left, array)
array.append({"key": current.key, "value": current.value})
self.inorder_helper(current.right, array)
return array

# Time Complexity:
# Space Complexity:
# Time Complexity: O(logn)
# Space Complexity: O(n)
def preorder(self):
Comment on lines +63 to 65

Choose a reason for hiding this comment

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

👍 Similar issues to time/space complexity

pass
array = []
if self.root == None:
return array
return self.preorder_helper(self.root, array)

# Time Complexity:
# Space Complexity:
def preorder_helper(self, current, array):
if current == None:
return
array.append({"key": current.key, "value": current.value})
self.preorder_helper(current.left, array)
self.preorder_helper(current.right, array)
return array

# Time Complexity: O(logn)
# Space Complexity: O(n)
def postorder(self):
Comment on lines +79 to 81

Choose a reason for hiding this comment

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

👍 Similar issues to time/space complexity

pass
array = []
if self.root == None:
return array
return self.postorder_helper(self.root, array)

def postorder_helper(self, current, array):
if current == None:
return
self.postorder_helper(current.left, array)
self.postorder_helper(current.right, array)
array.append({"key": current.key, "value": current.value})
return array

# Time Complexity:
# Space Complexity:
# Time Complexity: O(logn)
# Space Complexity: O(1)
def height(self):
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.

👍 However the space complexity is O(log n) due to the recursive call stack.

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

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

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




# # Useful for printing
# # Useful for printing
def to_s(self):
return f"{self.inorder()}"
Binary file modified tests/__pycache__/__init__.cpython-39.pyc
Binary file not shown.
2 changes: 2 additions & 0 deletions tests/test_binary_search_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,11 @@ def test_height_of_many_node_tree(tree_with_nodes):
tree_with_nodes.add(2.5, "bread")
assert tree_with_nodes.height() == 5

@pytest.mark.skip
def test_bfs_with_empty_tree(empty_tree):
assert empty_tree.bfs() == []

@pytest.mark.skip
def test_bfs_with_tree_with_nodes(tree_with_nodes):
expected_answer = [
{
Expand Down