代码之家  ›  专栏  ›  技术社区  ›  Z-Y.L

除了二叉搜索树外,是否有有效的数据结构来表示集合

  •  0
  • Z-Y.L  · 技术社区  · 5 年前

    我们知道,在Haskell中,一组元素在 Data.Set Ord 上课。

    然而,一般集合不需要其elememt作为 作战需求文件 . 因为集合没有重复的元素,所以它的元素是 Eq 上课就够了。

    [a] ,但是单链接列表的效率不如BST,而BST需要 作战需求文件 上课。

    set 类不需要其元素是可排序的。只有 __eq__ __ne__ 情商 类)定义足够,例如:

    class Fruit:
        def __init__(self, name):
            self.name = name.title()
    
        def __repr__(self):
            return self.name
    
        def __eq__(self, other):
            return self.name == other.name     # defines the equality operation
    
        def __ne__(self, other):
            return self.name != other.name     # defines the unequality operation
    
        def __hash__(self):
            return hash(self.name)     # so that Fruit instance can be an element of a set
    

    ?:x=水果('苹果')

    ?:y=水果('苹果')

    ?:z=水果('苹果')

    {苹果}

    ?:x<=y

    回溯(最近一次呼叫时间):

    文件“”,模块中的第1行

    x <= y
    

    所以我想知道Haskell中是否有一些有效的数据结构可以用来表示 Set 但不需要 作战需求文件

    0 回复  |  直到 5 年前
        1
  •  3
  •   John F. Miller    5 年前

    蟒蛇在作弊。 All values in Python are hashable and set() is just a dictionary with no value.

    Haskell作为一种静态类型语言,尤其是具有一类函数的静态类型语言,不能为数据类型创建任意顺序。因此 Data.Set 要求 Ord