Method: Containers::RubySplayTreeMap#delete
- Defined in:
- lib/containers/splay_tree_map.rb
#delete(key) ⇒ Object
Deletes the item and key if it’s found, and returns the item. Returns nil if key is not present.
Complexity: amortized O(log n)
map = Containers::SplayTreeMap.new
map["MA"] = "Massachusetts"
map["GA"] = "Georgia"
map.delete("GA") #=> "Georgia"
map.delete("DE") #=> nil
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/containers/splay_tree_map.rb', line 191 def delete(key) return nil if @root.nil? deleted = nil splay(key) if (key <=> @root.key) == 0 # The key exists deleted = @root.value if @root.left.nil? @root = @root.right else x = @root.right @root = @root.left splay(key) @root.right = x end end deleted end |