Note that there are some explanatory texts on larger screens.

plurals
  1. POIncrementing values in a search tree after insertion of a key-value pair
    primarykey
    data
    text
    <p>I'm working on a programming problem and am running into a roadblock. I'm trying to come up with a data structure to map an arbitrary integer to another integer. You may be inclined to say "Hash Table!" or "Search Tree!", and I have in fact thought of these (and even tried a dirty implementation). But, there is a catch!</p> <p>Every time I insert or remove a value from the mapping, I want to also increment/decrement (by one or some arbitrary offset) all values greater than or equal to the inserted/removed value. </p> <p>Here's an example.</p> <p>Say I have two lists of integers that I will use for my keys and values in this map:</p> <pre><code>Keys: (1, 6, 18, 21, 24) Vals: (2, 1, 3, 0, 4) </code></pre> <p>Now if I add a key-value pair (7, 1), I want to increment all values greater than or equal to 1 resulting in this: </p> <pre><code>Keys: (1, 6, 7, 18, 21, 24) Vals: (3, 2, 1, 4, 0, 5) </code></pre> <p>And subsequently if I delete the key-value pair (21, 0), this is the result:</p> <pre><code>Keys: (1, 6, 7, 18, 24) Vals: (2, 1, 0, 3, 4) </code></pre> <p>This is rather trivial to do with a couple of lists/arrays and some processing after each insertion/deletion (i.e., going through the values and changing them one-by-one). </p> <p>But, I'm looking for a way to do it more efficiently, perhaps without having to go through the entire list of values and increment/decrementing them. Perhaps by even delaying the increment/decrement until a value (that should have been incremented/decremented) has been requested. </p> <p>Any thoughts? </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload