python数据结构学习笔记-2016-10-24-02-使用排序列表实现集合ADT

        5.4 集合ADT

        在之前使用列表实现的集合ADT中,使用线性搜索在未排序列表查找元素,这是非常低效的。本节使用排序列表来实现集合ADT

#-*-coding: utf-8-*-

# 使用已排序列表实现集合ADT

class MySet(object):
    def __init__(self):
        self._theElements = list()

    def __len__(self):
        return len(self._theElements)

    def __contains__(self, element):
        ndx = self._findPosition(element)
        return ndx < len(self) and self._theElements[ndx] == element # 注意此处ndx < len(self),因为_findPosition()有可能返回的是len(self),这对应于新元素比底层列表的所有元素都要大

    def add(self, element):
        if element not in self:
            ndx = self._findPosition(element)
            self._theElements.insert(ndx, element)

    def remove(self, element):
        assert element in self, "The element must be in the set."
        ndx = self._findPosition(element)
        self._theElements.pop(ndx)

    def isSubsetOf(self, other):
        for element in self:
            if element not in other:
                return False
        reture True

    # 由于是使用排序列表实现的,说明元素的索引是固定的,因此比较两集合,直接遍历更有效
    def __eq__(self, other):
        if len(self) != len(other):
            return False
        else:
            for i in range(len(self)):
                if self._theElements[i] != other._theElements[i]:
                    return False
            return True

    def union(self, other):
        newSet = MySet()
        a = 0
        b = 0
        while a < len(self) and b < len(other):
            valueA = self._theElements[a]
            valueB = other._theElements[b]
            if valueA < valueB:
                newSet._theElements.append(valueA)
                a += 1
            elif valueA > valueB:
                newSet._theElements.append(valueB)
                b += 1
            else:
                newSet._theElements.append(valueA)
                a += 1
                b += 1
        while a < len(self):
            newSet._theElements.append(self._theElements[a])
            a += 1
        while b < len(other):
            newSet._theElements.append(other._theElements[b])
            b += 1
        return newSet

    def __iter__(self):
        return _SetIterator(self._theElements)

    # 使用二分法查找新元素在底层已排序列表中的索引
    def _findPosition(self, element):
        low = 0
        high = len(self._theElements) - 1
        while low <= high:
            mid = (high + low) / 2
            if self._theElements[mid] == element:
                return mid
            elif self._theElements[mid] > element:
                high = mid - 1
            else:
                low = mid + 1
        return low
        
        辅助方法_findPosition()就是使用二分法查找新元素在底层列表中所应该在的位置,其时间复杂度是O(log n)。由于in操作符同样也引用了此方法,所以__contain__()的时间复杂度是O(log n),注意ndx < len(self),因为_findPosition()有可能返回的是len(self),这对应于新元素比底层列表的所有元素都要大。在isSubsetOf()方法中,使用了in操作符,因此其时间复杂度即为O(n log n)。

        在判断两集合是否相等上,与之前使用未排序列表不同的是,这里因为使用排序列表,元素的索引是固定的,因此直接遍历更有效,时间复杂度是O(n)。

        求两集合的合集,类似于合并两个排序列表,不同的是不能有重复元素。所在在合并排序列表的基础上修改一下,即可。

        最后是未排序列表和已排序列表两种实现方式的时间复杂度比较。

操作未排序排序
s=Set()O(1)O(1)
len(s)O(1)O(1)
x in sO(n)O(log n)
s.add(x)O(n)O(n)
s.isSubsetOf(t)O(n²)O(n)
s == tO(n²)O(n log n)
s.union(t)O(n²)O(n)




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值