How to retrieve elements from sorted TreeSet using Binary Search?
Solution 1
TreeSet
is backed by a NavigableMap
, a TreeMap
specifically. Calling contains()
on a TreeSet
delegates to TreeMap.containsKey()
, which is a binary search implementation.
You can check if an object is contained in the set by using TreeSet.contains()
, but you have to have the object first. If you want to be able to look up and retrieve an object, then a Map
implementation will be better.
Solution 2
You could use TreeSet.floor(), which according to the docs
Returns the greatest element in this set less than or equal to the given element, or null if there is no such element.
Related videos on Youtube
Admin
Updated on October 01, 2022Comments
-
Admin over 1 year
I am trying to merge multiple sorted lists into one TreeSet.. And then I am thinking to apply Binary Search algorithm on that TreeSet to retrieve the element in O(log n) time complexity..
Below is my code in which I am passing List of Lists in in one of my method and combining them into
TreeSet
to avoid duplicacy... All the lists insideinputs
are sorted -private TreeSet<Integer> tree = new TreeSet<Integer>(); public void mergeMultipleLists(final List<List<Integer>> inputs) { tree = new TreeSet<Integer>(); for (List<Integer> input : inputs) { for(Integer ii : input) { tree.add(ii); } } } public List<Integer> getItem(final Integer x) { // extract elements from TreeSet in O(log n) }
- First of all, is this right way to merge multiple sorted lists into TreeSet? Is there any direct way to merge multiple sorted lists in TreeSet efficiently?
- Secondly, how would I extract an element from that TreeSet in O(log n) time complexity? I would like to find an element
x
in thatTreeSet
, if it is there, then return it, if it is not there then return the next largest value from theTreeSet
.
Or may be I am better off to another data structure as compared to which I am using currently?
UPDATED CODE:-
private TreeSet tree = new TreeSet();
public SearchItem(final List<List<Integer>> inputs) { tree = new TreeSet<Integer>(); for (List<Integer> input : inputs) { tree.addAll(input); } } public Integer getItem(final Integer x) { if(tree.contains(x)) { return x; } else { // now how do I extract next largest // element from it if x is not present } }
-
Admin about 10 yearsThanks Mike.. I updated the question with the latest code.. Earlier I misunderstood but after your suggestion, few things got cleared up.. But I have one doubt in my else statement.. Take a look at my code..
-
Mike B about 10 yearsMaybe the
higher()
method inTreeSet
? I've never used it but it looks like what you want. For future reference, you can look at the javadocs first to see if your answer is there: docs.oracle.com/javase/7/docs/api/java/util/TreeSet.html -
JBoy about 9 yearsU sure it uses a binarysearch imp? have a look at
Entry<K,V> getEntry
in TreeMap, to me it looks like a positional 'linked' based search. -
Mike B about 9 yearsIt might have changed since I posted my answer. It was definitely a binary search implementation at the time, though.