-
Notifications
You must be signed in to change notification settings - Fork 64
BST traversals #42
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
base: master
Are you sure you want to change the base?
BST traversals #42
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 |
---|---|---|
|
@@ -14,44 +14,160 @@ class Tree: | |
def __init__(self): | ||
self.root = None | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def add(self, key, value = None): | ||
pass | ||
# Time Complexity: O(logn) | ||
# Space Complexity: O(1) | ||
def add(self, key, value): | ||
new_node = TreeNode(key,value) | ||
if not self.root: | ||
self.root = new_node | ||
return | ||
current = self.root | ||
previous = None | ||
while current: | ||
previous = current | ||
if current.key > new_node.key: | ||
current = current.left | ||
else: | ||
current = current.right | ||
if previous.key > new_node.key: | ||
previous.left = new_node | ||
else: | ||
previous.right = new_node | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
|
||
|
||
|
||
|
||
# Time Complexity: O(logn) | ||
# Space Complexity: O(1) | ||
def find(self, key): | ||
Comment on lines
+41
to
43
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. 👍 works, given your inverted tree |
||
pass | ||
current_node = self.root | ||
|
||
while current_node: | ||
if current_node.key == key: | ||
return current_node.value | ||
elif current_node.key > key: | ||
current_node = current_node.left | ||
else: | ||
current_node = current_node.right | ||
return None | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
|
||
|
||
# Time Complexity: O(logn) | ||
# Space Complexity: O(1) | ||
def inorder(self): | ||
Comment on lines
+57
to
59
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. 👍 However the space and time complexities are O(n) due to the list you're building |
||
pass | ||
traversal_list = [] | ||
if not self.root: | ||
return traversal_list | ||
self.inorder_helper(self.root, traversal_list) | ||
return traversal_list | ||
|
||
# Left, Root, Right | ||
def inorder_helper(self, node, traversal_list): | ||
if node == None: | ||
return traversal_list | ||
self.inorder_helper(node.left, traversal_list) | ||
|
||
traversal_list.append({ | ||
'key': node.key, | ||
'value' : node.value | ||
}) | ||
self.inorder_helper(node.right, traversal_list) | ||
return traversal_list | ||
|
||
|
||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
|
||
#Root, Left, Right | ||
def preorder_helper(self, node, traversal_list): | ||
|
||
if node == None: | ||
return traversal_list | ||
traversal_list.append({'key': node.key, | ||
'value' : node.value}) | ||
|
||
self.preorder_helper(node.left, traversal_list) | ||
self.preorder_helper(node.right, traversal_list) | ||
return traversal_list | ||
|
||
# Time Complexity: O(logn) | ||
# Space Complexity: O(1) | ||
def preorder(self): | ||
Comment on lines
+94
to
96
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. 👍 O(n) for space/time |
||
pass | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
traversal_list = [] | ||
if not self.root: | ||
return traversal_list | ||
self.preorder_helper(self.root, traversal_list) | ||
return traversal_list | ||
|
||
|
||
|
||
# Left, Right, Root | ||
def postorder_helper(self, node, traversal_list): | ||
|
||
if node == None: | ||
return traversal_list | ||
|
||
self.postorder_helper(node.left, traversal_list) | ||
|
||
self.preorder_helper(node.right, traversal_list) | ||
|
||
traversal_list.append({'key': node.key, | ||
'value' : node.value}) | ||
return traversal_list | ||
|
||
# Time Complexity: O(logn) | ||
# Space Complexity: O(1) | ||
def postorder(self): | ||
Comment on lines
+119
to
121
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. 👍 O(n) for space/time |
||
pass | ||
traversal_list = [] | ||
if not self.root: | ||
return traversal_list | ||
self.postorder_helper(self.root, traversal_list) | ||
return traversal_list | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# to find height we need to do DFS and store a value after each leaf is found. | ||
def height_helper(self, node): | ||
if node == None: | ||
return 0 | ||
count = 1 | ||
left_height = self.height_helper(node.left) | ||
right_height = self.height_helper(node.right) | ||
return max(left_height,right_height)+ 1 | ||
|
||
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. 👍 Space & time complexity is O(n) |
||
pass | ||
if not self.root: | ||
return 0 | ||
return self.height_helper(self.root) | ||
|
||
|
||
def bfs_helper(self,node,bfs_list): | ||
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. |
||
|
||
if node.left == None or node.right == None: | ||
return bfs_list | ||
|
||
self.bfs_helper(node, node.left, bfs_list) | ||
bfs_list.append({"key": node.left.key, | ||
"value" : node.left.value}) | ||
|
||
self.bfs_helper(node, node.right, bfs_list) | ||
bfs_list.append({"key": node.right.key, | ||
"value" : node.right.value}) | ||
|
||
# # Optional Method | ||
return bfs_list | ||
# # Time Complexity: | ||
# # Space Complexity: | ||
# append root, LC,RC | ||
def bfs(self): | ||
pass | ||
|
||
bfs_list = [] | ||
if not self.root: | ||
return bfs_list | ||
|
||
self.bfs(self.root) | ||
return bfs_list | ||
|
||
|
||
|
||
|
||
# # 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.
You are storing things that are less than the current node on the right side and elements greater than on the left side. This works, but you end up with an inverted tree.
That's why your postorder isn't working properly on the tests.