How to design a data structure that allows one to search, insert and delete an integer X in O(1) time

后端 未结 4 900
清歌不尽
清歌不尽 2020-12-24 14:26

Here is an exercise (3-15) in the book \"Algorithm Design Manual\".

Design a data structure that allows one to search, insert, and delete an integer X

4条回答
  •  天命终不由人
    2020-12-24 15:15

    I first saw the idea in Cameron's answer in Jon Bentley Programming Pearls.

    The idea is pretty simple but it's not straightforward to see why the initial random values that may be on the uninitialized arrays does not matter. This link explains pretty well the insertion and search operations. Deletion is left as an exercise, but is answered by one of the commenters:

    remove-member(i):
        if not is-member(i): return
        j = dense[n-1];
        dense[sparse[i]] = j;
        sparse[j] = sparse[i];
        n = n - 1
    

提交回复
热议问题