-
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
BST traversals #42
base: master
Are you sure you want to change the base?
BST traversals #42
Conversation
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.
Nice work Lacy, except for building an inverted tree your solution works for most of the methods (just postorder is failing).
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 |
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.
# Time Complexity: O(logn) | ||
# Space Complexity: O(1) | ||
def find(self, key): |
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.
👍 works, given your inverted tree
# Time Complexity: O(logn) | ||
# Space Complexity: O(1) | ||
def inorder(self): |
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.
👍 However the space and time complexities are O(n) due to the list you're building
# Time Complexity: O(logn) | ||
# Space Complexity: O(1) | ||
def preorder(self): |
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.
👍 O(n) for space/time
# Time Complexity: O(logn) | ||
# Space Complexity: O(1) | ||
def postorder(self): |
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.
👍 O(n) for space/time
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 comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Space & time complexity is O(n)
|
||
def bfs_helper(self,node,bfs_list): |
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.
No description provided.