Skip to content

Space - Faezeh #19

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 3 commits into
base: master
Choose a base branch
from
Open

Conversation

faezeh-ashtiani
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.

Faezeh, pretty well done, except for a bug in height, overall nice work.

Take a look at my comments and let me know what questions you have.

Comment on lines +19 to 21
# Time Complexity: O(Log n)
# Space Complexity: O(1)
def add(key, value)

Choose a reason for hiding this comment

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

👍 , but the space complexity is O(log n) because of the call stack, assuming the tree is balanced.

Comment on lines +45 to 47
# Time Complexity: O(log n)
# Space Complexity: O(1)
def find(key)

Choose a reason for hiding this comment

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

👍 , but the space complexity is O(log n) because of the call stack, assuming the tree is balanced.

Comment on lines +75 to 77
# Time Complexity: O(Log n)
# Space Complexity: O(n)
def inorder

Choose a reason for hiding this comment

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

👍 Since we hit each node in the list, the the time complexity is also O(n)

Comment on lines +99 to +105
def inorder_helper(node, &block)
return if node.nil?

inorder_helper(node.left, &block)
yield node
inorder_helper(node.right, &block)
end

Choose a reason for hiding this comment

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

Clever use of a block-based method.

Comment on lines +107 to 109
# Time Complexity: O(Log n)
# Space Complexity: O(n)
def preorder

Choose a reason for hiding this comment

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

The traversals are all O(n) in time complexity.

Comment on lines +130 to 132
# Time Complexity: O(Log n)
# Space Complexity: O(n)
def postorder

Choose a reason for hiding this comment

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

👍

Comment on lines +153 to 155
# Time Complexity: O(n)
# Space Complexity: O(n)
def height

Choose a reason for hiding this comment

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

⚠ This doesn't quite work as it goes down the left and right sides of the tree but doesn't investigate all paths to the leaves.

A better solution:

def height_helper(current_node)
    return 0 if current_node.nil?
    return 1 +[height_helper(current_node.left), height_helper(current_node.right)].max
end

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