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

Cedar - Lux Barker #44

Open
wants to merge 3 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
91 changes: 77 additions & 14 deletions binary_search_tree/tree.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from collections import deque
class TreeNode:
def __init__(self, key, val = None):
if val == None:
Expand All @@ -7,7 +8,7 @@ def __init__(self, key, val = None):
self.value = val
self.left = None
self.right = None



class Tree:
Expand All @@ -17,41 +18,103 @@ def __init__(self):
# Time Complexity:
# Space Complexity:
def add(self, key, value = None):
pass
new_node = TreeNode(key, value)
if self.root is None:
self.root = new_node
return self.root
return self.add_helper(self.root, new_node)

def add_helper(self, curr, new_node):
if curr is None:
curr = new_node
elif new_node.key < curr.key:
curr.left = self.add_helper(curr.left, new_node)
else:
curr.right = self.add_helper(curr.right, new_node)
return curr

Choose a reason for hiding this comment

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

This implementation of add_helper works, but definitely does a bit of extra work in setting nodes. Essentially, every node on the path to where the new node ends up will have either its left or right node "updated", though most of these will be updated to the exact same node. The only one that gets a meaningful update is whenever the bottom of the tree is reached and gets its left/right node updated to the new node. These updates happen in lines 31 and 32.

This probably will not be too bad of a performance hit, but there is a way that you can do this and just update only the node you need.

But this overall fine enough!


# Time Complexity:
# Space Complexity:
def find(self, key):
pass
return self.find_helper(self.root, key)

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

# Time Complexity:
# Space Complexity:
def inorder(self):
pass
arr = []
self.inorder_helper(self.root, arr)
return arr

# Time Complexity:
def inorder_helper(self, curr, arr):
if curr is not None:
self.inorder_helper(curr.left, arr)
arr.append({"key": curr.key, "value": curr.value})
self.inorder_helper(curr.right, arr)

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

# Time Complexity:
def preorder_helper(self, curr, arr):
if curr is not None:
arr.append({"key": curr.key, "value": curr.value})
self.preorder_helper(curr.left, arr)
self.preorder_helper(curr.right, arr)
# Time Complexity:
# Space Complexity:
def postorder(self):
pass
arr = []
self.postorder_helper(self.root, arr)
return arr

def postorder_helper(self, curr, arr):
if curr is not None:
self.postorder_helper(curr.left, arr)
self.postorder_helper(curr.right, arr)
arr.append({"key": curr.key, "value": curr.value})

# Time Complexity:
# Space Complexity:
def height(self):
pass
return self.height_helper(self.root, 0)

def height_helper(self, curr, count):
if curr is None:
return count
left_height = self.height_helper(curr.left, count + 1)
right_height = self.height_helper(curr.right, count + 1)
return max(left_height, right_height)

# # Optional Method
# # Time Complexity:
# # Space Complexity:
# Optional Method
# Time Complexity:
# Space Complexity:
def bfs(self):
pass
visited = []
if self.root is None:
return visited
q = deque()
q.append(self.root)
while q:
curr = q.popleft()
visited.append({"key": curr.key, "value": curr.value})
if curr.left is not None:
q.append(curr.left)
if curr.right is not None:
q.append(curr.right)
return visited




# # Useful for printing
Expand Down