Skip to content

binary_search_tree #45

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 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
126 changes: 97 additions & 29 deletions binary_search_tree/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,51 +9,119 @@ def __init__(self, key, val = None):
self.right = None



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

# Time Complexity:
# Space Complexity:
def add_helper_function(self, current, next):
if next.key < current.key:
if not current.left:
current.left = next
return
self.add_helper_function(current.left, next)
else:
if not current.right:
current.right = next
return
self.add_helper_function(current.right, next)

def add(self, key, value = None):
pass
if not self.root:
self.root = TreeNode(key, value)
else:
new_node = TreeNode(key, value)
self.add_helper_function(self.root, new_node)

# Time Complexity: O(log n)
# Space Complexity: O(log n)

# Time Complexity:
# Space Complexity:

def find(self, key):
pass
current = self.root

while current:
if current.key == key:
return current.value
elif key < current.key:
current = current.left
else:
current = current.right

return None

# Time Complexity: O(log n)
# Space Complexity: O(log n)

Choose a reason for hiding this comment

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

Good news! Since you did an iterative implementation of find, you don't take the hit of the extra stack frames you get from recursion, so your space complexity here is actually O(1), since no new memory is allocated.



def inorder_helper_function(self, current, node_values):
if not current:
return node_values

self.inorder_helper_function(current.left, node_values)
node_values.append({
"key": current.key,
"value": current.value
})
self.inorder_helper_function(current.right, node_values)

# Time Complexity:
# Space Complexity:
return node_values

def inorder(self):
pass
values = []
return self.inorder_helper_function(self.root, values)

# Time Complexity: O(log n)
# Space Complexity: O(log n)

Choose a reason for hiding this comment

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

Note that unlike add and find where you only traverse down one branch of the tree, with the depth-first traversals, you do travel to every node in the tree, bumping up the space and time complexities to O(n).

This is the case for inorder, preorder, and postorder, so I won't repeat this there.



def preorder_helper_function(self, current, node_values):
if not current:
return node_values

node_values.append({
"key": current.key,
"value": current.value
})
self.preorder_helper_function(current.left, node_values)
self.preorder_helper_function(current.right, node_values)

return node_values

# Time Complexity:
# Space Complexity:
def preorder(self):
pass
values = []
return self.preorder_helper_function(self.root, values)

# Time Complexity:
# Space Complexity:
def postorder(self):
pass
# Time Complexity: O(log n)
# Space Complexity: O(log n)

# Time Complexity:
# Space Complexity:
def height(self):
pass

def postorder_helper_function(self, current, node_values):
if not current:
return node_values

self.postorder_helper_function(current.left, node_values)
self.postorder_helper_function(current.right, node_values)
node_values.append({
"key": current.key,
"value": current.value
})

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

def postorder(self):
values = []
return self.postorder_helper_function(self.root, values)


# Time Complexity: O(log n)
# Space Complexity: O(log n)


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

# # Useful for printing
def to_s(self):
return f"{self.inorder()}"
return max(self.height_helper(current.left), self.height_helper(current.right)) + 1

def height(self):
return self.height_helper(self.root)

Choose a reason for hiding this comment

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

It doesn't seem you added a time or space complexity calculation for your implementation of height, but I am not going to move down to a Yellow, but I do encourage you to think through it.