-
Notifications
You must be signed in to change notification settings - Fork 64
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
Abigail C - Rock #53
base: master
Are you sure you want to change the base?
Abigail C - Rock #53
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 |
---|---|---|
|
@@ -17,41 +17,129 @@ def __init__(self): | |
# Time Complexity: | ||
# Space Complexity: | ||
def add(self, key, value = None): | ||
pass | ||
if self.root == None: | ||
self.root = TreeNode(key,value) | ||
current = self.root | ||
|
||
while True: | ||
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. Just a personal note. I dislike |
||
if self.root.key == key: | ||
break | ||
elif current.key > key: | ||
if current.left != None: | ||
current = current.left | ||
else: | ||
current.left = TreeNode(key,value) | ||
break | ||
elif current.key < key: | ||
if current.right != None: | ||
current = current.right | ||
else: | ||
current.right = TreeNode(key,value) | ||
break | ||
# return self.root | ||
# if current == | ||
|
||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def find(self, key): | ||
Comment on lines
43
to
45
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/space complexity? |
||
pass | ||
current = self.root | ||
while current != None: | ||
if current.key == key: | ||
return current.value | ||
elif key < current.key: | ||
current = current.left | ||
else: | ||
current = current.right | ||
return None | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def inorder(self): | ||
Comment on lines
56
to
58
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/space complexity? |
||
pass | ||
def helper(node): | ||
if node == None: | ||
return [] | ||
left_part= helper(node.left) | ||
middle_part = [{"key":node.key,"value":node.value}] | ||
right_part = helper(node.right) | ||
result = left_part + middle_part + right_part | ||
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. Just a note that doing so much array concatenation is expensive rather than concatenating to the same area each time. |
||
return result | ||
|
||
return helper(self.root) | ||
|
||
|
||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def preorder(self): | ||
Comment on lines
72
to
74
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/space complexity? |
||
pass | ||
def helper( node): | ||
if node == None: | ||
return [] | ||
left_part= helper(node.left) | ||
middle_part = [{"key":node.key,"value":node.value}] | ||
right_part = helper(node.right) | ||
result = middle_part + left_part + right_part | ||
return result | ||
|
||
return helper(self.root) | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def postorder(self): | ||
Comment on lines
86
to
88
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/space complexity? |
||
pass | ||
def helper( node): | ||
if node == None: | ||
return [] | ||
left_part= helper(node.left) | ||
middle_part = [{"key":node.key,"value":node.value}] | ||
right_part = helper(node.right) | ||
result = left_part + right_part + middle_part | ||
return result | ||
|
||
return helper(self.root) | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def height(self): | ||
pass | ||
def helper(node): | ||
if node == None: | ||
return 0 | ||
left_part= helper(node.left) | ||
right_part = helper(node.right) | ||
result = max(left_part, right_part) + 1 | ||
return result | ||
return helper(self.root) | ||
|
||
|
||
# # Optional Method | ||
# # Time Complexity: | ||
# # Space Complexity: | ||
def bfs(self): | ||
pass | ||
if self.root == None: | ||
return [] | ||
|
||
|
||
queue = [] | ||
leveled_order = [{ | ||
"key" : self.root.key, | ||
"value":self.root.value | ||
}] | ||
|
||
queue.append(self.root) | ||
while queue != []: | ||
temp = queue.pop(0) | ||
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. Just a note that |
||
if temp.left: | ||
queue.append(temp.left) | ||
leveled_order.append({ | ||
"key" : temp.left.key, | ||
"value": temp.left.value | ||
}) | ||
if temp.right: | ||
queue.append(temp.right) | ||
leveled_order.append({ | ||
"key" : temp.right.key, | ||
"value": temp.right.value | ||
}) | ||
|
||
return leveled_order | ||
|
||
|
||
# # Useful for printing | ||
|
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/space complexity?