

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Some concept of Data Structures are Abstract, Balance Factor, Complete Binary Tree, Dynamically, Storage, Implementation, Sequential Search, Advanced Data Structures, Graph Coloring Two, Insertion Sort. Main points of this lecture are: Complete Binary Tree, Non-Intuitive, Powerful List, Array-Based Approach, Implement, Priority Queue, Additional, Items, Left Child, Right Child
Typology: Study notes
1 / 3
This page cannot be seen from the preview
Don't miss anything!


not used 6 15 10 114 20 20 50 300 125 117
Python List actually used to store heap items
a) For the above heap, the list/array indexes are indicated in [ ]'s. For a node at index i , what is the index of:
its left child if it exists:
its right child if it exists:
its parent if it exists:
b) What would the above heap look like after inserting 13 and then 3? (show the changes on above tree)
General Idea of insert(newItem): append newItem to the end of the list (easy to do, but violates heap-order property) restore the heap-order property by repeatedly swapping the newItem with its parent until it percolates to correct spot
c) What is the big-oh notation for inserting a new item in the heap?
d) Complete the code for the percUp method used by insert.
class BinHeap: def init(self): self.heapList = [0] self.currentSize = 0 def percUp(self,currentIndex): paraentIndex = while
def insert(self,k): self.heapList.append(k) self.currentSize = self.currentSize + 1 self.percUp(self.currentSize)
6
15 10
(^114 20 )
300 125 117
[1]
[2] [3]
[4] [5] [6] [7]
[8] [9] (^) [10]
0 1 2 3 4 5 6 7 8 9 10 not used 6 15 10 114 20 20 50 300 125 117
Python List actually used to store heap items
a) What item would delMin remove and return from the above heap?
b) What is the quickest way to fill the hole left by delMin?
c) What new problem does this cause?
General Idea of delMin(): remember the minimum value so it can be returned later (easy to find - at index 1) copy the last item in the list to the root, delete it from the right end, decrement size restore the heap-order property by repeatedly swapping this item with its smallest child until it percolates down to the correct spot return the minimum value
d) What would the above heap look like after delMin? (show the changes on above tree)
e) Complete the code for the percDown method used by delMin.
f) What is the big-oh notation for delMin?
Lecture 7 - Page 2
class BinHeap: . . . def minChild(self,i): if i * 2 + 1 > self.currentSize: # if only left child return i * 2 else: if self.heapList[i * 2] < self.heapList[i * 2 + 1]: return i * 2 else: return i * 2 + 1 def delMin(self): retval = self.heapList[1] self.heapList[1] = self.heapList[self.currentSize] self.currentSize = self.currentSize - 1 self.heapList.pop() self.percDown(1) return retval
def percDown(self,currentIndex):