diff --git a/skip_list/Python/SkipList.py b/skip_list/Python/SkipList.py index 3f2d8cf8..4d6abd1c 100644 --- a/skip_list/Python/SkipList.py +++ b/skip_list/Python/SkipList.py @@ -1,122 +1,109 @@ ''' - Skip list is a probabilistic data structure that allows efficient search, insertion and removal operations. - It allows fast search within an ordered sequence of elements, O(log(n)) complexity. + Skip list is a probabilistic data structure that allows efficient search, + insertion and removal operations. + It allows fast search within an ordered sequence of elements, + O(log(n)) complexity. Wikipedia Page: https://en.wikipedia.org/wiki/Skip_list ''' - from random import randint, seed import time - - class SkipNode: ''' - A single node in a SkipList data structure, which is similar to a normal linked list node. + A single node in a SkipList data structure, + which is similar to a normal linked list node. ''' - def __init__(self, height = 0, elem = None): - self.elem = elem - self.next = [None]*height + def __init__(self,height=0,elem=None): + self.elem=elem + self.next=[None]*height class SkipList: - def __init__(self): - # This will set the head node of the list. - self.head = SkipNode() - self.len = 0 - self.maxHeight = 0 - + self.head=SkipNode() + self.len=0 + self.maxHeight=0 def __len__(self): return self.len - ''' - The Search operation takes O(log(n)) time complexity as compared to O(n) in classical linked list. + The Search operation takes O(log(n)) time complexity + as compared to O(n) in classical linked list. ''' - def find(self, elem, update = None): - if update == None: + def find(self,elem,update=None): + if(update==None): update = self.updateList(elem) - if len(update) > 0: + if(len(update)>0): candidate = update[0].next[0] - if candidate != None and candidate.elem == elem: + if(candidate!=None and candidate.elem==elem): return candidate return None - ''' This function returns a randomized hieght for insertion algorithm. ''' def randomHeight(self): - height = 1 - while randint(1, 2) != 1: - height += 1 + height=1 + while randint(1,2)!=1: + height+=1 return height - ''' This function returns the skip list that is below the element which is closest and smaller than the element that is required to find. ''' - def updateList(self, elem): - update = [None]*self.maxHeight - x = self.head + 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 - + while x.next[i]!=None and x.next[i].elem