How to implement a set?

后端 未结 5 1303
别那么骄傲
别那么骄傲 2021-02-04 16:56

I want to implement a Set in C. Is it OK to use a linked list, when creating the SET, or should I use another approach ?

How do you usually implement your own set (if ne

5条回答
  •  难免孤独
    2021-02-04 17:51

    Sets are typically implemented either as red-black trees (which requires the elements to have a total order), or as an automatically-resizing hashtable (which requires a hash function).

    The latter is typically implemented by having the hashtable double in size and reinserting all elements when a certain capacity threshold (75% works well) is exceeded. This means that inidividual insert operations can be O(n), but when amortized over many operations, it's actually O(1).

提交回复
热议问题