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

Earth - Alice B. #21

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
111 changes: 87 additions & 24 deletions lib/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ class TreeNode
attr_reader :key, :value
attr_accessor :left, :right

def initialize(key, val)
def initialize(key, val)
@key = key
@value = val
@left = nil
@right = nil
end
end
end

class Tree
Expand All @@ -16,51 +16,114 @@ def initialize
@root = nil
end

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

👍 The time complexity is O(log n) if the tree is balanced and O(n) if it's unbalanced.

The space complexity is the same (because of the recursion).

raise NotImplementedError
@root = add_helper(@root, key, value)
end

def add_helper(root, key, value)
if root.nil?
root = TreeNode.new(key, value)
return root
end

if root.key > key
root.left = add_helper(root.left, key, value)
else
root.right = add_helper(root.right, key, value)
end

root
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(log n)
# Space Complexity: O(log n)
def find(key)
Comment on lines +40 to 42

Choose a reason for hiding this comment

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

👍 The time complexity is O(log n) if the tree is balanced and O(n) if it's unbalanced.

The space complexity is the same (because of the recursion).

raise NotImplementedError
find_helper(@root, key)
end

# Time Complexity:
# Space Complexity:
def find_helper(node, key)
return nil if node.nil?

if node.key == key
node.value
elsif node.key < key
find_helper(node.right, key)
else
find_helper(node.left, key)
end
end

# Time Complexity: O(n)
# Space Complexity: O(n)
def inorder
raise NotImplementedError
result = []
inorder_helper(@root, result)
result
end

def inorder_helper(node, result)
return if node.nil?

inorder_helper(node.left, result)
result.push({ key: node.key, value: node.value })
inorder_helper(node.right, result)
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(n)
def preorder
Comment on lines +74 to 76

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
result = []
preorder_helper(@root, result)
result
end

# Time Complexity:
# Space Complexity:
def preorder_helper(node, result)
return if node.nil?

result.push({ key: node.key, value: node.value })
preorder_helper(node.left, result)
preorder_helper(node.right, result)
end

# Time Complexity: O(n)
# Space Complexity: O(n)
def postorder
Comment on lines +90 to 92

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
result = []
postorder_helper(@root, result)
result
end

def postorder_helper(node, result)
return if node.nil?

postorder_helper(node.left, result)
postorder_helper(node.right, result)
result.push({ key: node.key, value: node.value })
end

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

Choose a reason for hiding this comment

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

👍 The space complexity (due to recursion) is O(log n) if the tree is balanced and O(n) if it's unbalanced.

raise NotImplementedError
height_helper(@root, 0)
end

def height_helper(node, steps)
return steps if node.nil?

[height_helper(node.left, steps + 1), height_helper(node.right, steps + 1)].max
end

# Optional Method
# Time Complexity:
# Space Complexity:
# Time Complexity:
# Space Complexity:
def bfs
raise NotImplementedError
return [] if @root.nil?
end

# Useful for printing
def to_s
return "#{self.inorder}"
inorder.to_s
end
end