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

C15 Paper Saejin Tanguay #48

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
149 changes: 126 additions & 23 deletions binary_search_tree/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,151 @@ def __init__(self, key, val = None):
self.left = None
self.right = None



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

# Time Complexity:
# Space Complexity:
# Time Complexity: On
# Space Complexity: O1
def add(self, key, value = None):
Comment on lines +15 to 17

Choose a reason for hiding this comment

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

👍 However space complexity is O(log n) if the tree is balanced and O(n) otherwise due to the recursive call stack.

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)

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

👍 However space complexity is O(log n) if the tree is balanced and O(n) otherwise due to the recursive call stack.

pass
if self.root == None:
return None
else:
return self.find_helper(self.root, key)

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

# Time Complexity:
# Space Complexity:
# Time Complexity: On
# Space Complexity: On
def inorder(self):
Comment on lines +51 to 53

Choose a reason for hiding this comment

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

👍

pass
all_ele = []
return self.inorder_helper(self.root, all_ele)

def inorder_helper(self, curr, all_ele):
if not curr:
return all_ele
self.inorder_helper(curr.left, all_ele)
all_ele.append({
"key": curr.key,
"value": curr.value})
self.inorder_helper(curr.right, all_ele)

# Time Complexity:
# Space Complexity:
return all_ele

# Time Complexity: On
# Space Complexity: On
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
all_ele = []
return self.preorder_helper(self.root, all_ele)

def preorder_helper(self, curr, all_ele):
if not curr:
return all_ele

all_ele.append({
"key": curr.key,
"value": curr.value
})
self.preorder_helper(curr.left, all_ele)
self.preorder_helper(curr.right, all_ele)

return all_ele

# Time Complexity:
# Space Complexity:
# Time Complexity: On
# Space Complexity: On
def postorder(self):
Comment on lines +87 to 89

Choose a reason for hiding this comment

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

👍

pass
all_ele = []
return self.postorder_helper(self.root, all_ele)

def postorder_helper(self, curr, all_ele):
if not curr:
return all_ele

self.postorder_helper(curr.left, all_ele)
self.postorder_helper(curr.right, all_ele)

all_ele.append({
"key": curr.key,
"value": curr.value
})

# Time Complexity:
# Space Complexity:
return all_ele

# # Time Complexity: On
# # Space Complexity: O1
def height(self):
Comment on lines +107 to 109

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 ) for a balanced tree and O(n) for an unbalanced tree.

This is due to the recursive call stack.

pass
return self.height_helper(self.root)

def height_helper(self, curr):
if not curr:
return 0

left_height = self.height_helper(curr.left)
right_height = self.height_helper(curr.right)

return max(left_height, right_height) +1

# # Optional Method
# # Time Complexity:
# # Space Complexity:
# # Time Complexity: On
# # Space Complexity: On
def bfs(self):
Comment on lines +122 to 124

Choose a reason for hiding this comment

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

👍

pass

arr = []

if not self.root:
return arr

root = {
"key": self.root.key,
"value": self.root.value}

temp = [self.root]
arr.append(root)

while len(temp) > 0:
curr = temp[0]

if curr.left:
temp.append(curr.left)
arr.append({
"key": curr.left.key,
"value": curr.left.value})

if curr.right:
temp.append(curr.right)
arr.append({
"key": curr.right.key,
"value": curr.right.value})

temp.remove(curr)

return arr



Expand Down
Binary file removed tests/__pycache__/__init__.cpython-39.pyc
Binary file not shown.