Skip to content

tree fun #16

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
135 changes: 117 additions & 18 deletions lib/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,141 @@ def initialize
@root = nil
end

# Time Complexity:
# Space Complexity:

#what to do if node is empty, smaller or larger than initial node
def add_helper(node, key, value)
#if the node is empty, the create the node with a key, value
if node.nil?
node = TreeNode.new(key, value)
#if the key is less than the node key, then put it to the left side
elsif key < node.key
node.left = add_helper(node.left, key, value)
#if key is larger than the node key, then add it to the right side
elsif key > node.key
node.right = add_helper(node.right, key, value)
end
return node
end

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

👍 The time/space complexities are O(log n) if the tree is balanced and O(n) if they are unbalanced.

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

# Time Complexity:
# Space Complexity:
#--------------------------------------------------------------------------#
#pretty much copied pattern from above
#what to do if node is empty, return empty
#otherwise if node is not empty, then set it as the key
#if key is less than -> left
#if key is greater than -> right
def find_helper(node, key)
if node.nil?
return nil
elsif node.key == key
return node.value
elsif key < node.key
return find_helper(node.left, key)
elsif key > node.key
return find_helper(node.right, key)
end
end

# Time Complexity: O(n)
# Space Complexity: O(1)
def find(key)
Comment on lines +59 to 61

Choose a reason for hiding this comment

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

👍 Similar issues to add with time/space complexities.

raise NotImplementedError
return find_helper(@root, key)
end

# Time Complexity:
# Space Complexity:
#--------------------------------------------------------------------------#
#use list is a reference to pass
#check if node is empty, if yes, return the list
def inorder_helper(node, list)
if node.nil?
return list
end
#push key,value
list.push({key: node.key, value: node.value})
inorder_helper(node.left, list)
inorder_helper(node.right, list)
end

# Time Complexity: O(n)
# Space Complexity: O(n)
def inorder
Comment on lines +78 to 80

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
return inorder_helper(@root, [])
end

# Time Complexity:
# Space Complexity:
#--------------------------------------------------------------------------#
#used same logic as inorder
#use list is a reference to pass
#check if node is empty, if yes, return the list
def preorder_helper(node, list)
if node.nil?
return list
end

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


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

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
return preorder_helper(@root, [])
end

# Time Complexity:
# Space Complexity:
#--------------------------------------------------------------------------#
#used same logic as inorder
#use list is a reference to pass
#check if node is empty, if yes, return the list
def postorder_helper(node, list)
if node.nil?
return list
end

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

Comment on lines +110 to +118

Choose a reason for hiding this comment

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

This looks like a pre-order method.

Suggested change
def postorder_helper(node, list)
if node.nil?
return list
end
list << {key: node.key, value: node.value}
postorder_helper(node.left, list)
postorder_helper(node.right, list)
def postorder_helper(node, list)
if node.nil?
return list
end
postorder_helper(node.left, list)
postorder_helper(node.right, list)
list << {key: node.key, value: node.value}

end

# Time Complexity: O(n)
# Space Complexity: 0(n)
def postorder
raise NotImplementedError
return postorder_helper(@root, [])
end

# Time Complexity:
# Space Complexity:
#--------------------------------------------------------------------------#
def height_helper(node)
if node.nil?
return 0
end
left_length = height_helper(node.left)
right_length = height_helper(node.right)

if left_length > right_length
longest_length = left_length
else
longest_length = right_length
end
longest_length += 1 # Add one for the current node
return longest_length

end

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

Choose a reason for hiding this comment

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

👍 , the time & space complexities are O(n) for unbalanced trees and O(log n) when the trees are balanced.

raise NotImplementedError
return height_helper(@root)
end

#--------------------------------------------------------------------------#
#--------------------------------------------------------------------------#

# Optional Method
# Time Complexity:
# Space Complexity:
Expand Down