rishiilluri

Untitled

Sep 23rd, 2022
1,374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.95 KB | None | 0 0
  1. ## problem 3
  2. import random
  3. class Node:
  4.     def __init__(self, value, levels):
  5.         self.value = value
  6.         self.next = [None]*(levels+1)
  7.    
  8. class SkipList:
  9.     def __init__(self, no_of_levels, p):
  10.         self.p = p
  11.         self.no_of_levels = no_of_levels
  12.         self.root = Node(-1, no_of_levels)
  13.        
  14.     def get_random_level(self):
  15.         level = 0
  16.         while True:
  17.             if level+1 >= self.no_of_levels:
  18.                 break
  19.             r = random.random()
  20.             if r < self.p:
  21.                 level +=1
  22.             else:
  23.                 break
  24.         return level
  25.        
  26.     def insert(self, value):
  27.         previous = [self.root]*(self.no_of_levels+1)
  28.         current = self.root
  29.        
  30.         cur_level = self.no_of_levels
  31.        
  32.         while cur_level>=0:
  33.             while current.next[cur_level] and current.next[cur_level].value < value:
  34.                 current = current.next[cur_level]
  35.             previous[cur_level] = current
  36.             cur_level-=1
  37.        
  38.        
  39.         random_level = self.get_random_level()
  40.         new_node = Node(value, random_level)
  41.        
  42.         for i in range(random_level+1):
  43.             new_node.next[i] = previous[i].next[i]
  44.             previous[i].next[i] = new_node
  45.            
  46.     def lookup_search(self, value):
  47.         current = self.root
  48.         cur_level = self.no_of_levels
  49.         while cur_level>=0:
  50.             while current.next[cur_level] and current.next[cur_level].value < value:
  51.                 current = current.next[cur_level]
  52.             cur_level-=1
  53.         if current.next[0] and current.next[0].value == value:
  54.             return True
  55.         else:
  56.             self.insert(value)
  57.    
  58.     def delete(self, value):
  59.         previous = [self.root]*(self.no_of_levels+1)
  60.         current = self.root
  61.         cur_level = self.no_of_levels
  62.        
  63.         while cur_level>=0:
  64.             while current.next[cur_level] and current.next[cur_level].value < value:
  65.                 current = current.next[cur_level]
  66.             previous[cur_level] = current
  67.             cur_level-=1
  68.         current = current.next[0]
  69.         if current and current.value == value:
  70.             for i in range(self.no_of_levels+1):
  71.                 if previous[i].next[i] != current:
  72.                     break
  73.                 previous[i].next[i] = current.next[i]  
  74.            
  75.     def print(self):
  76.         head = self.root
  77.         for level in range(self.no_of_levels+1):
  78.             current = head.next[level]
  79.             print(f"Level {level}: ")
  80.             while current:
  81.                 print(current.value, end=" ")
  82.                 current = current.next[level]
  83.             print()
  84.  
  85.    
  86. sl = SkipList(3, 0.6)
  87. sl.insert("iub")
  88. sl.insert("usa")
  89. sl.insert("there")
  90. sl.insert("sort")
  91. sl.lookup_search("god")
  92. sl.lookup_search("word")
  93. sl.lookup_search("iub")
  94. sl.print()
  95. sl.delete("there")
  96. sl.print()
  97. sl.insert("myhome")
  98. sl.print()
  99.  
  100.    
Advertisement
Add Comment
Please, Sign In to add comment