Skip to content
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
112 changes: 92 additions & 20 deletions lib/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,114 @@ class Tree
attr_reader :root
def initialize
@root = nil
@nodes = []
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(log n) for balanced trees, O(n) for unbalanced trees; where n is the number of nodes
# Space Complexity: O(n), where n is the height
def add(key, value)
Comment on lines +20 to 22

Choose a reason for hiding this comment

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

👍

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

# Time Complexity:
# Space Complexity:
def find(key)
raise NotImplementedError
def add_helper(current, new_node)
if new_node.key <= current.key
return current.left = new_node if !current.left
add_helper(current.left, new_node)
else
return current.right = new_node if !current.right
add_helper(current.right, new_node)
end
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(log n) for balanced trees, O(n) for unbalanced trees; where n is the number of nodes
# Space Complexity: O(n), where n is the height
def find(key, current = @root)
Comment on lines +38 to +40

Choose a reason for hiding this comment

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

👍

return if !@root
return current.value if current.key == key

key <= current.key ? find(key, current.left) : find(key, current.right)
end

# Time Complexity: O(n), where n is the number of nodes
# Space Complexity: O(n), where n is the height
def inorder
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.

👍

raise NotImplementedError
return @nodes if !@root
inorder_helper(@root)
return @nodes
end

# Time Complexity:
# Space Complexity:
def inorder_helper(current)
return if !current
# traverse the left subtree
inorder_helper(current.left)
# visit the current node
@nodes.push(
{
key: current.key,
value: current.value
}
)
#traverse the right subtree
inorder_helper(current.right)
end

# Time Complexity: O(n), where n is the number of nodes
# Space Complexity: O(n), where n is the height
def preorder
Comment on lines +70 to 72

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
return @nodes if !@root
preorder_helper(@root)
return @nodes
end

# Time Complexity:
# Space Complexity:
def preorder_helper(current)
return if !current
# visit the current node
@nodes.push(
{
key: current.key,
value: current.value
}
)
# traverse the left subtree
preorder_helper(current.left)
#traverse the right subtree
preorder_helper(current.right)
end

# Time Complexity: O(n), where n is the number of nodes
# Space Complexity: O(n), where n is the height
def postorder
Comment on lines +93 to 95

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
return @nodes if !@root
postorder_helper(@root)
return @nodes
end

# Time Complexity:
# Space Complexity:
def height
raise NotImplementedError
def postorder_helper(current)
return if !current
# traverse the left subtree
postorder_helper(current.left)
#traverse the right subtree
postorder_helper(current.right)
# visit the current node
@nodes.push(
{
key: current.key,
value: current.value
}
)
end

# Time Complexity: O(n), where n is the number of nodes
# Space Complexity: O(n), where n is the height
def height(current = @root, height = 0)
Comment on lines +116 to +118

Choose a reason for hiding this comment

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

👍

# If the current node is nil return 0
return height if !current
# Otherwise return 1 plus the maximum of the heights of the right and left subtrees
left = height(current.left, height + 1)
right = height(current.right, height + 1)
return left >= right ? left : right
end

# Optional Method
Expand Down