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