Skip to content

Commit

Permalink
Do loop for retrieval
Browse files Browse the repository at this point in the history
No need to do recursive call.  Perf becomes slightly better.
  • Loading branch information
Hiroshi Nakamura committed Mar 27, 2012
1 parent 9ce4d7c commit a272088
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions lib/rbtree_map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,18 @@ def insert(key, value)

# returns value
def retrieve(key)
case key <=> @key
when -1
@left.retrieve(key)
when 0
@value
when 1
@right.retrieve(key)
ptr = self
while ptr != EMPTY
case key <=> ptr.key
when -1
ptr = ptr.left
when 0
return ptr.value
when 1
ptr = ptr.right
end
end
nil
end

def height
Expand Down

0 comments on commit a272088

Please sign in to comment.