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

Binary Search Tree Paper Karla T. #55

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

# Time Complexity:
# Space Complexity:
# Time Complexity: O(log n)
# Space Complexity: O(log n )

def add_helper(self, current, key, value):
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.

👍 Time/space complexity is correct if the tree is balanced. O(n) otherwise.

if current is None:
return TreeNode(key, value)
if current.key >= key:
current.left = self.add_helper(current.left, key, value)
else:
current.right = self.add_helper(current.right, key, value)
return current

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


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

Choose a reason for hiding this comment

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

👍

pass
current = self.root
while current:
if current.key == key:
return current.value
elif current.key > key:
current = current.left
else:
current = current.right

# Time Complexity: O( n )
# Space Complexity: O( n )
def inorder_helper(self, current_node, listofvalues):
if not current_node:
return listofvalues
obj = {'key': current_node.key, 'value': current_node.value}
listofvalues.append(obj)
self.inorder_helper(current_node.right, listofvalues)

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

# Time Complexity: O(n)
# Space Complexity: O(n)
def preorder_helper(self, current_node, listofvalues):
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.

👍

if not current_node:
return listofvalues
obj = {'key': current_node.key, 'value': current_node.value}
listofvalues.append(obj)
self.preorder_helper(current_node.left, listofvalues)
self.preorder_helper(current_node.right, listofvalues)
return listofvalues

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

# Time Complexity: O(n)
# Space Complexity: O(n)
def postorder_helper(self, current_node, listofvalues):
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.

👍

if not current_node:
return listofvalues
obj = {'key': current_node.key, 'value':current_node.value}
self.postorder_helper(current_node.left, listofvalues)
self.postorder_helper(current_node.right, listofvalues)
listofvalues.append(obj)

# Time Complexity:
# Space Complexity:
def postorder(self):
pass
listofvalues = []
return self.postorder_helper(self.root, listofvalues)
# Time Complexity: O(n)
# Space Complexity: O(log n)
def height_helper(self, current_node):
Comment on lines +89 to +91

Choose a reason for hiding this comment

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

👍

if not current_node:
return 0
return max(self.height_helper(current_node.left), self.height_helper(current_node.right)) + 1

# Time Complexity:
# Space Complexity:
def height(self):
pass

return self.height_helper(self.root)

# # Optional Method
# # Time Complexity:
Expand Down
Binary file modified tests/__pycache__/__init__.cpython-39.pyc
Binary file not shown.