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

Finished all but the bfs part #45

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
115 changes: 93 additions & 22 deletions binary_search_tree/tree.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class TreeNode:
def __init__(self, key, val = None):
def __init__(self, key, val=None):
if val == None:
val = key

Expand All @@ -14,35 +14,108 @@ class Tree:
def __init__(self):
self.root = None

# Time Complexity:
# Space Complexity:
def add(self, key, value = None):
pass
def add_helper(self, current_node, key, value):
if current_node == None:
return TreeNode(key, value)
if current_node.key > key:
current_node.left = self.add_helper(current_node.left, key, value)
else:
current_node.right = self.add_helper(current_node.right, key, value)
return current_node

# Time Complexity:
# Space Complexity:
# Time Complexity: O(log n)
# Space Complexity: O(log n)
def add(self, key, value=None):
Comment on lines +26 to +28

Choose a reason for hiding this comment

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

👍

if self.root == None:
self.root = TreeNode(key, value)
else:
self.add_helper(self.root, key, value)

# Time Complexity: O(log n)
# Space Complexity: O(1)
def find(self, key):
Comment on lines +34 to 36

Choose a reason for hiding this comment

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

👍 Nice iterative solution

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

def inorder_helper(self, root, elements):
if root == None:
return

# Time Complexity:
# Space Complexity:
self.inorder_helper(root.left, elements)
elements.append({"key": root.key, "value": root.value})
self.inorder_helper(root.right, elements)
return

# Time Complexity: O(n)
# Space Complexity: O(n)
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.

👍

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

elements = []
self.inorder_helper(self.root, elements)

# Time Complexity:
# Space Complexity:
return elements

def preorder_helper(self, root, elements):
if root == None:
return

elements.append({"key": root.key, "value": root.value})
self.preorder_helper(root.left, elements)
self.preorder_helper(root.right, elements)
return elements

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

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 []

elements = []
self.preorder_helper(self.root, elements)
return elements

# Time Complexity:
# Space Complexity:
def postorder_helper(self, root, elements):
if root == None:
return

self.postorder_helper(root.left, elements)
self.postorder_helper(root.right, elements)
elements.append({"key": root.key, "value": root.value})

# Time Complexity: O(n)
# Space Complexity: O(n)
def postorder(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 []
elements = []
self.postorder_helper(self.root, elements)
return elements

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

left = self.height_helper(node.left)
right = self.height_helper(node.right)

# Time Complexity:
# Space Complexity:
return max(left, right) + 1

# Time Complexity: O(n)
# Space Complexity: O(log n)
def height(self):
Comment on lines +112 to 114

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 0

return self.height_helper(self.root)


# # Optional Method
Expand All @@ -51,8 +124,6 @@ def height(self):
def bfs(self):
pass




# # Useful for printing
def to_s(self):
Expand Down
Binary file modified tests/__pycache__/__init__.cpython-39.pyc
Binary file not shown.