Skip to content

Laurel S. - Cedar #41

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 4 commits 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
137 changes: 117 additions & 20 deletions binary_search_tree/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,142 @@ def __init__(self, key, val = None):


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

# Time Complexity:
# Space Complexity:
# Time Complexity: O(log n)
# Space Complexity: O(1)
def add(self, key, value = None):
pass
node = TreeNode(key, value)
if not self.root:
self.root = node
return self.root

current = self.root
previous = None
while current:
previous = current
if key <= current.key:
current = current.left
else:
current = current.right
if key <= previous.key:
previous.left = node
else:
previous.right = node

# RECURSIVE
# Time Complexity:
# Space Complexity:
# def add(self, key, value = None):
# # base case
# if not current.left and not current.right:

Choose a reason for hiding this comment

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

Not a big deal, but generally I recommend removing unfinished but uncommented code like this to just keep the source clean as a style thing.



# Time Complexity: O(log n)
# Space Complexity: O(1)
def find(self, key):
pass
if not self.root:
return None
if self.root.key == key:
return self.root.value
current = self.root
while current:
if current.key == key:
return current.value
if key <= current.key:
current = current.left
else:
current = current.right
return None

# Time Complexity:
# Space Complexity:
def inorder_helper(self, root, nodes):
if root:
self.inorder_helper(root.left, nodes)
nodes.append({'key': root.key, 'value': root.value})
self.inorder_helper(root.right, nodes)

# Time Complexity: O(n) while n = number of nodes in BST
# Space Complexity: O(n)
def inorder(self):
pass
# left, root, right
if not self.root:
return []
nodes = []

# Time Complexity:
# Space Complexity:
self.inorder_helper(self.root, nodes)
return nodes


def preorder_helper(self, current, nodes):
if current:
nodes.append({'key': current.key, 'value': current.value})
self.preorder_helper(current.left, nodes)
self.preorder_helper(current.right, nodes)
return

# Time Complexity: O(n) n = number of nodes in BST
# Space Complexity: O(n)
def preorder(self):
pass
# root -> left -> right
if not self.root:
return []
nodes_list = []
self.preorder_helper(self.root, nodes_list)
return nodes_list

# Time Complexity:
# Space Complexity:

def postorder_helper(self, current, nodes_list):
if current:
self.postorder_helper(current.left, nodes_list)
self.postorder_helper(current.right, nodes_list)
nodes_list.append({'key': current.key, 'value': current.value})

# Time Complexity: O(n) n = number of nodes in BST
# Space Complexity: O(n)
def postorder(self):
pass
# left -> right -> root
if not self.root:
return []
nodes_list = []
self.postorder_helper(self.root, nodes_list)
return nodes_list

# Time Complexity:
# Space Complexity:

def height_helper(self, root):
if not root:
return 0
left_height = self.height_helper(root.left)
right_height = self.height_helper(root.right)
return 1 + max(left_height, right_height)


# Time Complexity: O(n) and n = # of nodes in BST
# Space Complexity: O(n) ? not sure on this one

Choose a reason for hiding this comment

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

Good news, neither are the instructors, because as I was doing this grading I realized our instructor solution was wrong on these calculations! In this case as there is no additional space allocated, I would consider this O(1).

def height(self):
pass
if not self.root:
return 0
return self.height_helper(self.root)


# # Optional Method
# # Time Complexity:
# # Space Complexity:
# Optional Method
# Time Complexity: O(n) where n = number of nodes in BST
# Space Complexity: O(n)
def bfs(self):
pass
if not self.root:
return []
queue = [ self.root ]
values = []
while queue:
current = queue.pop(0)
# {'key': 5, 'value': 'Peter'}
values.append({'key': current.key, 'value': current.value})
if current.left:
queue.append(current.left)
if current.right:
queue.append(current.right)
return values



Expand Down