Created
November 14, 2022 09:29
-
-
Save chienhsiang-hung/e533d937f4db749dde5cf918785869af to your computer and use it in GitHub Desktop.
Heap data structure is mainly used to represent a priority queue. In Python, it is available using the “heapq” module. The property of this data structure in Python is that each time the smallest heap element is popped(min-heap).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 19, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"<class 'list'> [1, 3, 9, 7, 5]\n" | |
] | |
} | |
], | |
"source": [ | |
"import heapq\n", | |
"\n", | |
"li = [5,7,9,1,3]\n", | |
"li_copy = li.copy()\n", | |
"\n", | |
"heapq.heapify(li)\n", | |
"heapq.heapify(li_copy)\n", | |
"\n", | |
"print(type(li), li)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 20, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[1, 3, 4, 7, 5, 9]\n", | |
"1\n", | |
"3\n", | |
"4\n", | |
"5\n", | |
"7\n", | |
"9\n", | |
"[]\n", | |
"[1, 3, 9, 7, 5]\n" | |
] | |
} | |
], | |
"source": [ | |
"heapq.heappush(li_copy, 4)\n", | |
"\n", | |
"print(li_copy)\n", | |
"for _ in range(len(li_copy)):\n", | |
" print(heapq.heappop(li_copy))\n", | |
"\n", | |
"print(li_copy)\n", | |
"print(li)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 21, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[1, 3, 4, 7, 5, 9]\n", | |
"1\n", | |
"3\n", | |
"4\n", | |
"5\n", | |
"7\n", | |
"9\n", | |
"[]\n" | |
] | |
} | |
], | |
"source": [ | |
"heapq.heappush(li, 4)\n", | |
"\n", | |
"print(li)\n", | |
"for _ in range(len(li)):\n", | |
" print(heapq.heappop(li))\n", | |
"print(li)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 25, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[1, 3, 9, 7, 5]\n", | |
"1\n", | |
"[2, 3, 9, 7, 5]\n" | |
] | |
} | |
], | |
"source": [ | |
"# push and pop simultaneously\n", | |
"\n", | |
"li = [5,7,9,1,3]\n", | |
"\n", | |
"heapq.heapify(li)\n", | |
"print(li)\n", | |
"\n", | |
"print(heapq.heappushpop(li, 2))\n", | |
"print(li)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 26, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[1, 3, 9, 7, 5]\n", | |
"1\n", | |
"[2, 3, 9, 7, 5]\n" | |
] | |
} | |
], | |
"source": [ | |
"# push and pop simultaneously\n", | |
"\n", | |
"li = [5,7,9,1,3]\n", | |
"\n", | |
"heapq.heapify(li)\n", | |
"print(li)\n", | |
"\n", | |
"print(heapq.heapreplace(li, 2))\n", | |
"print(li)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 28, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[9, 7, 5]\n", | |
"[1, 3, 5]\n" | |
] | |
} | |
], | |
"source": [ | |
"# find largest and smallest\n", | |
"li = [5,7,9,1,3]\n", | |
"heapq.heapify(li)\n", | |
"\n", | |
"print(heapq.nlargest(3, li))\n", | |
"print(heapq.nsmallest(3, li))" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3.9.5 64-bit", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.9.5" | |
}, | |
"orig_nbformat": 4, | |
"vscode": { | |
"interpreter": { | |
"hash": "1b231fd55510270dbc2eaafeacb1ac5de2910453973c3eda2b3f9024c6c2bec4" | |
} | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment