-
Notifications
You must be signed in to change notification settings - Fork 69
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
BTS 16/18 #62
base: master
Are you sure you want to change the base?
BTS 16/18 #62
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
# from recursive_helpers import * | ||
|
||
class TreeNode: | ||
def __init__(self, key, val = None): | ||
def __init__(self, key, val=None): | ||
if val == None: | ||
val = key | ||
|
||
|
@@ -9,40 +11,125 @@ def __init__(self, key, val = None): | |
self.right = None | ||
|
||
|
||
|
||
class Tree: | ||
def __init__(self): | ||
self.root = None | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def add(self, key, value = None): | ||
pass | ||
# Space Complexity: | ||
# iterative approach | ||
def add(self, key, value=None): | ||
node = TreeNode(key, value) | ||
if self.root is None: | ||
self.root = node | ||
return | ||
current = self.root | ||
|
||
while current: | ||
if current.key > key: | ||
# ##go left | ||
if current.left is None: | ||
# ##Add node to the left | ||
current.left = node | ||
return | ||
else: | ||
current = current.left | ||
else: | ||
# ##go right | ||
if current.right is None: | ||
# ##Add the node on the right | ||
current.right = node | ||
return | ||
else: | ||
current = current.right | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
Comment on lines
-22
to
-23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ⏱🪐 Time and space complexity? |
||
def find(self, key): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✨ |
||
pass | ||
result = self.find_helper(key, self.root) | ||
# catch edge case to avoid value error | ||
if result is not None: | ||
return result.value | ||
return result | ||
|
||
def find_helper(self, key, current_node): | ||
""" | ||
|
||
:return: | ||
""" | ||
if current_node is None or current_node.key == key: | ||
return current_node | ||
if key > current_node.key: | ||
return self.find_helper(key, current_node.right) | ||
return self.find_helper(key, current_node.left) | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Space Complexity: | ||
Comment on lines
-28
to
+65
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ⏱🪐 Time and space complexity? |
||
def inorder(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✨ |
||
pass | ||
""" | ||
input: tree class | ||
:return: list of key value tuples from traversing tree inorder | ||
""" | ||
inorder_result = [] | ||
# call recursive function with current node and result_list | ||
self.inorder_helper(self.root, inorder_result) | ||
return inorder_result | ||
|
||
def inorder_helper(self, current_node, inorder_result): | ||
""" | ||
:param current_node: self.root | ||
:param inorder_result: list | ||
:return: populates input dictionary with key and node values from BST | ||
""" | ||
if current_node is None: | ||
return | ||
|
||
# traverse left side until current_node is none | ||
# pass in left child node and list | ||
self.inorder_helper(current_node.left, inorder_result) | ||
# append key value pairs as backtrack up the stack | ||
inorder_result.append({'key': current_node.key, 'value': current_node.value}) | ||
# traverse right | ||
self.inorder_helper(current_node.right, inorder_result) | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
Comment on lines
93
to
94
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ⏱🪐 Time and space complexity? |
||
def preorder(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✨ |
||
pass | ||
preorder_result = [] | ||
self.preorder_helper(self.root, preorder_result) | ||
return preorder_result | ||
|
||
def preorder_helper(self,curr_node,preorder_result): | ||
if curr_node is None: | ||
return | ||
preorder_result.append({'key': curr_node.key, 'value': curr_node.value}) | ||
self.preorder_helper(curr_node.left, preorder_result) | ||
self.preorder_helper(curr_node.right, preorder_result) | ||
|
||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
Comment on lines
108
to
109
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ⏱🪐 Time and space complexity? |
||
def postorder(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✨ |
||
pass | ||
postorder_result = [] | ||
self.postorder_helper(self.root, postorder_result) | ||
return postorder_result | ||
|
||
def postorder_helper(self,curr_node,postorder_result): | ||
if curr_node is None: | ||
return | ||
self.postorder_helper(curr_node.left, postorder_result) | ||
self.postorder_helper(curr_node.right, postorder_result) | ||
postorder_result.append({'key': curr_node.key, 'value': curr_node.value}) | ||
|
||
|
||
def height_helper(self, curr_node): | ||
if curr_node is None: | ||
return 0 | ||
return 1 + max(self.height_helper(curr_node.left), self.height_helper(curr_node.right)) | ||
|
||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
Comment on lines
129
to
130
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ⏱🪐 Time and space complexity? |
||
def height(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✨ |
||
pass | ||
return self.height_helper(self.root) | ||
|
||
|
||
# # Optional Method | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
⏱🪐 Time and space complexity?