Skip to content

Commit a4f6258

Browse files
committed
12
1 parent cd1ffea commit a4f6258

File tree

1 file changed

+322
-0
lines changed

1 file changed

+322
-0
lines changed

Heap.ipynb

+322
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,322 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"id": "9b055156",
7+
"metadata": {},
8+
"outputs": [
9+
{
10+
"name": "stdout",
11+
"output_type": "stream",
12+
"text": [
13+
"[None, None, None, None, None, None, None, None, None, None]\n"
14+
]
15+
}
16+
],
17+
"source": [
18+
"heap = [None]*10\n",
19+
"print(heap)"
20+
]
21+
},
22+
{
23+
"cell_type": "code",
24+
"execution_count": 2,
25+
"id": "fb9a5fc9",
26+
"metadata": {},
27+
"outputs": [
28+
{
29+
"name": "stdout",
30+
"output_type": "stream",
31+
"text": [
32+
"[None, 19, 7, 17, 3, 5, 12, 10, 1, 2]\n"
33+
]
34+
}
35+
],
36+
"source": [
37+
"heap[1] = 19\n",
38+
"heap[1*2] = 7\n",
39+
"heap[1*2+1] = 17\n",
40+
"heap[2*2] = 3\n",
41+
"heap[2*2+1] = 5\n",
42+
"heap[3*2] = 12\n",
43+
"heap[3*2+1] = 10\n",
44+
"heap[4*2] = 1\n",
45+
"heap[4*2+1] = 2\n",
46+
"print(heap)"
47+
]
48+
},
49+
{
50+
"cell_type": "code",
51+
"execution_count": 3,
52+
"id": "a5f62588",
53+
"metadata": {},
54+
"outputs": [],
55+
"source": [
56+
"def left(i):\n",
57+
" return i*2\n",
58+
"def right(i):\n",
59+
" return i*2+1\n",
60+
"def parent(i):\n",
61+
" return i//2"
62+
]
63+
},
64+
{
65+
"cell_type": "code",
66+
"execution_count": 4,
67+
"id": "42afc7e8",
68+
"metadata": {},
69+
"outputs": [],
70+
"source": [
71+
"def is_max_heap(h):\n",
72+
" n = len(h) - 1\n",
73+
" for i in range(n,1,-1):\n",
74+
" p = parent(i)\n",
75+
" if h[i] > h[p]:\n",
76+
" return False\n",
77+
" return True"
78+
]
79+
},
80+
{
81+
"cell_type": "code",
82+
"execution_count": 5,
83+
"id": "9c2fcb6c",
84+
"metadata": {},
85+
"outputs": [
86+
{
87+
"name": "stdout",
88+
"output_type": "stream",
89+
"text": [
90+
"[None, 19, 7, 17, 3, 5, 12, 10, 1, 2] True\n",
91+
"[None, 1, 2, 3] False\n"
92+
]
93+
}
94+
],
95+
"source": [
96+
"if __name__ == \"__main__\":\n",
97+
" h = [None,19,7,17,3,5,12,10,1,2]\n",
98+
" print(h, is_max_heap(h))\n",
99+
" h = [None,1,2,3]\n",
100+
" print(h, is_max_heap(h))"
101+
]
102+
},
103+
{
104+
"cell_type": "code",
105+
"execution_count": 6,
106+
"id": "ee834f06",
107+
"metadata": {},
108+
"outputs": [],
109+
"source": [
110+
"def max_heapify(heap,heap_size,i):\n",
111+
" l = left(i)\n",
112+
" r = right(i)\n",
113+
" if l <= heap_size and heap[l] > heap[i]:\n",
114+
" largest = l\n",
115+
" else:\n",
116+
" largest = i\n",
117+
" if r <= heap_size and heap[r] > heap[largest]:\n",
118+
" largest = r\n",
119+
" \n",
120+
" if largest == i:\n",
121+
" return\n",
122+
" heap[i], heap[largest] = heap[largest], heap[i]\n",
123+
" max_heapify(heap,heap_size,largest)\n",
124+
" "
125+
]
126+
},
127+
{
128+
"cell_type": "code",
129+
"execution_count": 7,
130+
"id": "f81ac0d8",
131+
"metadata": {},
132+
"outputs": [
133+
{
134+
"name": "stdout",
135+
"output_type": "stream",
136+
"text": [
137+
"[None, 19, 7, 12, 3, 5, 17, 10, 1, 2]\n",
138+
"[None, 19, 7, 17, 3, 5, 12, 10, 1, 2]\n",
139+
"\n",
140+
"[None, 1, 2, 3]\n",
141+
"[None, 3, 2, 1]\n"
142+
]
143+
}
144+
],
145+
"source": [
146+
"if __name__ == \"__main__\":\n",
147+
" h = [None,19,7,12,3,5,17,10,1,2]\n",
148+
" print(h)\n",
149+
" max_heapify(h,9,3)\n",
150+
" print(h)\n",
151+
" print()\n",
152+
" h = [None,1,2,3]\n",
153+
" print(h)\n",
154+
" max_heapify(h,3,1)\n",
155+
" print(h)\n",
156+
" "
157+
]
158+
},
159+
{
160+
"cell_type": "code",
161+
"execution_count": 8,
162+
"id": "40483dbe",
163+
"metadata": {},
164+
"outputs": [],
165+
"source": [
166+
"def build_max_heap(h):\n",
167+
" heap_size = len(h)-1\n",
168+
" \n",
169+
" for i in range(heap_size//2, 0, -1):\n",
170+
" max_heapify(h, heap_size,i)"
171+
]
172+
},
173+
{
174+
"cell_type": "code",
175+
"execution_count": 9,
176+
"id": "42a8e6e9",
177+
"metadata": {},
178+
"outputs": [
179+
{
180+
"name": "stdout",
181+
"output_type": "stream",
182+
"text": [
183+
"Before building heap [None, 12, 7, 1, 3, 10, 17, 19, 2, 5]\n",
184+
"After building heap [None, 19, 10, 17, 5, 7, 12, 1, 2, 3]\n"
185+
]
186+
}
187+
],
188+
"source": [
189+
"if __name__ == \"__main__\":\n",
190+
" h = [None,12,7,1,3,10,17,19,2,5]\n",
191+
" print(\"Before building heap\",h)\n",
192+
" build_max_heap(h)\n",
193+
" print(\"After building heap\",h)\n",
194+
"\n",
195+
" "
196+
]
197+
},
198+
{
199+
"cell_type": "code",
200+
"execution_count": 10,
201+
"id": "dbbf88ec",
202+
"metadata": {},
203+
"outputs": [],
204+
"source": [
205+
"def heap_delete(h,num):\n",
206+
" size = len(h)-1\n",
207+
" last_heap = h[size]\n",
208+
" for i in range(1, size):\n",
209+
" if num == h[i]:\n",
210+
" break\n",
211+
" h[i],h[size] = h[size],h[i]\n",
212+
" h.remove(num)\n",
213+
" for i in range((size//2)-1,1,-1):\n",
214+
" max_heapify(h,size,i)\n",
215+
" return h"
216+
]
217+
},
218+
{
219+
"cell_type": "code",
220+
"execution_count": 11,
221+
"id": "c3f1c615",
222+
"metadata": {},
223+
"outputs": [
224+
{
225+
"name": "stdout",
226+
"output_type": "stream",
227+
"text": [
228+
"[None, 19, 7, 17, 3, 5, 12, 2, 1]\n"
229+
]
230+
}
231+
],
232+
"source": [
233+
"if __name__ == \"__main__\":\n",
234+
" h = [None,19,7,12,3,5,17,10,1,2]\n",
235+
" heap_delete(h,10)\n",
236+
" print(h)"
237+
]
238+
},
239+
{
240+
"cell_type": "code",
241+
"execution_count": 18,
242+
"id": "07c187ba",
243+
"metadata": {},
244+
"outputs": [],
245+
"source": [
246+
"def heap_sort(h):\n",
247+
" build_max_heap(h)\n",
248+
" print(h)\n",
249+
" heap_size = len(h)-1\n",
250+
" \n",
251+
" for i in range(heap_size,1,-1):\n",
252+
" h[1], h[heap_size] = h[heap_size], h[1]\n",
253+
" heap_size -= 1\n",
254+
" max_heapify(h,heap_size,i)\n",
255+
" print(\"sorting...\",h)\n",
256+
" \n",
257+
" \n",
258+
" "
259+
]
260+
},
261+
{
262+
"cell_type": "code",
263+
"execution_count": 19,
264+
"id": "bac192d7",
265+
"metadata": {},
266+
"outputs": [
267+
{
268+
"name": "stdout",
269+
"output_type": "stream",
270+
"text": [
271+
"[None, 19, 7, 17, 3, 5, 12, 10, 1, 2]\n",
272+
"sorting... [None, 2, 7, 17, 3, 5, 12, 10, 1, 19]\n",
273+
"sorting... [None, 1, 7, 17, 3, 5, 12, 10, 2, 19]\n",
274+
"sorting... [None, 10, 7, 17, 3, 5, 12, 1, 2, 19]\n",
275+
"sorting... [None, 12, 7, 17, 3, 5, 10, 1, 2, 19]\n",
276+
"sorting... [None, 5, 7, 17, 3, 12, 10, 1, 2, 19]\n",
277+
"sorting... [None, 3, 7, 17, 5, 12, 10, 1, 2, 19]\n",
278+
"sorting... [None, 17, 7, 3, 5, 12, 10, 1, 2, 19]\n",
279+
"sorting... [None, 7, 17, 3, 5, 12, 10, 1, 2, 19]\n",
280+
"[None, 7, 17, 3, 5, 12, 10, 1, 2, 19]\n"
281+
]
282+
}
283+
],
284+
"source": [
285+
"if __name__ == \"__main__\":\n",
286+
" h = [None,19,7,12,3,5,17,10,1,2]\n",
287+
" heap_sort(h)\n",
288+
" print(h)\n",
289+
" "
290+
]
291+
},
292+
{
293+
"cell_type": "code",
294+
"execution_count": null,
295+
"id": "fc923f37",
296+
"metadata": {},
297+
"outputs": [],
298+
"source": []
299+
}
300+
],
301+
"metadata": {
302+
"kernelspec": {
303+
"display_name": "Python 3 (ipykernel)",
304+
"language": "python",
305+
"name": "python3"
306+
},
307+
"language_info": {
308+
"codemirror_mode": {
309+
"name": "ipython",
310+
"version": 3
311+
},
312+
"file_extension": ".py",
313+
"mimetype": "text/x-python",
314+
"name": "python",
315+
"nbconvert_exporter": "python",
316+
"pygments_lexer": "ipython3",
317+
"version": "3.9.7"
318+
}
319+
},
320+
"nbformat": 4,
321+
"nbformat_minor": 5
322+
}

0 commit comments

Comments
 (0)