Skip to content

Does SortedSet support ceiling/floor method or similiar? #6

@ywchang

Description

@ywchang

Hi,

I was wondering how SortedSet can help achieve the same functionality that Java TreeSet implementing ceiling/floor method? Let's take ceiling for example, it accepts a object as parameter, and tries to find if the current set has a matching element first. If the element exists in set, then return; otherwise, return the next element in the set that is larger than this parameter. If it exists return, otherwise return null.

Below are some test cases.

[1,2,3,4,5]
ceiling(3) => 3
[1,2,3,5]
ceiling(4) => 5
[1,2,3,4,5]
ceiling(6) => null

In Java, since it's using a red-black tree, it can utilise the balancing property to find out this ceiling/floor value very efficiently. However, in Ruby I've no idea how to do it effectively. Would appreciate if you can share any thoughts/comments on this. Many thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions