Skip to content

Space - Lak #17

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
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
102 changes: 78 additions & 24 deletions lib/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,94 @@ 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) assuming the tree is balanced.

raise NotImplementedError
# raise NotImplementedError
# if the root is nil set the root to be a new node with the given value and return the node.
return @root = TreeNode.new(key, value) if @root.nil?
current = @root
while current != nil
if key <= current.key
if current.left.nil?
current.left = TreeNode.new(key, value)
return
end
current = current.left
else
if current.right.nil?
current.right = TreeNode.new(key, value)
return
end
current = current.right
end
end
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(log n)
# Space Complexity: O(1)
def find(key)
Comment on lines +43 to 45

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
return nil if @root.nil?
current = @root
while current != nil
if key == current.key
return current.value
elsif key > current.key
current = current.right
else
current = current.left
end
end
end

# Time Complexity:
# Space Complexity:
def inorder
raise NotImplementedError
# Time Complexity: O(n)
# Space Complexity: O(n)

def inorder(current = @root, array = [])
Comment on lines +59 to +62

Choose a reason for hiding this comment

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

👍

# traverse the left subtree
# visit the current node
# traverse the right subtree
return array if current.nil?
inorder(current.left, array)
array << { key: current.key, value: current.value }
inorder(current.right, array)

end

# Time Complexity:
# Space Complexity:
def preorder
raise NotImplementedError

# Time Complexity: O(n)
# Space Complexity: O(n)
def preorder(current = @root, array = [])
Comment on lines +73 to +75

Choose a reason for hiding this comment

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

👍

# visit the current node
# traverse the left subtree
# traverse the right subtree
return array if current.nil?
array << { key: current.key, value: current.value }
preorder(current.left, array)
preorder(current.right, array)
end

# Time Complexity:
# Space Complexity:
def postorder
raise NotImplementedError
# Time Complexity: O(n)
# Space Complexity: O(n)

def postorder(current = @root, array = [])
Comment on lines +85 to +88

Choose a reason for hiding this comment

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

👍

# traverse the left subtree
# traverse the right subtree
# visit the current node
return array if current.nil?
postorder(current.left, array)
postorder(current.right, array)
array << { key: current.key, value: current.value }
end

# Time Complexity:
# Space Complexity:
def height
raise NotImplementedError

# Time Complexity: O(n)
# Space Complexity: O(n)
def height(current = @root)
Comment on lines +98 to +100

Choose a reason for hiding this comment

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

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

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

# Optional Method
Expand Down