Пример #1
0
 def extract_max(self):
     """
     The method gets the max of that heap 
     actually it removes it so :)
     """
     if self.heap_size < 1:
         raise Exception("The heap is empty sorry")
     
     maximum = self.__getitem__(0)
     super(MaxHeap,self).__setitem__(0,self.__getitem__(self.heap_size-1))
     #remove the item
     self.__delitem__(self.heap_size-1)
     self.heap_size -=1
     #check if that value has broken sth
     max_heapify(self,0,self.heap_size-1)
     return maximum
Пример #2
0
    def new_delete(self,index):
        """
        New delete operation for heap
        """
        if self.heap_size < 1:
            raise Exception("The heap is empty sorry")
        
        #swap it with the latest one
        super(MaxHeap,self).__setitem__(index,self.__getitem__(self.heap_size-1))
        #remove the item
        self.__delitem__(self.heap_size-1)
        self.heap_size -=1
        if index == self.heap_size:
            return

        #print "Index is :",index
        #check firstly if it is bigger than its parent
        if (index > 0) and (self.__getitem__(self.__get_parent_index(index)) < self.__getitem__(index)):
            self.increase_key(index,self.__getitem__(index),bigger=False)
        else:#if not heapify to down
            max_heapify(self,index,self.heap_size-1)