Skip to content

Ivette F. - Spruce #49

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Ivette F. - Spruce #49

wants to merge 2 commits into from

Conversation

IvetteDF
Copy link

@IvetteDF IvetteDF commented Jul 7, 2022

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.

✨💫 Nice work, Ivette. I left a couple comments on how you might refactor, particularly your height implementation. Let me know what questions you have.

🟢

else:
parent_node = parent_node.left
else:
return "WTF"

Choose a reason for hiding this comment

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

🤯
You could remove this line and leave line 36 as an else (or as is)

Comment on lines +17 to +18
# Time Complexity: O(log(n))
# Space Complexity: O(1)

Choose a reason for hiding this comment

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

self.root = added_node
return self.root
parent_node = self.root
while True:

Choose a reason for hiding this comment

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

Generally try to avoid having while loop conditions that can never be Falsey. You always want to be making progress towards make the loop condition False in the loop body.

How might you refactor your code to eliminate the while True. (Hint: look at your find implementation)

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

✨ Nice iterative solution

return None

# left, current, right
def inorder_helper(self, current_node, node_list = None):

Choose a reason for hiding this comment

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

pass
return self.inorder_helper(self.root)

def preorder_helper(self, current_node, node_list = None):

Choose a reason for hiding this comment

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

# Time Complexity:
# Space Complexity:
# 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.


# Time Complexity:
# Space Complexity:
def postorder_helper(self, current_node, node_list = None):

Choose a reason for hiding this comment

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

return node_list

# 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.

Comment on lines +136 to +137
# Time Complexity: O(nlog(n))
# Space Complexity: O(m) where m is the number of end nodes

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

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