Skip to content

Commit 411e4ee

Browse files
committed
Heap
1 parent 730ee72 commit 411e4ee

File tree

1 file changed

+135
-0
lines changed

1 file changed

+135
-0
lines changed

Tree/Heap.ipynb

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [
8+
{
9+
"name": "stdout",
10+
"output_type": "stream",
11+
"text": [
12+
"Removing node with two children....\n",
13+
"Removing a leaf node...\n",
14+
"5 \n",
15+
"13 \n",
16+
"14 \n"
17+
]
18+
}
19+
],
20+
"source": [
21+
"\n",
22+
"class Heap(object):\n",
23+
"\n",
24+
" HEAP_SIZE = 10\n",
25+
"\n",
26+
" def __init__(self):\n",
27+
" self.heap = [0]*Heap.HEAP_SIZE;\n",
28+
" self.currentPosition = -1;\n",
29+
"\n",
30+
" def insert(self, item):\n",
31+
"\n",
32+
" if self.isFull():\n",
33+
" print(\"Heap is full..\");\n",
34+
" return \n",
35+
" \n",
36+
" self.currentPosition = self.currentPosition + 1\n",
37+
" self.heap[self.currentPosition] = item\n",
38+
" self.fixUp(self.currentPosition)\n",
39+
" \n",
40+
" def fixUp(self, index):\n",
41+
"\n",
42+
" parentIndex = int((index-1)/2)\n",
43+
"\n",
44+
" while parentIndex >= 0 and self.heap[parentIndex] < self.heap[index]:\n",
45+
" temp = self.heap[index]\n",
46+
" self.heap[index] = self.heap[parentIndex]\n",
47+
" self.heap[parentIndex] = temp\n",
48+
" index=parentIndex\n",
49+
" parentIndex = (int)((index-1)/2)\n",
50+
"\n",
51+
" def heapsort(self):\n",
52+
"\n",
53+
" for i in range(0,self.currentPosition+1):\n",
54+
" temp = self.heap[0]\n",
55+
" print(\"%d \" % temp)\n",
56+
" self.heap[0] = self.heap[self.currentPosition-i]\n",
57+
" self.heap[self.currentPosition-i] = temp\n",
58+
" self.fixDown(0,self.currentPosition-i-1)\n",
59+
"\n",
60+
" def fixDown(self, index, upto):\n",
61+
"\n",
62+
"\t\twhile index <= upto:\n",
63+
"\t\t\n",
64+
"\t\t\tleftChild = 2*index+1\n",
65+
"\t\t\trightChild = 2*index+2\n",
66+
"\t\n",
67+
"\t\t\tif leftChild < upto:\n",
68+
"\t\t\t\tchildToSwap = None\n",
69+
"\t\t\t\t\n",
70+
"\t\t\t\tif rightChild > upto:\n",
71+
"\t\t\t\t\tchildToSwap = leftChild\n",
72+
"\t\t\t\telse:\n",
73+
"\t\t\t\t\tif self.heap[leftChild] > self.heap[rightChild]:\n",
74+
"\t\t\t\t\t\tchildToSwap = leftChild\n",
75+
"\t\t\t\t\telse:\n",
76+
"\t\t\t\t\t\tchildToSwap = rightChild\n",
77+
"\t\t\t\t\n",
78+
"\t\t\t\tif self.heap[index] < self.heap[childToSwap]:\n",
79+
"\t\t\t\t\ttemp = self.heap[index]\n",
80+
"\t\t\t\t\tself.heap[index] = self.heap[childToSwap]\n",
81+
"\t\t\t\t\tself.heap[childToSwap] = temp\n",
82+
"\t\t\t\telse:\n",
83+
"\t\t\t\t\tbreak\n",
84+
"\t\t\t\t\n",
85+
"\t\t\t\tindex = childToSwap\n",
86+
"\t\t\telse:\n",
87+
"\t\t\t\tbreak;\t\t\t\t\t\t\t\n",
88+
"\t\t\t\n",
89+
"\tdef isFull(self):\n",
90+
"\t\tif self.currentPosition == Heap.HEAP_SIZE:\n",
91+
"\t\t\treturn True\n",
92+
"\t\telse:\n",
93+
"\t\t\treturn False\n",
94+
"\t\t\t\n",
95+
"\t\t\t\n",
96+
"if __name__ == \"__main__\":\n",
97+
"\n",
98+
"\theap = Heap()\n",
99+
"\theap.insert(10)\n",
100+
"\theap.insert(-20)\n",
101+
"\theap.insert(0)\n",
102+
"\theap.insert(2)\n",
103+
"\theap.insert(4)\n",
104+
"\theap.insert(5)\n",
105+
"\theap.insert(6)\n",
106+
"\theap.insert(7)\n",
107+
"\theap.insert(20)\n",
108+
"\theap.insert(15)\n",
109+
"\t\n",
110+
"\theap.heapsort()"
111+
]
112+
}
113+
],
114+
"metadata": {
115+
"kernelspec": {
116+
"display_name": "Python 3",
117+
"language": "python",
118+
"name": "python3"
119+
},
120+
"language_info": {
121+
"codemirror_mode": {
122+
"name": "ipython",
123+
"version": 3
124+
},
125+
"file_extension": ".py",
126+
"mimetype": "text/x-python",
127+
"name": "python",
128+
"nbconvert_exporter": "python",
129+
"pygments_lexer": "ipython3",
130+
"version": "3.7.4"
131+
}
132+
},
133+
"nbformat": 4,
134+
"nbformat_minor": 4
135+
}

0 commit comments

Comments
 (0)