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

Fire - Alice D #20

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
find method updated; add method implemented
codeandmorecode committed Apr 3, 2021
commit 263d75c58150700f0b02f937d6f7029e009a992c
38 changes: 33 additions & 5 deletions lib/tree.rb
Original file line number Diff line number Diff line change
@@ -19,23 +19,51 @@ def initialize
# Time Complexity:
# Space Complexity:
def add(key, value)
if @root.nil?
@root = TreeNode.new(key, value)
return @root.value
end

current = @root

while key <= current.key || key > current.key
if key == current.key
"This key is already taken."
elsif key < current.key
if current.left.nil?
current.left = TreeNode.new(key,value)
return value
else
current = current.left
end
else
if current.right.nil?
current.right = TreeNode.new(key,value)
return value
else
current = current.right
end
end
end

end


# Time Complexity:
# Space Complexity:
def find(key)

current = @root
while current != nil
if current.key <= key
return true if current.key == key
if current.key == key
return current.value
elsif current.key > key
current = current.left
elsif current.key >= key
return true if current.key == key
elsif current.key < key
current = current.right
end
return false
end
return nil
end

# Time Complexity: