Given an integer $d$, I need to devise a data structure $S$ with the following actions:
- BUILD(S): build the data structure $S$ from $n$ elements in $\Theta(n\lg{n})$
- INSERT(S, k): insert a new element to $S$ with the key $k$ in $\Theta(\lg{n} + d)$
- DELETE(S, k): delete the element in $S$ with the key $k$ in $\Theta(\lg{n} + d)$
- D-SUCCESSOR(S, p): find the $d$ successor of the element pointed by $p$ in $\Theta(1)$
- K-SUCCESSOR(S, p, k): find the $k$ successor of element pointed by $p$ in $\Theta(\lg{n})$
$d$ is part of the data structure and I can't treat it like a const in efficiency calculations. Also I need to use minimal space.
Successor is defined as the next element after given element $p$ if all $n$ elements in $S$ were ordered, thus the k-successor is defined recursively as the successor for the (k-1)-successor, and the d-successor is the k-successor when $k=d$
My idea is to use an Order-Static tree, OST, because it can do actions 1-3 & 5 in $\Theta(n\lg{n})$ and $\Theta(n)$ respectively, and I'm left with action 4, and due to the requirement for $\Theta(1)$ I summarized that each node in the tree needs to have a pointer to its d-successor, and maintaining it is what adds $d$ to the insert and delete actions. But maintaining this additional pointer means I need to update it after every insert and delete for all $d$ predecessors of the element added (or deleted) doing this using OST existing functions will add $\Theta(d\lg{n})$ to both insert and delete making them run in more than $\Theta(\lg{n} + d)$.
So I've decided to add another pointer, in addition to the one to the d-successor. The second pointer will point to the predecessor of each node. In insert after adding the new node to the tree I get the new node successor in $\Theta(\lg{n})$ and update its and the new node's predecessor pointer, then I loop to the previous $d$ predecessors and for each I update the d-successor pointer to be pointer of its predecessor with the last one (the d-predecessor) pointing to the new node. I do the same for deletion, and now both run in $\Theta(\lg{n} + d)$.
Though I found the desired $S$ I'm wondering if I can make it without adding more than 1 pointer? maybe even without additional pointers at all?