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

BST traversals #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

BST traversals #42

wants to merge 1 commit into from

Conversation

LacyDraper
Copy link

No description provided.

Copy link

@CheezItMan CheezItMan left a 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).

Comment on lines +26 to +35
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

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.

Comment on lines +41 to 43
# Time Complexity: O(logn)
# Space Complexity: O(1)
def find(self, key):

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

Comment on lines +57 to 59
# Time Complexity: O(logn)
# Space Complexity: O(1)
def inorder(self):

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

Comment on lines +94 to 96
# Time Complexity: O(logn)
# Space Complexity: O(1)
def preorder(self):

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

Comment on lines +119 to 121
# Time Complexity: O(logn)
# Space Complexity: O(1)
def postorder(self):

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):

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):

Choose a reason for hiding this comment

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

⚠️ Good start, but for BFS you really need a queue of some sort, so recursion isn't the best solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants