So, I'm trying to implement a Data Structure to handle Dynamic Order Statistic. The Data Structure has following operations:
- add(x): inserts a new element with value x
- get(k): returns the k-th smallest element: k = ceiling(n/a), where n = amount of elements in the data structure and a = constant factor.
- reset: resets the whole datastructuer, i.e. the data structure is "empty after it
I implemented my data structure using a balanced AVL tree. Using this the operations have following time complexity:
- add(x): O(log(n))
- get(k): O(log(n))
Here is my implemenation for the get(k) which uses O(log(n)) time:
public static int get(Node current, int k) {
int l = tree.sizeLeft(current) + 1;
if(k == l) {
return current.value;
} else if(k < l) {
if(current.left == null) {
return current.value;
}
return get(current.left, k);
} else {
if(current.right == null) {
return current.value;
}
return get(current.right, k);
}
}
And here's my implementation for the node class:
class Node {
int height, value, bal, size; // bal = balanceFactor, size = amount of nodes in tree
rooted at current node
Node leftChild = null;
Node rightChild = null;
public Node(int val) {
value = val;
height = 1;
size = 1;
}
}
However, my task is to implement a data structure that can handle the above operations and only taking O(1) (constant) time for the operation get(k). (And add(x) still taking O(log(n)) time). Also, I'm not allowed to use a hashmap.
Is it possible to modify my implementation in order to get constant time? Or, what kind of datastructure can handle the get(k) operation in constant time?