I want to implement data structure with operations both pertinent to arrays - i.e. indexing, and linked list - quick access to prev/next item. Resembles sparse array, but the memory is not a concern - the concern is time complexity.
Requirements:
1..N
- you can afford to allocate an array for it (i.e. memory is not a concern)Operations:
insert(key, data)
- O(1)find(key)
- O(1) - returns the "node" with datadelete(node)
- O(1)next(node)
- O(1) - find next occupied node, in the ordering given by keyprev(node)
- O(1)I was thinking of implementation in an array with pointers to the next/prev occupied item, but I have problems in the insert
operation - how do I find the prev and next items, i.e. the place in the double linked list where to put the new item - I don't know how to make this O(1)
.
If this is not possible please provide a proof.
Techopedia Explains Sparse Array Arrays are labeled in ways that show their sequence – for example, in common computer language notation, an array of six variables named A(6) can hold values for A1, A2, A3, A4, A5 and A6. If more than three or four of these values are zero, the array is said to be “sparse.”
If the array is sparse, the value of the length property is greater than the number of elements. Sparse arrays can be created with the Array() constructor or simply by assigning to an array index larger than the current array length . a = new Array ( 5 ); // No elements, but a.
3. What is the difference between a normal(naive) array and a sparse array? Explanation: A naive implementation allocates space for the entire size of the array, whereas a sparse array(linked list implementation) allocates space only for the non-default values.
A sparse array in Java is a data structure which maps keys to values. Same idea as a Map, but different implementation: A Map is represented internally as an array of lists, where each element in these lists is a key,value pair. Both the key and value are object instances.
You can do this with a Van Emde Boas tree.
The tree supports the operations you require:
Insert: insert a key/value pair with an m-bit key Delete: remove the key/value pair with a given key Lookup: find the value associated with a given key FindNext: find the key/value pair with the smallest key at least a given k FindPrevious: find the key/value pair with the largest key at most a given k
And the time complexity is O(logm) where m is the number of bits in the keys.
For example if all your keys are 16 bit integers between 0 and 65535, m would be 16.
EDIT
If the keys are in the range 1..N, the complexity is O(loglogN) for each of these operations.
The tree also supports min and max operations, which would have complexity O(1).
This tree works by using a large array of children trees.
For example, suppose we had 16 bit keys. The first layer of the tree would store an array of 2^8 (=256) children trees. The first child is responsible for keys from 0 to 255, the second for keys 256,257,..,511, etc.
This makes it very easy to lookup to see whether a node is present as we can simply go straight to the corresponding array element.
However, by itself this would make finding the next element hard as we might need to search 256 children trees to find a nonzero element.
The Van Emde Boas tree contains two additions that make it easy to find the next element:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With