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

Asli (Alf) - Spruce - C16 #54

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

Conversation

asliathman
Copy link

No description provided.

Copy link

@kyra-patton kyra-patton left a comment

Choose a reason for hiding this comment

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

Hi Alf, it looks like you may have looked at Ivette's code for this project. While I do not mind if you turn to outside resources for help, it is difficult for me to give feedback as to your understanding of I believe it may not be your own work. For that reason, I'm grading this as a red - because I cannot assess how well you understand the problem.

At this point, you do not need to resubmit. It will not affect your ability to graduate.

self.root = added

parent = self.root
while True:

Choose a reason for hiding this comment

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

We generally try to avoid while loops whose conditions can never be Falsey. With while loops, we always want the body of the while loop to be making progress toward the condition being False.

How might you refactor you code to eliminate this? Hint: Look at your find function

Comment on lines +38 to +39
else:
return "something went wrong"

Choose a reason for hiding this comment

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

This isn't necessary

Suggested change
else:
return "something went wrong"

# Time Complexity:
# Space Complexity:
# Time Complexity: 0(log(n))
# Space Complexity: 0(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.

return nodes

# Time Complexity: O(n)
# Space Complexity: O(n)
def inorder(self):

Choose a reason for hiding this comment

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

return nodes

# Time Complexity: O(n)
# Space Complexity: O(n)
def preorder(self):

Choose a reason for hiding this comment

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

return nodes

# Time Complexity: O(n)
# Space Complexity: O(n)
def postorder(self):

Choose a reason for hiding this comment

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


# Time Complexity: O(nlog(n))
# Space Complexity: 0(m) -> m == # of nodes
def height(self):

Choose a reason for hiding this comment

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

✨ This time and space complexity is correct for your solution!

You could refactor your code to achieve an O(n) time and space solution. Instead of having height_helper return a list, have it return an integer. When you make a recursive call on the left/right subtree, you know that your height is 1 + the height of the deepest subtree. So you can say that the height is 1 + max(height of left subtree, height of right subtree).

pass
added = TreeNode(key, value)
if not self.root:
self.root = added

Choose a reason for hiding this comment

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

If you do not return after this guard close, you'll enter into an infinite loop

Suggested change
self.root = added
self.root = added
return added

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