Complete Binary Tree - Data Structures - Lecture Notes, Study notes of Data Structures and Algorithms

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

2012/2013

Uploaded on 04/30/2013

jut
jut 🇮🇳

4.5

(63)

77 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1. Section 6.6 discusses a very “non-intuitive”, but powerful list/array-based approach to implement a priority queue,
call a binary heap. The list/array is used to store a complete binary tree (a full tree with any additional leaves as far left
as possible) with the items being arranges by heap-order property, i.e., each node is either of its children. An
example of a min heap “viewed” an a complete binary tree would be:
6
15 10
114 20 50
20
300 125 117
[1]
[2] [3]
[4] [5] [6] [7]
[8] [9] [10]
1 23456789100
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)
Data Structures (CS 1520) Lecture 7 Name:_________________
L
ecture 7
- Page
1
Docsity.com
pf3

Partial preview of the text

Download Complete Binary Tree - Data Structures - Lecture Notes and more Study notes Data Structures and Algorithms in PDF only on Docsity!

  1. Section 6.6 discusses a very “non-intuitive”, but powerful list/array-based approach to implement a priority queue, call a binary heap. The list/array is used to store a complete binary tree (a full tree with any additional leaves as far left as possible) with the items being arranges by heap-order property , i.e., each node is ≤ either of its children. An example of a min heap “viewed” an a complete binary tree would be:

[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) 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)

Lecture 7Docsity.com - Page 1

  1. Now let us consider the delMin operation that removes and returns the minimum item.

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):

Docsity.com