Skip to content

Space - Katie #26

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 1 commit 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
107 changes: 99 additions & 8 deletions lib/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,48 +19,139 @@ def initialize
# Time Complexity:
# Space Complexity:
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 time & space complexity?

raise NotImplementedError
new_node = TreeNode.new(key, value)
if @root == nil
@root = new_node
return new_node
else
add_helper(@root, new_node)
end
end

def add_helper(current, new_node)
if new_node.key <= current.key
if current.left == nil
return current.left = new_node
else
add_helper(current.left, new_node)
end
else
if current.right == nil
return current.right = new_node
else
add_helper(current.right, new_node)
end
end
end

# Time Complexity:
# Space Complexity:
def find(key)
Comment on lines 47 to 49

Choose a reason for hiding this comment

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

👍 , time/space complexity?

raise NotImplementedError
if @root == nil
return nil
elsif @root.key == key
return @root.value
else
current_node = @root
while current_node != nil
if key == current_node.key
return current_node.value
elsif key <= current_node.key
current_node = current_node.left
else
current_node = current_node.right
end
end
end
end



# Time Complexity:
# Space Complexity:
def inorder

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
ordered_array = []
if @root != nil
inorder_helper(@root, ordered_array)
end
return ordered_array
end

def inorder_helper(node, array)
return if node.nil?
inorder_helper(node.left, array)
array.push({"key": node.key, "value": node.value})
inorder_helper(node.right, array)
end

# Time Complexity:
# Space Complexity:
def preorder
Comment on lines 87 to 89

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
preordred_array = []
if @root != nil
preorder_helper(@root, preordred_array)
end
return preordred_array
end

def preorder_helper(node, array)
return if node.nil?
array << {key: node.key, value: node.value}

preorder_helper(node.left, array)
preorder_helper(node.right, array)
end




# Time Complexity:
# Space Complexity:
def postorder
Comment on lines 108 to 110

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
preordred_array = []
if @root != nil
postorder_helper(@root, preordred_array)
end
return preordred_array
end

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

postorder_helper(node.left, array)
postorder_helper(node.right, array)
array << {key: node.key, value: node.value}
end

# Time Complexity:
# Space Complexity:
def height
Comment on lines 126 to 128

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
count = 0
if @root != nil
count = height_helper(@root)
end
return count
end

def height_helper(root)
return 0 if root.nil?

return find_max(height_helper(root.left), height_helper(root.right)) + 1
end

def find_max(a,b)
a >= b ? a : b
end

# Optional Method
# Time Complexity:
# Space Complexity:
def bfs
raise NotImplementedError
# raise NotImplementedError
end

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