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

Lilly C16 #50

Open
wants to merge 1 commit 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
125 changes: 106 additions & 19 deletions binary_search_tree/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,122 @@ class Tree:
def __init__(self):
self.root = None

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

Choose a reason for hiding this comment

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

since you aren't recursively calling this, the space complexity is O(1) because no new memory is dependent upon the size of the input

def add(self, key, value = None):
pass

# Time Complexity:
# Space Complexity:
if self.root == None:
self.root = TreeNode(key, value)
return

current = self.root
while current != None:
if current.key > key:
if current.left == None:
current.left = TreeNode(key, value)
return
else:
current = current.left
else:
if current.right == None:
current.right = TreeNode(key, value)
return
else:
current = current.right

# Time Complexity: o(1)

Choose a reason for hiding this comment

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

Time complexity for find will be O(log n) because you are still iterating through the BST, but only one half of it every time.

# Space Complexity: O(n)

Choose a reason for hiding this comment

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

since you aren't recursively calling this, the space complexity is O(1) because no new memory is dependent upon the size of the input

# search for a node in the tree
def find(self, key):

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 != None:
if current.key == key:
return current.value
elif current.key > key:
current = current.left
else:
current = current.right
return None



# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(n)
def inorder_helper(self, current, result):
if current == None:
return result
else:
result = self.inorder_helper(current.left, result)
result.append({"key":current.key, "value":current.value})
result = self.inorder_helper(current.right, result)
return result

def inorder(self):

Choose a reason for hiding this comment

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

👍

pass
result = []
current = self.root
if current == None:
return result
else:
result = self.inorder_helper(current, result)
return result

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(n)
def preorder_helper(self, current, result):
if current == None:
return result
else:
result.append({"key":current.key, "value":current.value})
result = self.preorder_helper(current.left, result)
result = self.preorder_helper(current.right, result)
return result
def preorder(self):

Choose a reason for hiding this comment

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

👍

pass
result = []
current = self.root
if current == None:
return result
else:
result = self.preorder_helper(current, result)
return result

# Time Complexity: O(n)
# Space Complexity: O(n)
def postorder_helper(self, current, result):
if current == None:
return result
else:
result = self.postorder_helper(current.left, result)
result = self.postorder_helper(current.right, result)
result.append({"key":current.key, "value":current.value})
return result

# Time Complexity:
# Space Complexity:
def postorder(self):

Choose a reason for hiding this comment

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

👍

pass
result = []
current = self.root
if current == None:
return result
else:
result = self.postorder_helper(current, result)
return result

# Time Complexity: O(n)
# Space Complexity: O(n)
def height_helper(self, current):
if current == None:
return 0
else:
left_height = self.height_helper(current.left)
right_height = self.height_helper(current.right)
if left_height > right_height:
return left_height + 1
else:
return right_height + 1

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

Choose a reason for hiding this comment

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

👍

pass
current = self.root
if current == None:
return 0
else:
return self.height_helper(current)


# # Optional Method
Expand Down