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

Sumitra - scissors class #36

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
185 changes: 161 additions & 24 deletions binary_search_tree/tree.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class TreeNode:
# sorting by key
def __init__(self, key, val = None):
if val == None:
val = key
Expand All @@ -7,51 +8,187 @@ def __init__(self, key, val = None):
self.value = val
self.left = None
self.right = None



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

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

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:
self.root = TreeNode(key, value)
else:
self.add_helper(self.root, key, value)

def add_helper(self, current_node, key, value):
if current_node == None:
return TreeNode(key, value)

# key = 7 , value = "Ada"
# we are adding by key
if key <= current_node.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:
# # Not recursive
# # Time Complexity: O(log n)
# # Space Complexity: O(1)
# def find(self, key):
# current = self.root
# while current:
# if key < current.key:
# current = current.left
# elif key > current.key:
# current = current.right
# else:
# return current.value
# return None

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

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 call stack.

pass
if self.root == None:
return None
else:
return self.find_helper(self.root, key)

def find_helper(self, current_node, key):
if current_node == None:
return None

# Time Complexity:
# Space Complexity:
if current_node.key == key:
return current_node.value
elif current_node.key <= key:
return self.find_helper(current_node.right, key)
else:
return self.find_helper(current_node.left, key)


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

Choose a reason for hiding this comment

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

👍

pass
nodes_in_tree = []

if self.root == None:
return nodes_in_tree

# Time Complexity:
# Space Complexity:
return self.helper_inorder(self.root, nodes_in_tree)

def helper_inorder(self, node, nodes_in_tree):
if node == None:
return

self.helper_inorder(node.left, nodes_in_tree)
nodes_in_tree.append({
'key': node.key,
'value': node.value
})
self.helper_inorder(node.right, nodes_in_tree)

return nodes_in_tree


# Time Complexity: O(n)
# Space Complexity: O(n)
def preorder(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
nodes_in_tree = []

if self.root == None:
return nodes_in_tree

return self.helper_preorder(self.root, nodes_in_tree)

def helper_preorder(self, node, nodes_in_tree):
if node == None:
return

nodes_in_tree.append({
'key': node.key,
'value': node.value
})
self.helper_preorder(node.left, nodes_in_tree)
self.helper_preorder(node.right, nodes_in_tree)

return nodes_in_tree


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

Choose a reason for hiding this comment

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

👍

pass
nodes_in_tree = []

if self.root == None:
return nodes_in_tree

return self.helper_postorder(self.root, nodes_in_tree)

def helper_postorder(self, node, nodes_in_tree):
if node == None:
return

# Time Complexity:
# Space Complexity:
self.helper_postorder(node.left, nodes_in_tree)
self.helper_postorder(node.right, nodes_in_tree)
nodes_in_tree.append({
'key': node.key,
'value': node.value
})

return nodes_in_tree


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

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.helper_height(self.root)

def helper_height(self, node):
if node == None:
return 0

# # Optional Method
# # Time Complexity:
# # Space Complexity:
left = self.helper_height(node.left)
right = self.helper_height(node.right)

return max(left, right) + 1


# Optional Method
# # Time Complexity: O(n)
# # Space Complexity: O(n)
def bfs(self):
pass
nodes_in_tree = []

if self.root == None:
return nodes_in_tree

return self.helper_bf(self.root, nodes_in_tree)

def helper_bf(self, node, nodes_in_tree):

Choose a reason for hiding this comment

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

Do you need the helper? You're not doing recursion.

if node == None:
return

queue = []
queue.append(node)

while len(queue) != 0:
current_node = queue.pop(0)
nodes_in_tree.append({
'key': current_node.key,
'value': current_node.value
})

if current_node.left != None:
queue.append(current_node.left)

if current_node.right != None:
queue.append(current_node.right)

return nodes_in_tree


# # Useful for printing
Expand Down
Binary file modified tests/__pycache__/__init__.cpython-39.pyc
Binary file not shown.