Skip to content

Charlotte:Space:Tree-Practice #34

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ gem 'minitest'
gem 'minitest-spec'
gem 'minitest-reporters'
gem "pry"
gem 'pry-nav'

Choose a reason for hiding this comment

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

Interesting gem, what does it do?

gem 'minitest-skip'

97 changes: 79 additions & 18 deletions lib/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,101 @@ def initialize
@root = nil
end

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

Choose a reason for hiding this comment

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

👍

return TreeNode.new(key, value) if node.nil?
if key < node.key
node.left = add_recursive(node.left, key, value)
else
node.right = add_recursive(node.right, key, value)
end
return node
end

def add(key, value)
raise NotImplementedError
@root = add_recursive(@root, key, value)
end

# Time Complexity: O(log n)
# Space Complexity: O(1)
def find_recursive(node, key)
Comment on lines +35 to +37

Choose a reason for hiding this comment

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

👍

return node.value if node.key == key

if key <= node.key
node.left = find_recursive(node.left, key)
else
node.right = find_recursive(node.right, key)
end
end

# Time Complexity:
# Space Complexity:
def find(key)
raise NotImplementedError
current = @root
return nil if !current

return find_recursive(root, key)
end

# Time Complexity: O(n)
# Space Complexity: O(n)
def inorder_recursive(node, list)
Comment on lines +54 to +56

Choose a reason for hiding this comment

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

👍

return list if node.nil?

inorder_recursive(node.left, list)
list << {key: node.key, value: node.value}
inorder_recursive(node.right, list)
end

# Time Complexity:
# Space Complexity:
def inorder
raise NotImplementedError
return inorder_recursive(@root, [])
end

# Time Complexity: O(n)
# Space Complexity: O(n)
def preorder_recursive(node, list)
Comment on lines +68 to +70

Choose a reason for hiding this comment

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

👍

return list if node.nil?

list << {key: node.key, value: node.value}
preorder_recursive(node.left, list)
preorder_recursive(node.right, list)
end

# Time Complexity:
# Space Complexity:
def preorder
raise NotImplementedError
return preorder_recursive(@root, [])
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(n)
def postorder_recursive(node, list)
Comment on lines +82 to +84

Choose a reason for hiding this comment

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

👍

return list if node.nil?

postorder_recursive(node.left, list)
postorder_recursive(node.right, list)
list << {key: node.key, value: node.value}
end
def postorder
raise NotImplementedError
return postorder_recursive(@root, [])
end

# Time Complexity: O(n)
# Space Complexity: O(n
def height_recursive(node)
Comment on lines +95 to +97

Choose a reason for hiding this comment

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

👍 , the space complexity is right if the tree is unbalanced, if it's balanced, the space complexity is O(log n)

return 0 if node.nil?

left = height_recursive(node.left)
right = height_recursive(node.right)

if left > right
height = left
else
height = right
end
height += 1
return height
end

# Time Complexity:
# Space Complexity:
def height
raise NotImplementedError
return height_recursive(@root)
end

# Optional Method
Expand Down