Skip to content

Leaves - Nicky C.#23

Open
njch5 wants to merge 2 commits into
Ada-C12:masterfrom
njch5:master
Open

Leaves - Nicky C.#23
njch5 wants to merge 2 commits into
Ada-C12:masterfrom
njch5:master

Conversation

@njch5

@njch5 njch5 commented Feb 24, 2020

Copy link
Copy Markdown

No description provided.

@CheezItMan CheezItMan left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nice work, you hit the basic learning goals, with some issues on space/time complexity. You also have a height method which isn't working. Check out my comments and let me know if you have any questions.

Comment thread lib/tree.rb
Comment on lines +32 to 35
# Time Complexity: O(h) where h is the max height of the binary search tree.
# In a best case scenario, time complexity would be O(logn)
# Space Complexity: O(1)
def add(key, value)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The space complexity is O(log n) if the tree is balanced and O(n) if it's not.

Comment thread lib/tree.rb
Comment on lines +50 to 53
# Time Complexity: If tree is balanced, it would be O(logn) n being the size of the tree
# In a worst case scenario, the time complexity would be O(n)
# Space Complexity: O(1)
def find(key)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The space complexity is O(log n) if the tree is balanced and O(n) if it's not.

Comment thread lib/tree.rb
Comment on lines +66 to 68
# Time Complexity: O(h) h is the height of the tree
# Space Complexity: O(h) h is the height of the tree
def inorder

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Since you're visiting each node and creating a list containing all node values... the time/space complexity is O(n).

Comment thread lib/tree.rb
Comment on lines +81 to 83
# Time Complexity: O(h) h is the height of the tree
# Space Complexity: O(h) h is the height of the tree
def preorder

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Since you're visiting each node and creating a list containing all node values... the time/space complexity is O(n).

Comment thread lib/tree.rb
Comment on lines +96 to 98
# Time Complexity: O(h) h is the height of the tree
# Space Complexity: O(h) h is the height of the tree
def postorder

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Since you're visiting each node and creating a list containing all node values... the time/space complexity is O(n).

Comment thread lib/tree.rb
Comment on lines +102 to +111
def height_helper(current_node, max, count)
return max if current_node.nil?

if count > max
max = count
end

height_helper(current_node.left, max, count + 1)
height_helper(current_node.right, max, count + 1)
end

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ This method isn't working since it's always returning the right subtree's height.

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