Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.63 KB | None | 0 0
  1. __license__ = 'Nathalie (c) EPITA'
  2. __docformat__ = 'reStructuredText'
  3. __revision__ = '$Id: heap.py 2019-03-07'
  4.  
  5. """
  6. Heap homework
  7. 2019
  8. @author: login
  9. """
  10.  
  11. #given function
  12.  
  13. def Heap():
  14.   """ returns a fresh new empty heap
  15.       :rtype: list (heap)
  16.  """
  17.   return [None]
  18.    
  19. #---------------------------------------------------------------
  20.    
  21. def is_empty(H):
  22.   """ tests if H is empty
  23.        :param H: the heap
  24.        :rtype: bool
  25.  """
  26.   return len(H) < 2
  27.  
  28. def push(H, elt, val):
  29.   """ adds the pair (val, elt) to heap H (in place)
  30.    
  31.       :param H: The heap
  32.       :param elt: The element to add
  33.       :param val: The value associated to elt
  34.  """
  35.   i = len(H)
  36.   H.append((val,elt))
  37.  
  38.   while( i != 0 and i//2 != 0 and H[i][0] < H[i//2][0]):
  39.     H[i] = H[i//2]
  40.     H[i//2] = (val, elt)
  41.     i = i//2
  42.  
  43. def __heapify(L,i):
  44.   """
  45.    outputs a heap containing the elements of a given list in the correct minHeap order
  46.    :param L: the given list
  47.    :param i: the index of the root
  48.    
  49.    :rtype: Heap
  50.  """
  51.   if(2*i < len(L) and L[i][0] > L[2*i][0]):
  52.     temp = L[i]
  53.     L[i] = L[2*i]
  54.     L[2*i] = temp
  55.  
  56.     __heapify(L, 2*i)
  57.  
  58.   if(2*i+1 < len(L) and L[i][0] > L[2*i+1][0]):
  59.     temp = L[i]
  60.     L[i] = L[2*i+1]
  61.     L[2*i+1] = temp
  62.  
  63.     __heapify(L, 2*i+1)
  64.  
  65. def pop(H):
  66.     """ removes and returns the pair of smallest value in the heap H
  67.    
  68.       :param H: The heap
  69.       :rtype: (num, any) (the removed pair)
  70.    """
  71.     if(len(H) > 2):
  72.       output = H[1]
  73.       H[1] = H.pop()
  74.  
  75.       __heapify(H,1)
  76.    
  77.       return output
  78.     else:
  79.       return H.pop()
  80. #---------------------------------------------------------------
  81. def __isInMinHeapOrder(T,i):
  82.   #I hope the name is explicit enough
  83.   output = True
  84.  
  85.   if(2*i < len(T)):
  86.     if (T[i] < T[2*i]):
  87.       output = __isInMinHeapOrder(T,2*i)
  88.     else:
  89.       return False
  90.  
  91.   if(output and 2*i+1 < len(T)):
  92.     if (T[i] < T[2*i+1]):
  93.       output = __isInMinHeapOrder(T,2*i+1)
  94.     else:
  95.       return False
  96.  
  97.   return output
  98.  
  99. def is_heap(T):
  100.     """ tests whether the complete tree T is a heap
  101.    
  102.       :param T: a complete tree in hierarchical representation
  103.       :rtype: bool
  104.    """
  105.     return __isInMinHeapOrder(T,1)
  106.    
  107. def heap_sort(L):
  108.     """ sorts the associative list L in increasing order according to values
  109.    
  110.        :param L: a list containing pairs (element, value)
  111.        :rtype: (any, num) list (the list sorted)
  112.    """
  113.     H = Heap()
  114.     for pair in L:
  115.       push(H, pair[1], pair[0])
  116.    
  117.     output = []
  118.     while len(H) > 1:
  119.       output.append(pop(H))
  120.    
  121.     return output
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement