Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the time complexity of floorEntry() method of NavigableMap?

Tags:

java

sortedmap

If I have a NavigableMap already formed. What will be the time required for a floorEntry() operation to execute? will it be O(1) or O(logn)?

for example:

If I have a NavigableMap with n intervals, and I use map.floorEntry(k) for some random k, what will be the time complexity of the execution of the operation?

like image 437
Neil Avatar asked Aug 24 '17 08:08

Neil


People also ask

What is floorEntry in TreeMap?

floorEntry. Returns a key-value mapping associated with the greatest key less than or equal to the given key, or null if there is no such key.

What is a NavigableMap in Java?

The NavigableMap interface is a member of the Java Collection Framework. It belongs to java. util package and It is an extension of SortedMap which provides convenient navigation methods like lowerKey, floorKey, ceilingKey and higherKey, and along with this popular navigation method.

What is the complexity of HashMap?

HashMap has complexity of O(1) for insertion and lookup. HashMap allows one null key and multiple null values. HashMap does not maintain any order.

What is the use of TreeMap in Java?

The TreeMap in Java is used to implement Map interface and NavigableMap along with the AbstractMap Class. The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used.

What are the methods of navigablemap?

The methods of the NavigableMap are given in the following table. Here, K – The type of the keys in the map. V – The type of values mapped in the map. Returns a key-value mapping associated with the least key greater than or equal to the given key, or null if there is no such key.

What are the two methods used to calculate the time complexity?

There are two such methods used, time complexity and space complexity which are discussed below: Time Complexity: The time complexity of an algorithm quantifies the amount of time taken by an algorithm to run as a function of the length of the input.

What is a red-black tree based navigablemap implementation?

A Red-Black tree based NavigableMap implementation. This implementation provides guaranteed log (n) time cost for the containsKey, get, put and remove operations. Algorithms are adaptations of those in Cormen, Leiserson, and Rivest's Introduction to Algorithms.

What is the time complexity of logarithmic time complexity?

Logarithmic time – O (log n) An algorithm is said to have a logarithmic time complexity when it reduces the size of the input data in each step. This indicates that the number of operations is not the same as the input size. The number of operations gets reduced as the input size increases.


1 Answers

NavigableMap is an interface, so I can't answer for any implementation of that interface. However, for the TreeMap implementation, floorEntry() requires log(n) time.

The Javadoc of TreeMap only states that

This implementation provides guaranteed log(n) time cost for the containsKey, get, put and remove operations

but the implementation of floorEntry() is similar to the implementation of get() in terms of complexity.

Both call a helper method (get() calls getEntry() and floorEntry() calls getFloorEntry()) that performs most of the logic, and both of the helper methods have a while loop that advances to the left or right child in each iteration, until it finds what it is looking for or reaches a leaf. Thus the required time is the depth of the tree - O(log(n)).

Here are the implementations of getEntry() and floorEntry():

/**
 * Returns this map's entry for the given key, or {@code null} if the map
 * does not contain an entry for the key.
 *
 * @return this map's entry for the given key, or {@code null} if the map
 *         does not contain an entry for the key
 * @throws ClassCastException if the specified key cannot be compared
 *         with the keys currently in the map
 * @throws NullPointerException if the specified key is null
 *         and this map uses natural ordering, or its comparator
 *         does not permit null keys
 */
final Entry<K,V> getEntry(Object key) {
    // Offload comparator-based version for sake of performance
    if (comparator != null)
        return getEntryUsingComparator(key);
    if (key == null)
        throw new NullPointerException();
    @SuppressWarnings("unchecked")
        Comparable<? super K> k = (Comparable<? super K>) key;
    Entry<K,V> p = root;
    while (p != null) {
        int cmp = k.compareTo(p.key);
        if (cmp < 0)
            p = p.left;
        else if (cmp > 0)
            p = p.right;
        else
            return p;
    }
    return null;
}

/**
 * Gets the entry corresponding to the specified key; if no such entry
 * exists, returns the entry for the greatest key less than the specified
 * key; if no such entry exists, returns {@code null}.
 */
final Entry<K,V> getFloorEntry(K key) {
    Entry<K,V> p = root;
    while (p != null) {
        int cmp = compare(key, p.key);
        if (cmp > 0) {
            if (p.right != null)
                p = p.right;
            else
                return p;
        } else if (cmp < 0) {
            if (p.left != null) {
                p = p.left;
            } else {
                Entry<K,V> parent = p.parent;
                Entry<K,V> ch = p;
                while (parent != null && ch == parent.left) {
                    ch = parent;
                    parent = parent.parent;
                }
                return parent;
            }
        } else
            return p;

    }
    return null;
}
like image 173
Eran Avatar answered Oct 29 '22 22:10

Eran