|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Binary Heap Implementation\n", |
| 8 | + "\n", |
| 9 | + "Here is the reference code for the Binary Heap Implementation. Make sure to refer to the video lecture for the full explanation!" |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "markdown", |
| 14 | + "metadata": {}, |
| 15 | + "source": [ |
| 16 | + "### Binary Heap Operations\n", |
| 17 | + "**The basic operations we will implement for our binary heap are as follows:**\n", |
| 18 | + "\n", |
| 19 | + "* BinaryHeap() creates a new, empty, binary heap.\n", |
| 20 | + "* insert(k) adds a new item to the heap.\n", |
| 21 | + "* findMin() returns the item with the minimum key value, leaving item in the heap.\n", |
| 22 | + "* delMin() returns the item with the minimum key value, removing the item from the heap.\n", |
| 23 | + "* isEmpty() returns true if the heap is empty, false otherwise.\n", |
| 24 | + "* size() returns the number of items in the heap.\n", |
| 25 | + "* buildHeap(list) builds a new heap from a list of keys." |
| 26 | + ] |
| 27 | + }, |
| 28 | + { |
| 29 | + "cell_type": "code", |
| 30 | + "execution_count": 1, |
| 31 | + "metadata": { |
| 32 | + "collapsed": true |
| 33 | + }, |
| 34 | + "outputs": [], |
| 35 | + "source": [ |
| 36 | + "class BinHeap:\n", |
| 37 | + " def __init__(self):\n", |
| 38 | + " self.heapList = [0]\n", |
| 39 | + " self.currentSize = 0\n", |
| 40 | + "\n", |
| 41 | + "\n", |
| 42 | + " def percUp(self,i):\n", |
| 43 | + " \n", |
| 44 | + " while i // 2 > 0:\n", |
| 45 | + " \n", |
| 46 | + " if self.heapList[i] < self.heapList[i // 2]:\n", |
| 47 | + " \n", |
| 48 | + " \n", |
| 49 | + " tmp = self.heapList[i // 2]\n", |
| 50 | + " self.heapList[i // 2] = self.heapList[i]\n", |
| 51 | + " self.heapList[i] = tmp\n", |
| 52 | + " i = i // 2\n", |
| 53 | + "\n", |
| 54 | + " def insert(self,k):\n", |
| 55 | + " \n", |
| 56 | + " self.heapList.append(k)\n", |
| 57 | + " self.currentSize = self.currentSize + 1\n", |
| 58 | + " self.percUp(self.currentSize)\n", |
| 59 | + "\n", |
| 60 | + " def percDown(self,i):\n", |
| 61 | + " \n", |
| 62 | + " while (i * 2) <= self.currentSize:\n", |
| 63 | + " \n", |
| 64 | + " mc = self.minChild(i)\n", |
| 65 | + " if self.heapList[i] > self.heapList[mc]:\n", |
| 66 | + " \n", |
| 67 | + " tmp = self.heapList[i]\n", |
| 68 | + " self.heapList[i] = self.heapList[mc]\n", |
| 69 | + " self.heapList[mc] = tmp\n", |
| 70 | + " i = mc\n", |
| 71 | + "\n", |
| 72 | + " def minChild(self,i):\n", |
| 73 | + " \n", |
| 74 | + " if i * 2 + 1 > self.currentSize:\n", |
| 75 | + " \n", |
| 76 | + " return i * 2\n", |
| 77 | + " else:\n", |
| 78 | + " \n", |
| 79 | + " if self.heapList[i*2] < self.heapList[i*2+1]:\n", |
| 80 | + " return i * 2\n", |
| 81 | + " else:\n", |
| 82 | + " return i * 2 + 1\n", |
| 83 | + "\n", |
| 84 | + " def delMin(self):\n", |
| 85 | + " retval = self.heapList[1]\n", |
| 86 | + " self.heapList[1] = self.heapList[self.currentSize]\n", |
| 87 | + " self.currentSize = self.currentSize - 1\n", |
| 88 | + " self.heapList.pop()\n", |
| 89 | + " self.percDown(1)\n", |
| 90 | + " return retval\n", |
| 91 | + "\n", |
| 92 | + " def buildHeap(self,alist):\n", |
| 93 | + " i = len(alist) // 2\n", |
| 94 | + " self.currentSize = len(alist)\n", |
| 95 | + " self.heapList = [0] + alist[:]\n", |
| 96 | + " while (i > 0):\n", |
| 97 | + " self.percDown(i)\n", |
| 98 | + " i = i - 1" |
| 99 | + ] |
| 100 | + }, |
| 101 | + { |
| 102 | + "cell_type": "code", |
| 103 | + "execution_count": null, |
| 104 | + "metadata": { |
| 105 | + "collapsed": true |
| 106 | + }, |
| 107 | + "outputs": [], |
| 108 | + "source": [] |
| 109 | + } |
| 110 | + ], |
| 111 | + "metadata": { |
| 112 | + "kernelspec": { |
| 113 | + "display_name": "Python 2", |
| 114 | + "language": "python", |
| 115 | + "name": "python2" |
| 116 | + }, |
| 117 | + "language_info": { |
| 118 | + "codemirror_mode": { |
| 119 | + "name": "ipython", |
| 120 | + "version": 2 |
| 121 | + }, |
| 122 | + "file_extension": ".py", |
| 123 | + "mimetype": "text/x-python", |
| 124 | + "name": "python", |
| 125 | + "nbconvert_exporter": "python", |
| 126 | + "pygments_lexer": "ipython2", |
| 127 | + "version": "2.7.11" |
| 128 | + } |
| 129 | + }, |
| 130 | + "nbformat": 4, |
| 131 | + "nbformat_minor": 0 |
| 132 | +} |
0 commit comments