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

Abigail C - Rock #53

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
104 changes: 96 additions & 8 deletions binary_search_tree/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,129 @@ def __init__(self):
# Time Complexity:
# Space Complexity:
def add(self, key, value = None):
Comment on lines 17 to 19

Choose a reason for hiding this comment

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

👍 Time/space complexity?

pass
if self.root == None:
self.root = TreeNode(key,value)
current = self.root

while True:

Choose a reason for hiding this comment

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

Just a personal note. I dislike while True loops which depend on break statements to control the flow of execution because it makes the loop harder to trace and understand.

if self.root.key == key:
break
elif current.key > key:
if current.left != None:
current = current.left
else:
current.left = TreeNode(key,value)
break
elif current.key < key:
if current.right != None:
current = current.right
else:
current.right = TreeNode(key,value)
break
# return self.root
# if current ==


# Time Complexity:
# Space Complexity:
def find(self, key):
Comment on lines 43 to 45

Choose a reason for hiding this comment

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

👍 time/space complexity?

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

# Time Complexity:
# Space Complexity:
def inorder(self):
Comment on lines 56 to 58

Choose a reason for hiding this comment

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

👍 time/space complexity?

pass
def helper(node):
if node == None:
return []
left_part= helper(node.left)
middle_part = [{"key":node.key,"value":node.value}]
right_part = helper(node.right)
result = left_part + middle_part + right_part

Choose a reason for hiding this comment

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

Just a note that doing so much array concatenation is expensive rather than concatenating to the same area each time.

return result

return helper(self.root)



# Time Complexity:
# Space Complexity:
def preorder(self):
Comment on lines 72 to 74

Choose a reason for hiding this comment

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

👍 Time/space complexity?

pass
def helper( node):
if node == None:
return []
left_part= helper(node.left)
middle_part = [{"key":node.key,"value":node.value}]
right_part = helper(node.right)
result = middle_part + left_part + right_part
return result

return helper(self.root)

# Time Complexity:
# Space Complexity:
def postorder(self):
Comment on lines 86 to 88

Choose a reason for hiding this comment

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

👍 time/space complexity?

pass
def helper( node):
if node == None:
return []
left_part= helper(node.left)
middle_part = [{"key":node.key,"value":node.value}]
right_part = helper(node.right)
result = left_part + right_part + middle_part
return result

return helper(self.root)

# Time Complexity:
# Space Complexity:
def height(self):
pass
def helper(node):
if node == None:
return 0
left_part= helper(node.left)
right_part = helper(node.right)
result = max(left_part, right_part) + 1
return result
return helper(self.root)


# # Optional Method
# # Time Complexity:
# # Space Complexity:
def bfs(self):
pass
if self.root == None:
return []


queue = []
leveled_order = [{
"key" : self.root.key,
"value":self.root.value
}]

queue.append(self.root)
while queue != []:
temp = queue.pop(0)

Choose a reason for hiding this comment

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

Just a note that queue.pop(0) is an O(n) operation. Python does have a deque data structure which provides a similar O(1) operation.

https://www.geeksforgeeks.org/deque-in-python/

if temp.left:
queue.append(temp.left)
leveled_order.append({
"key" : temp.left.key,
"value": temp.left.value
})
if temp.right:
queue.append(temp.right)
leveled_order.append({
"key" : temp.right.key,
"value": temp.right.value
})

return leveled_order


# # Useful for printing
Expand Down