Skip to content

Commit 0d97aa6

Browse files
authored
Stack queue heap file upload
1 parent b1828a7 commit 0d97aa6

File tree

1 file changed

+346
-0
lines changed

1 file changed

+346
-0
lines changed

Stacks, Queues & Heaps.ipynb

Lines changed: 346 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,346 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Stacks, Queues & Heaps\n",
8+
"© Joe James, 2019.\n",
9+
"\n",
10+
"### Stack using Python List\n",
11+
"Stack is a LIFO data structure -- last-in, first-out. \n",
12+
"Use append() to push an item onto the stack. \n",
13+
"Use pop() to remove an item."
14+
]
15+
},
16+
{
17+
"cell_type": "code",
18+
"execution_count": 2,
19+
"metadata": {
20+
"scrolled": true
21+
},
22+
"outputs": [
23+
{
24+
"name": "stdout",
25+
"output_type": "stream",
26+
"text": [
27+
"[4, 7, 12, 19]\n"
28+
]
29+
}
30+
],
31+
"source": [
32+
"my_stack = list()\n",
33+
"my_stack.append(4)\n",
34+
"my_stack.append(7)\n",
35+
"my_stack.append(12)\n",
36+
"my_stack.append(19)\n",
37+
"print(my_stack)"
38+
]
39+
},
40+
{
41+
"cell_type": "code",
42+
"execution_count": 3,
43+
"metadata": {},
44+
"outputs": [
45+
{
46+
"name": "stdout",
47+
"output_type": "stream",
48+
"text": [
49+
"19\n",
50+
"12\n",
51+
"[4, 7]\n"
52+
]
53+
}
54+
],
55+
"source": [
56+
"print(my_stack.pop())\n",
57+
"print(my_stack.pop())\n",
58+
"print(my_stack)"
59+
]
60+
},
61+
{
62+
"cell_type": "markdown",
63+
"metadata": {},
64+
"source": [
65+
"### Stack using List with a Wrapper Class\n",
66+
"We create a Stack class and a full set of Stack methods. \n",
67+
"But the underlying data structure is really a Python List. \n",
68+
"For pop and peek methods we first check whether the stack is empty, to avoid exceptions."
69+
]
70+
},
71+
{
72+
"cell_type": "code",
73+
"execution_count": 4,
74+
"metadata": {},
75+
"outputs": [],
76+
"source": [
77+
"class Stack():\n",
78+
" def __init__(self):\n",
79+
" self.stack = list()\n",
80+
" def push(self, item):\n",
81+
" self.stack.append(item)\n",
82+
" def pop(self):\n",
83+
" if len(self.stack) > 0:\n",
84+
" return self.stack.pop()\n",
85+
" else:\n",
86+
" return None\n",
87+
" def peek(self):\n",
88+
" if len(self.stack) > 0:\n",
89+
" return self.stack[len(self.stack)-1]\n",
90+
" else:\n",
91+
" return None\n",
92+
" def __str__(self):\n",
93+
" return str(self.stack)"
94+
]
95+
},
96+
{
97+
"cell_type": "markdown",
98+
"metadata": {},
99+
"source": [
100+
"### Test Code for Stack Wrapper Class"
101+
]
102+
},
103+
{
104+
"cell_type": "code",
105+
"execution_count": 5,
106+
"metadata": {},
107+
"outputs": [
108+
{
109+
"name": "stdout",
110+
"output_type": "stream",
111+
"text": [
112+
"[1, 3]\n",
113+
"3\n",
114+
"1\n",
115+
"1\n",
116+
"None\n"
117+
]
118+
}
119+
],
120+
"source": [
121+
"my_stack = Stack()\n",
122+
"my_stack.push(1)\n",
123+
"my_stack.push(3)\n",
124+
"print(my_stack)\n",
125+
"print(my_stack.pop())\n",
126+
"print(my_stack.peek())\n",
127+
"print(my_stack.pop())\n",
128+
"print(my_stack.pop())"
129+
]
130+
},
131+
{
132+
"cell_type": "markdown",
133+
"metadata": {},
134+
"source": [
135+
"---\n",
136+
"### Queue using Python Deque\n",
137+
"Queue is a FIFO data structure -- first-in, first-out. \n",
138+
"Deque is a double-ended queue, but we can use it for our queue. \n",
139+
"We use append() to enqueue an item, and popleft() to dequeue an item. \n",
140+
"See [Python docs](https://docs.python.org/3/library/collections.html#collections.deque) for deque."
141+
]
142+
},
143+
{
144+
"cell_type": "code",
145+
"execution_count": 6,
146+
"metadata": {},
147+
"outputs": [
148+
{
149+
"name": "stdout",
150+
"output_type": "stream",
151+
"text": [
152+
"deque([5, 10])\n",
153+
"5\n"
154+
]
155+
}
156+
],
157+
"source": [
158+
"from collections import deque\n",
159+
"my_queue = deque()\n",
160+
"my_queue.append(5)\n",
161+
"my_queue.append(10)\n",
162+
"print(my_queue)\n",
163+
"print(my_queue.popleft())"
164+
]
165+
},
166+
{
167+
"cell_type": "markdown",
168+
"metadata": {},
169+
"source": [
170+
"### Fun exercise:\n",
171+
"Write a wrapper class for the Queue class, similar to what we did for Stack, but using Python deque. \n",
172+
"Try adding enqueue, dequeue, and get_size methods."
173+
]
174+
},
175+
{
176+
"cell_type": "markdown",
177+
"metadata": {},
178+
"source": [
179+
"### Python Single-ended Queue Wrapper Class using Deque\n",
180+
"We rename the append method to enqueue, and popleft to dequeue. \n",
181+
"We also add peek and get_size operations."
182+
]
183+
},
184+
{
185+
"cell_type": "code",
186+
"execution_count": 7,
187+
"metadata": {},
188+
"outputs": [],
189+
"source": [
190+
"from collections import deque\n",
191+
"class Queue():\n",
192+
" def __init__(self):\n",
193+
" self.queue = deque()\n",
194+
" self.size = 0\n",
195+
" def enqueue(self, item):\n",
196+
" self.queue.append(item)\n",
197+
" self.size += 1\n",
198+
" def dequeue(self, item):\n",
199+
" if self.size > 0:\n",
200+
" self.size -= 1\n",
201+
" return self.queue.popleft()\n",
202+
" else: \n",
203+
" return None\n",
204+
" def peek(self):\n",
205+
" if self.size > 0:\n",
206+
" ret_val = self.queue.popleft()\n",
207+
" queue.appendleft(ret_val)\n",
208+
" return ret_val\n",
209+
" else:\n",
210+
" return None\n",
211+
" def get_size(self):\n",
212+
" return self.size"
213+
]
214+
},
215+
{
216+
"cell_type": "markdown",
217+
"metadata": {},
218+
"source": [
219+
"### Python MaxHeap\n",
220+
"A MaxHeap always bubbles the highest value to the top, so it can be removed instantly. \n",
221+
"Public functions: push, peek, pop \n",
222+
"Private functions: __swap, __floatUp, __bubbleDown, __str__."
223+
]
224+
},
225+
{
226+
"cell_type": "code",
227+
"execution_count": 11,
228+
"metadata": {},
229+
"outputs": [],
230+
"source": [
231+
"class MaxHeap:\n",
232+
" def __init__(self, items=[]):\n",
233+
" super().__init__()\n",
234+
" self.heap = [0]\n",
235+
" for item in items:\n",
236+
" self.heap.append(item)\n",
237+
" self.__floatUp(len(self.heap) - 1)\n",
238+
"\n",
239+
" def push(self, data):\n",
240+
" self.heap.append(data)\n",
241+
" self.__floatUp(len(self.heap) - 1)\n",
242+
"\n",
243+
" def peek(self):\n",
244+
" if self.heap[1]:\n",
245+
" return self.heap[1]\n",
246+
" else:\n",
247+
" return False\n",
248+
" \n",
249+
" def pop(self):\n",
250+
" if len(self.heap) > 2:\n",
251+
" self.__swap(1, len(self.heap) - 1)\n",
252+
" max = self.heap.pop()\n",
253+
" self.__bubbleDown(1)\n",
254+
" elif len(self.heap) == 2:\n",
255+
" max = self.heap.pop()\n",
256+
" else: \n",
257+
" max = False\n",
258+
" return max\n",
259+
"\n",
260+
" def __swap(self, i, j):\n",
261+
" self.heap[i], self.heap[j] = self.heap[j], self.heap[i]\n",
262+
"\n",
263+
" def __floatUp(self, index):\n",
264+
" parent = index//2\n",
265+
" if index <= 1:\n",
266+
" return\n",
267+
" elif self.heap[index] > self.heap[parent]:\n",
268+
" self.__swap(index, parent)\n",
269+
" self.__floatUp(parent)\n",
270+
"\n",
271+
" def __bubbleDown(self, index):\n",
272+
" left = index * 2\n",
273+
" right = index * 2 + 1\n",
274+
" largest = index\n",
275+
" if len(self.heap) > left and self.heap[largest] < self.heap[left]:\n",
276+
" largest = left\n",
277+
" if len(self.heap) > right and self.heap[largest] < self.heap[right]:\n",
278+
" largest = right\n",
279+
" if largest != index:\n",
280+
" self.__swap(index, largest)\n",
281+
" self.__bubbleDown(largest)\n",
282+
" \n",
283+
" def __str__(self):\n",
284+
" return str(self.heap)"
285+
]
286+
},
287+
{
288+
"cell_type": "markdown",
289+
"metadata": {},
290+
"source": [
291+
"### MaxHeap Test Code"
292+
]
293+
},
294+
{
295+
"cell_type": "code",
296+
"execution_count": 12,
297+
"metadata": {},
298+
"outputs": [
299+
{
300+
"name": "stdout",
301+
"output_type": "stream",
302+
"text": [
303+
"[0, 95, 10, 21, 3]\n",
304+
"95\n",
305+
"21\n"
306+
]
307+
}
308+
],
309+
"source": [
310+
"m = MaxHeap([95, 3, 21])\n",
311+
"m.push(10)\n",
312+
"print(m)\n",
313+
"print(m.pop())\n",
314+
"print(m.peek())"
315+
]
316+
},
317+
{
318+
"cell_type": "code",
319+
"execution_count": null,
320+
"metadata": {},
321+
"outputs": [],
322+
"source": []
323+
}
324+
],
325+
"metadata": {
326+
"kernelspec": {
327+
"display_name": "Python 3",
328+
"language": "python",
329+
"name": "python3"
330+
},
331+
"language_info": {
332+
"codemirror_mode": {
333+
"name": "ipython",
334+
"version": 3
335+
},
336+
"file_extension": ".py",
337+
"mimetype": "text/x-python",
338+
"name": "python",
339+
"nbconvert_exporter": "python",
340+
"pygments_lexer": "ipython3",
341+
"version": "3.7.0"
342+
}
343+
},
344+
"nbformat": 4,
345+
"nbformat_minor": 2
346+
}

0 commit comments

Comments
 (0)