Created
January 1, 2018 19:14
-
-
Save sachinnair90/3bee2ef7dd3ff0dc5aec44ec40e2d127 to your computer and use it in GitHub Desktop.
Simple Skip list implementation in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from random import randint, seed | |
class Node: | |
def __init__(self, height = 0, elem = None): | |
self.elem = elem | |
self.next = [None]*height | |
class SkipList: | |
def __init__(self): | |
self.head = Node() | |
self.len = 0 | |
self.maxHeight = 0 | |
def __len__(self): | |
return self.len | |
def find(self, elem, update = None): | |
if update == None: | |
update = self.updateList(elem) | |
if len(update) > 0: | |
item = update[0].next[0] | |
if item != None and item.elem == elem: | |
return item | |
return None | |
def contains(self, elem, update = None): | |
return self.find(elem, update) != None | |
def randomHeight(self): | |
height = 1 | |
while randint(1, 2) != 1: | |
height += 1 | |
return height | |
def updateList(self, elem): | |
update = [None]*self.maxHeight | |
x = self.head | |
for i in reversed(range(self.maxHeight)): | |
while x.next[i] != None and x.next[i].elem < elem: | |
x = x.next[i] | |
update[i] = x | |
return update | |
def insert(self, elem): | |
_node = Node(self.randomHeight(), elem) | |
self.maxHeight = max(self.maxHeight, len(_node.next)) | |
while len(self.head.next) < len(_node.next): | |
self.head.next.append(None) | |
update = self.updateList(elem) | |
if self.find(elem, update) == None: | |
for i in range(len(_node.next)): | |
_node.next[i] = update[i].next[i] | |
update[i].next[i] = _node | |
self.len += 1 | |
def remove(self, elem): | |
update = self.updateList(elem) | |
x = self.find(elem, update) | |
if x != None: | |
for i in reversed(range(len(x.next))): | |
update[i].next[i] = x.next[i] | |
if self.head.next[i] == None: | |
self.maxHeight -= 1 | |
self.len -= 1 | |
def printList(self): | |
for i in range(len(self.head.next)-1, -1, -1): | |
x = self.head | |
while x.next[i] != None: | |
print x.next[i].elem, | |
x = x.next[i] | |
print '' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/kunigami/blog-examples/blob/master/skip-list/skipList.py