Pyton.
Tworzenie prostej sterty
The heapify (iterowalny) :- Ta funkcja jest używana przekonwertuj iterowalną stertę na stertę struktura danych. tj. w kolejności sterty.
Python3
# importing 'heapq' to implement heap queue> import> heapq> # initializing list> li> => [> 5> ,> 7> ,> 9> ,> 1> ,> 3> ]> # using heapify to convert list into heap> heapq.heapify(li)> # printing created heap> print> (> 'The created heap is : '> ,(> list> (li)))> |
>
>Wyjście
The created heap is : [1, 3, 9, 7, 5]>
Efektywne dołączanie i wysuwanie elementów
- heappush(heap, ele): Ta funkcja służy do wstawiania elementu wymienionego w jej argumentach do sterty. The kolejność jest dostosowywana, tak aby zachować strukturę sterty. heppop(sterta): Ta funkcja służy do usuwania i zwracania najmniejszego elementu ze sterty. Kolejność jest dostosowywana tak, aby zachować strukturę sterty.
Python3
Java podwójnie na ciąg
# importing 'heapq' to implement heap queue> import> heapq> # initializing list> li> => [> 5> ,> 7> ,> 9> ,> 1> ,> 3> ]> # using heapify to convert list into heap> heapq.heapify(li)> # printing created heap> print> (> 'The created heap is : '> , end> => '')> print> (> list> (li))> # using heappush() to push elements into heap> # pushes 4> heapq.heappush(li,> 4> )> # printing modified heap> print> (> 'The modified heap after push is : '> , end> => '')> print> (> list> (li))> # using heappop() to pop smallest element> print> (> 'The popped and smallest element is : '> , end> => '')> print> (heapq.heappop(li))> |
>
>Wyjście
The created heap is : [1, 3, 9, 7, 5] The modified heap after push is : [1, 3, 4, 7, 5, 9] The popped and smallest element is : 1>
Dołączanie i pojawianie się jednocześnie
- heappushpop(heap, ele): - Ta funkcja łączy w jednej instrukcji działanie operacji push i pop, zwiększając wydajność. Po tej operacji porządek na stercie zostaje zachowany. heapreplace(heap, ele): - Ta funkcja również wstawia i usuwa elementy w jednej instrukcji, ale różni się od powyższej funkcji. W tym przypadku element jest najpierw otwierany, a następnie wypychany. oznacza to, że może zostać zwrócona wartość większa niż wypchnięta wartość. heapreplace() zwraca najmniejszą wartość pierwotnie znajdującą się na stercie, niezależnie od wypchniętego elementu, w przeciwieństwie do heappushpop().
Python3
# importing 'heapq' to implement heap queue> import> heapq> # initializing list 1> li1> => [> 5> ,> 1> ,> 9> ,> 4> ,> 3> ]> # initializing list 2> li2> => [> 5> ,> 7> ,> 9> ,> 4> ,> 3> ]> # using heapify() to convert list into heap> heapq.heapify(li1)> heapq.heapify(li2)> # using heappushpop() to push and pop items simultaneously> # pops 2> print> (> 'The popped item using heappushpop() is : '> , end> => '')> print> (heapq.heappushpop(li1,> 2> ))> # using heapreplace() to push and pop items simultaneously> # pops 3> print> (> 'The popped item using heapreplace() is : '> , end> => '')> print> (heapq.heapreplace(li2,> 2> ))> |
>
>Wyjście
The popped item using heappushpop() is : 1 The popped item using heapreplace() is : 3>
Znajdź największe i najmniejsze elementy ze sterty w Pythonie
- nlargest(k, iterable, key = fun): Ta funkcja służy do zwracania k największych elementów z określonego iterowalnego i spełnia klucz, jeśli jest wspomniany. nsmallest(k, iterable, key = fun): Ta funkcja służy do zwracania k najmniejszych elementów z określonego iterowalnego i spełnia klucz, jeśli jest wspomniany.
Python3
pyton __imię__
# Python code to demonstrate working of> # nlargest() and nsmallest()> # importing 'heapq' to implement heap queue> import> heapq> # initializing list> li1> => [> 6> ,> 7> ,> 9> ,> 4> ,> 3> ,> 5> ,> 8> ,> 10> ,> 1> ]> # using heapify() to convert list into heap> heapq.heapify(li1)> # using nlargest to print 3 largest numbers> # prints 10, 9 and 8> print> (> 'The 3 largest numbers in list are : '> , end> => '')> print> (heapq.nlargest(> 3> , li1))> # using nsmallest to print 3 smallest numbers> # prints 1, 3 and 4> print> (> 'The 3 smallest numbers in list are : '> , end> => '')> print> (heapq.nsmallest(> 3> , li1))> |
>
dostępne
>Wyjście
The 3 largest numbers in list are : [10, 9, 8] The 3 smallest numbers in list are : [1, 3, 4]>
Przykład:
Python3
import> heapq> # Initialize a list with some values> values> => [> 5> ,> 1> ,> 3> ,> 7> ,> 4> ,> 2> ]> # Convert the list into a heap> heapq.heapify(values)> # Print the heap> print> (> 'Heap:'> , values)> # Add a new value to the heap> heapq.heappush(values,> 6> )> # Print the updated heap> print> (> 'Heap after push:'> , values)> # Remove and return the smallest element from the heap> smallest> => heapq.heappop(values)> # Print the smallest element and the updated heap> print> (> 'Smallest element:'> , smallest)> print> (> 'Heap after pop:'> , values)> # Get the n smallest elements from the heap> n_smallest> => heapq.nsmallest(> 3> , values)> # Print the n smallest elements> print> (> 'Smallest 3 elements:'> , n_smallest)> # Get the n largest elements from the heap> n_largest> => heapq.nlargest(> 2> , values)> # Print the n largest elements> print> (> 'Largest 2 elements:'> , n_largest)> |
>
>Wyjście
Heap: [1, 4, 2, 7, 5, 3] Heap after push: [1, 4, 2, 7, 5, 3, 6] Smallest element: 1 Heap after pop: [2, 4, 3, 7, 5, 6] Smallest 3 elements: [2, 3, 4] Largest 2 elements: [7, 6]>
Ten program tworzy kolejkę sterty za pomocą modułu heapq w Pythonie i wykonuje różne operacje, takie jak konwersja listy na stertę, dodanie nowej wartości do sterty, usunięcie najmniejszego elementu ze sterty, pobranie n najmniejszych i n największych elementów ze sterty sterta.
Notatka że moduł heapq w Pythonie udostępnia funkcje umożliwiające wykonywanie operacji na stercie lokalnie, bez tworzenia osobnej struktury danych dla sterty. Moduł heapq jest wydajny i łatwy w użyciu, co czyni go popularnym wyborem do implementowania kolejek priorytetowych i innych struktur danych w Pythonie.
Zalety używania kolejki sterty (lub sterty) w Pythonie:
- Wydajny: kolejka sterty to wysoce wydajna struktura danych do zarządzania kolejkami priorytetowymi i stertami w Pythonie. Zapewnia logarytmiczną złożoność czasową dla wielu operacji, co czyni go popularnym wyborem w wielu zastosowaniach. Oszczędność miejsca: kolejki sterty oszczędzają miejsce, ponieważ przechowują elementy w reprezentacji opartej na tablicach, minimalizując obciążenie związane ze strukturami danych opartymi na węzłach, takimi jak listy połączone. Łatwy w użyciu: kolejki sterty w Pythonie są łatwe w obsłudze dzięki prostemu i intuicyjnemu interfejsowi API, który ułatwia wykonywanie podstawowych operacji, takich jak wstawianie, usuwanie i pobieranie elementów ze sterty. Elastyczność: kolejek sterty w Pythonie można używać do implementowania różnych struktur danych, takich jak kolejki priorytetowe, sterty i drzewa binarne, co czyni je wszechstronnym narzędziem do wielu zastosowań.
Wady korzystania z kolejki sterty (lub sterty) w Pythonie:
- Ograniczona funkcjonalność: kolejki sterty są przeznaczone głównie do zarządzania kolejkami priorytetowymi i stertami i mogą nie być odpowiednie w przypadku bardziej złożonych struktur danych i algorytmów. Brak dostępu losowego: Kolejki sterty nie obsługują losowego dostępu do elementów, co utrudnia dostęp do elementów znajdujących się w środku sterty lub modyfikowanie elementów, które nie znajdują się na szczycie sterty. Brak sortowania: Kolejki sterty nie obsługują sortowania, więc jeśli chcesz posortować elementy w określonej kolejności, będziesz musiał użyć innej struktury danych lub algorytmu. Nie są bezpieczne dla wątków: Kolejki sterty nie są bezpieczne dla wątków, co oznacza, że mogą nie nadawać się do użycia w aplikacjach wielowątkowych, gdzie synchronizacja danych ma kluczowe znaczenie.
Ogólnie rzecz biorąc, kolejki sterty są wysoce wydajną i elastyczną strukturą danych do zarządzania kolejkami priorytetowymi i stertami w Pythonie, ale mogą mieć ograniczoną funkcjonalność i mogą nie być odpowiednie dla wszystkich aplikacji.