跳表(Skip List)是一种概率型数据结构,用于实现有序元素集合,支持高效的插入、删除和查找操作。跳表通过在多个层级上维护链表,使得平均时间复杂度可以达到 O ( lo g n )。下面是一个简单的 Python 实现:
pythonCopy Codeimport randomclass Node: def __init__(self, value, level): self.value = value self.forward = [None] * (level + 1)class SkipList: def __init__(self, max_level, p): self.max_level = max_level self.p = p # Probability for random level generation self.header = self._create_node(self.max_level, -1) # Header node, value doesn't matter self.level = 0 # Current highest level in the skip list def _create_node(self, lvl, value): return Node(value, lvl) def _random_level(self): lvl = 0 while random.random() < self.p and lvl < self.max_level: lvl += 1 return lvl def insert(self, value): update = [None] * (self.max_level + 1) current = self.header # Start from the highest level of the skip list for i in range(self.level, -1, -1): while current.forward[i] and current.forward[i].value < value: current = current.forward[i] update[i] = current current = current.forward[0] # If the value is not already present, insert it if current is None or current.value != value: rlevel = self._random_level() if rlevel > self.level: for i in range(self.level + 1, rlevel + 1): update[i] = self.header self.level = rlevel node = self._create_node(rlevel, value) for i in range(rlevel + 1): node.forward[i] = update[i].forward[i] update[i].forward[i] = node def search(self, value): current = self.header for i in range(self.level, -1, -1): while current.forward[i] and current.forward[i].value < value: current = current.forward[i] current = current.forward[0] if current and current.value == value: return True return False def delete(self, value): update = [None] * (self.max_level + 1) current = self.header for i in range(self.level, -1, -1): while current.forward[i] and current.forward[i].value < value: current = current.forward[i] update[i] = current current = current.forward[0] if current is not None and current.value == value: for i in range(self.level + 1): if update[i].forward[i] != current: break update[i].forward[i] = current.forward[i] while self.level > 0 and self.header.forward[self.level] is None: self.level -= 1# 示例用法skip_list = SkipList(3, 0.5) skip_list.insert(3) skip_list.insert(6) skip_list.insert(7) skip_list.insert(9)print(skip_list.search(3)) # 输出 Trueprint(skip_list.search(4)) # 输出 Falseskip_list.delete(3)print(skip_list.search(3)) # 输出 False
http://xuexiao.zgjzs.org
http://jianzhu.zgjzs.org
http://gongcheng.zgjzs.org
http://sheji.zgjzs.org
http://fangwu.zgjzs.org
http://jiaoyu.zgjzs.org
http://kejian.zgjzs.org
http://chengshi.zgjzs.org
http://guihua.zgjzs.org
http://jiaju.zgjzs.org
http://jianli.zgjzs.org
http://zhucai.zgjzs.org
http://jichu.zgjzs.org
http://gaosu.zgjzs.org
http://ditu.zgjzs.org
http://lvyou.zgjzs.org
http://zhishi.zgjzs.org
http://shipin.zgjzs.org
http://peixun.zgjzs.org
http://wenhua.zgjzs.org
http://gongsi.zgjzs.org
http://renwu.zgjzs.org
http://xinwen.zgjzs.org
http://zixun.zgjzs.org
http://anli.zgjzs.org
http://guwen.zgjzs.org
http://zhaopin.zgjzs.org
http://yingxiao.zgjzs.org
http://guanli.zgjzs.org
http://peitao.zgjzs.org
http://jixie.zgjzs.org
http://zhuangxiu.zgjzs.org
http://anquan.zgjzs.org
http://chengguo.zgjzs.org
http://baogao.zgjzs.org
http://chengjiu.zgjzs.org
http://zhanshi.zgjzs.org
http://huodong.zgjzs.org
http://huiyi.zgjzs.org
http://tongji.zgjzs.org
http://jiaoliu.zgjzs.org
http://keti.zgjzs.org
http://jingcai.zgjzs.org
http://tuandui.zgjzs.org
http://jingying.zgjzs.org
http://wenjian.zgjzs.org
http://baoming.zgjzs.org
http://wangzhan.zgjzs.org
http://xiazai.zgjzs.org
http://chengxin.zgjzs.org
http://nanzhuang.shangpin.org
http://xiezi.shangpin.org
http://baozi.shangpin.org
http://yundong.shangpin.org
http://shuma.shangpin.org
http://diannao.shangpin.org
http://shouji.shangpin.org
http://jiajv.shangpin.org
http://canju.shangpin.org
http://muying.shangpin.org
http://shipin.shangpin.org
http://jiushui.shangpin.org
http://jiafang.shangpin.org
http://shouna.shangpin.org
http://meishi.shangpin.org
http://lingshi.shangpin.org
http://chufang.shangpin.org
http://jianshen.shangpin.org
http://qiche.shangpin.org
http://yundongyi.shangpin.org
http://bangong.shangpin.org
http://wenju.shangpin.org
http://shuben.shangpin.org
http://dianqi.shangpin.org
http://jiadian.shangpin.org
http://fanghuo.shangpin.org
http://fangshui.shangpin.org
http://fangdao.shangpin.org
http://muyuan.shangpin.org
http://fengshui.shangpin.org
http://zhuangxiu.shangpin.org
http://gongju.shangpin.org
http://chaju.shangpin.org
http://weisheng.shangpin.org
http://xiangbao.shangpin.org
http://yanjing.shangpin.org
http://shoubiao.shangpin.org
http://shipinbao.shangpin.org
http://jiazhuang.shangpin.org
http://chuanglian.shangpin.org
http://yundongxie.shangpin.org
http://tiyu.shangpin.org
http://luyou.shangpin.org
http://jiejing.shangpin.org
http://shumaerji.shangpin.org
http://chejian.shangpin.org
http://xihu.shangpin.org
http://zhizao.shangpin.org
http://cheshen.shangpin.org
http://zhinen.shangpin.org
http://xinwen.snjrw.org
http://zixun.snjrw.org
http://keji.snjrw.org
http://jiaoyu.snjrw.org
http://renwu.snjrw.org
http://wenhua.snjrw.org
http://lishi.snjrw.org
http://jingji.snjrw.org
http://caijing.snjrw.org
http://jinrong.snjrw.org
http://touzi.snjrw.org
http://fangchan.snjrw.org
http://chanye.snjrw.org
http://gongye.snjrw.org
http://qiche.snjrw.org
http://shuma.snjrw.org
http://yule.snjrw.org
http://dianying.snjrw.org
http://yinyue.snjrw.org
http://meiti.snjrw.org
http://hulianwang.snjrw.org
http://chuangye.snjrw.org
http://gongsi.snjrw.org
http://baike.snjrw.org
http://zhishi.snjrw.org
http://wenxue.snjrw.org
http://yishu.snjrw.org
http://zhanlan.snjrw.org
http://huodong.snjrw.org
http://jiaoliu.snjrw.org
http://keti.snjrw.org
http://xueshu.snjrw.org
http://wenjian.snjrw.org
http://baogao.snjrw.org
http://shuju.snjrw.org
http://diaocha.snjrw.org
http://mingren.snjrw.org
http://jiemu.snjrw.org
http://shipin.snjrw.org
http://zhuanlan.snjrw.org
http://zhuanti.snjrw.org
http://wangzhan.snjrw.org
http://wangluo.snjrw.org
http://zhanzhang.snjrw.org
http://shichang.snjrw.org
http://guanli.snjrw.org
http://anquan.snjrw.org
http://jishu.snjrw.org
http://xiazai.snjrw.org
http://wenzhang.snjrw.org