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

Whit - Paper #39

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
201 changes: 179 additions & 22 deletions binary_search_tree/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,46 +14,203 @@ class Tree:
def __init__(self):
self.root = None

# Time Complexity:
# Space Complexity:
# Time Complexity: O(log n) if balanced
# Space Complexity: O(1)

def add(self, key, value = None):
Comment on lines +17 to 20

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 stack

pass
new_node = TreeNode(key, value)

if self.root == None:
self.root = new_node
return self.root

return self.__add_helper(self.root, new_node)

def __add_helper(self, current, new_node):
if not current:
return new_node

if new_node.key <= current.key:
current.left = self.__add_helper(current.left, new_node)
else:
current.right = self.__add_helper(current.right, new_node)

return current

# Time Complexity:
# Space Complexity:
def add_iterative(self, key, value = None):

Choose a reason for hiding this comment

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

Nice iterative solution

if self.root == None:
self.root = TreeNode(key=key, val=value)
return self.root

current_node = self.root

while current_node:
if key <= current_node.key:
if current_node.left:
current_node = current_node.left
else:
current_node.left = TreeNode(key=key, val=value)
return current_node.left
elif key > current_node.key:
if current_node.right:
current_node = current_node.right
else:
current_node.right = TreeNode(key=key, val=value)
return current_node.right

# Time Complexity: O(log n) if balanced
# Space Complexity: O(1)
def find(self, key):
Comment on lines +61 to 63

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)

pass
if self.root == None:
return None

return self.__find_helper(self.root, key)

def __find_helper(self, root, key):
if not root:
return None

if root.key == key:
return root.value
elif key < root.key:
return self.__find_helper(root.left, key)
else:
return self.__find_helper(root.right, key)


def find_iterative(self, key):
current_node = self.root
while current_node:
if current_node.key == key:
return current_node.value
elif key < current_node.key:
current_node = current_node.left
else:
current_node = current_node.right

# Time Complexity:
# Space Complexity:
return None


# Time Complexity: O(n)
# Space Complexity: O(n)
def inorder(self):
Comment on lines +94 to 96

Choose a reason for hiding this comment

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

👍

pass
if self.root == None:
return []

result = []

return self.__inorder_helper(self.root, result)

def __inorder_helper(self, root, result):
if root:
self.__inorder_helper(root.left, result)
result.append(self.to_dict(root))
self.__inorder_helper(root.right, result)

return result



# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(n)
def preorder(self):
Comment on lines +114 to 116

Choose a reason for hiding this comment

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

👍

pass
if self.root == None:
return []

# Time Complexity:
# Space Complexity:
result = []

return self.__preorder_helper(self.root, result)

def __preorder_helper(self, root, result):
if root:
result.append(self.to_dict(root))
self.__preorder_helper(root.left, result)
self.__preorder_helper(root.right, result)

return result

def preorder_iterative(self):
if not self.root:
return []

result = []
stack = []
stack.append(self.root)

while len(stack) > 0:
root = stack.pop()
result.append(self.to_dict(root))
if root.right:
stack.append(root.right)
if root.left:
stack.append(root.left)

return result

# Time Complexity: O(n)
# Space Complexity: O(n)
def postorder(self):
Comment on lines +150 to 152

Choose a reason for hiding this comment

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

👍

pass
if not self.root:
return []

result = []

return self.__postorder_helper(self.root, result)

def __postorder_helper(self, root, result):
if root:
self.__postorder_helper(root.left, result)
self.__postorder_helper(root.right, result)
result.append(self.to_dict(root))

return result

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(1)
def height(self):
Comment on lines +168 to 170

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.

pass
if self.root == None:
return 0

return self.__height_helper(self.root)

def __height_helper(self, root):
if not root:
return 0

max_height = max(self.__height_helper(root.left), self.__height_helper(root.right)) + 1

return max_height


# # Optional Method
# # Time Complexity:
# # Space Complexity:
# # Time Complexity: O(n)
# # Space Complexity: O(n)
def bfs(self):
Comment on lines +186 to 188

Choose a reason for hiding this comment

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

Nice BFS solution!

pass

if self.root == None:
return []

bfs = []
queue = [self.root]

while len(queue) > 0:
current = queue.pop(0)
bfs.append(self.to_dict(current))

if current.left:
queue.append(current.left)
if current.right:
queue.append(current.right)

return bfs



# # Useful for printing
def to_s(self):
return f"{self.inorder()}"

def to_dict(self, node):
return {
'key': node.key,
'value': node.value
}
Binary file modified tests/__pycache__/__init__.cpython-39.pyc
Binary file not shown.