C - How to implement Set data structure?

后端 未结 4 804
梦谈多话
梦谈多话 2020-11-30 19:10

Is there any tricky way to implement a set data structure (a collection of unique values) in C? All elements in a set will be of the same type and there is a huge RAM memory

4条回答
  •  孤城傲影
    2020-11-30 19:15

    Sets are usually implemented as some variety of a binary tree. Red black trees have good worst case performance.

    These can also be used to build an map to allow key / value lookups.

    This approach requires some sort of ordering on the elements of the set and the key values in a map.

    I'm not sure how you would manage a set that could possibly contain itself using binary trees if you limit set membership to well defined types in C ... comparison between such constructs could be problematic. You could do it easily enough in C++, though.

提交回复
热议问题