rishiilluri

Untitled

Oct 21st, 2022
791
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.37 KB | None | 0 0
  1.  class PriorityQueue():
  2.     def __init__(self, initial_elements):
  3.         self.pq = []
  4.         self.size = 0
  5.         for element in initial_elements:
  6.             self.insert(element)
  7.            
  8.     def swapPositions(self, pos1, pos2):
  9.         temp=self.pq[pos1]
  10.         self.pq[pos1]=self.pq[pos2]
  11.         self.pq[pos2]=temp
  12.        
  13.     def heapify_bottom_up(self, idx):    
  14.         while idx > 0:
  15.             parent = (idx-1)//2
  16.             if self.pq[idx][0] > self.pq[parent][0]:
  17.                 self.swapPositions(idx, parent)
  18.             else:
  19.                 break
  20.                    
  21.            
  22.     def heapify_top_down(self):
  23.         idx = 0
  24.         while idx<self.size:
  25.             l = idx*2+1
  26.             r = idx*2+2
  27.            
  28.             if l >=self.size:
  29.                 return
  30.             if r >=self.size:
  31.                 if self.pq[l][0] > self.pq[idx][0]:
  32.                     swapPositions(idx, l)
  33.                     idx = l
  34.                 else:
  35.                     return
  36.            
  37.             left_priority = self.pq[l][0]
  38.             right_priority = self.pq[r][0]
  39.            
  40.             left_value = self.pq[l][1]
  41.             right_value = self.pq[r][1]
  42.            
  43.             if self.pq[idx][0] > left_priority and self.pq[idx][0] > right_priority:
  44.                 return
  45.             if right_priority > left_priority and right_priority > self.pq[idx][0]:
  46.                 self.swapPositions(idx, r)
  47.                 idx = r
  48.             if left_priority > right_priority and left_priority > self.pq[idx][0]:
  49.                 self.swapPositions(idx, l)
  50.                 idx = l
  51.  
  52.                      
  53.    
  54.     def insert(self, element):
  55.         self.pq.append(element)
  56.         self.size += 1
  57.         self.heapify_bottom_up(self.size-1)
  58.        
  59.     def delete_root(self):
  60.         self.pq[0] = self.pq[-1]
  61.         self.size -= 1
  62.         self.pq.pop()
  63.         self.heapify_top_down()
  64.        
  65.     def get_max(self):
  66.         top = self.pq[0]
  67.         self.delete_root()
  68.         return top
  69.     def print(self):
  70.         for i in self.pq:
  71.             print(i)
  72.            
  73.            
  74. q = PriorityQueue([(4, "Applied Algorithms")])
  75. q.insert((1, "Database Design"))
  76. q.insert((2, "Data Science"))
  77.  
  78.  
  79. q.print()
  80. k, v = q.get_max()
  81. print(v)
  82. k, v = q.get_max()
  83. print(v)
  84. k, v = q.get_max()
  85. print(v)
  86.  
  87.  
  88. print()
  89. print()
Advertisement
Add Comment
Please, Sign In to add comment