Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

add element to heap python

import heapq
H = [21,1,45,78,3,5]
# Covert to a heap
heapq.heapify(H)
print(H)
# Add element
heapq.heappush(H,8)
print(H)
Comment

add element to heap python


def HeapSort(A,T):
    def heapify(A):
        start = (len(A) - 2) / 2
        while start >= 0:
            siftDown(A, start, len(A) - 1)
            start -= 1

    def siftDown(A, start, end):
        root = start
        while root * 2 + 1 <= end:
            child = root * 2 + 1
            if child + 1 <= end and T.count(A[child]) < T.count(A[child + 1]):
                child += 1
            if child <= end and T.count(A[root]) < T.count(A[child]):
                A[root], A[child] = A[child], A[root]
                root = child
            else:
                return

    heapify(A)
    end = len(A) - 1
    while end > 0:
        A[end], A[0] = A[0], A[end]
        siftDown(A, 0, end - 1)
        end -= 1


if __name__ == '__main__':
    text = "the quick brown fox jumped over the the quick brown quick log log"
    heap = list(set(text.split()))
    print heap

    HeapSort(heap,text)
    print heap

Comment

PREVIOUS NEXT
Code Example
Python :: python seconds counter 
Python :: get number of bits on integer in python 
Python :: pandas drop column by index range 
Python :: plt.imshow not showing 
Python :: create a dataframe with series 
Python :: python scond max function 
Python :: dataframe catch data types 
Python :: the four pillars of Op in Python 
Python :: matplotlib title cilpped off 
Python :: openpyxl delete rows 
Python :: python multiply all elements in array by constant 
Python :: sacar la posicion en una lista python 
Python :: pandas rename index values 
Python :: how to make a pygame window 
Python :: howt to make caluclator in python 
Python :: shuffle array python 
Python :: how to see if a proxy is up in python 
Python :: select a value randomly in a set python 
Python :: create folder python 
Python :: extract n grams from text python 
Python :: split dataset into train, test and validation sets 
Python :: fatal error detected failed to execute script 
Python :: display current local time in readable format 
Python :: concat dictionary of dataframes 
Python :: how to access all the elements of a matrix in python using for loop 
Python :: pandas open text file 
Python :: object.image.url email template django 
Python :: python legend outside 
Python :: os.getlogin() python 
Python :: flask clear session 
ADD CONTENT
Topic
Content
Source link
Name
6+6 =