Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Binary Heap Assignment: Algorithms, CScD-320, Spring 2009, Assignments of Algorithms and Programming

Instructions for assignment 8 of the algorithms course, cscd-320, spring 2009. Students are required to draw the corresponding complete binary tree, heapify the tree as a min heap, insert elements using the bubbling up process, and perform remove min operations. Tree and array formats for each step.

Typology: Assignments

Pre 2010

Uploaded on 08/18/2009

koofers-user-bf4
koofers-user-bf4 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CScD-320, Algorithms, Spring 2009
Assignment 8: Binary Heap — Key
Sahni, DSAAJ, Problem 13.9 (p. 515), initial data altered and expanded.
Consider the array theHeap = [-, 12, 8, 1, 4, 2, 13, 10, 16, 15, 5, 7, 6]. Note that Sahni has the
heap rooted at subscript [1].
a. Draw the corresponding complete binary tree.
Initial state:
12
8 1
4 2 13 10
16 15 5 7 6
b. Heapify the tree as a min heap by making appropriate use of the method of Program 13.4
[equivalent with the method covered in lecture]. [In other words, if using Program 13.4,
adjust for min heap versus max heap.] Show each step in tree format and show the final
result in both tree and array format.
Adjusting from 6 down:
12
8 1
4 2 6 10
16 15 5 7 13
Adjusting from 5 down:
12
8 1
4 2 6 10
16 15 5 7 13
Adjusting from 4 down:
12
8 1
4 2 6 10
16 15 5 7 13
Adjusting from 3 down:
12
8 1
4 2 6 10
16 15 5 7 13
Adjusting from 2 down:
12
2 1
4 5 6 10
16 15 8 7 13
Adjusting from 1 down:
1
2 6
4 5 12 10
16 15 8 7 13
[-, 1, 2, 6, 4, 5, 12, 10, 16, 15, 8, 7, 13]
Printed 2020/ 11 /30 at 02:27 Page 1
pf2

Partial preview of the text

Download Binary Heap Assignment: Algorithms, CScD-320, Spring 2009 and more Assignments Algorithms and Programming in PDF only on Docsity!

CScD-320, Algorithms, Spring 2009

Assignment 8: Binary Heap — Key

Sahni, DSAAJ, Problem 13.9 (p. 515), initial data altered and expanded.

Consider the array theHeap = [-, 12, 8, 1, 4, 2, 13, 10, 16, 15, 5, 7, 6]. Note that Sahni has the

heap rooted at subscript [1].

a. Draw the corresponding complete binary tree.

Initial state: 12 8 1 4 2 13 10 16 15 5 7 6

b. Heapify the tree as a min heap by making appropriate use of the method of Program 13.

[equivalent with the method covered in lecture]. [In other words, if using Program 13.4,

adjust for min heap versus max heap.] Show each step in tree format and show the final

result in both tree and array format.

Adjusting from 6 down: 12 8 1 4 2 6 10 16 15 5 7 13 Adjusting from 5 down: 12 8 1 4 2 6 10 16 15 5 7 13 Adjusting from 4 down: 12 8 1 4 2 6 10 16 15 5 7 13 Adjusting from 3 down: 12 8 1 4 2 6 10 16 15 5 7 13 Adjusting from 2 down: 12 2 1 4 5 6 10 16 15 8 7 13 Adjusting from 1 down: 1 2 6 4 5 12 10 16 15 8 7 13 [-, 1, 2, 6, 4, 5, 12, 10, 16, 15, 8, 7, 13] Printed 2020/ 11 月/30 at 02:27 Page 1

Page 2

c. Now insert 14, 3, 11, and 9 (in this order) using the bubbling up process of Program 13.2,

adjusted for min heap [equivalently the upheap method from lecture]. Show the min

heap in tree format following each insert, and show the heap in array format at the end.

Inserting 14 1 2 6 4 5 12 10 16 15 8 7 13 14 Inserting 3 1 2 3 4 5 12 6 16 15 8 7 13 14 10 Inserting 11 1 2 3 4 5 12 6 16 15 8 7 13 14 10 11 Inserting 9 1 2 3 4 5 12 6 9 15 8 7 13 14 10 11 16 [-, 1, 2, 3, 4, 5, 12, 6, 9, 15, 8, 7, 13, 14, 10, 11, 16]

d. Perform three remove min operations on the min heap of part c. Use the remove method

of Program 13.3 (with appropriate min heap adjustments) [or as developed in lecture].

Show the min heap in tree format following each remove, and show the heap in array

format at the end.

Remove min return 1 2 4 3 9 5 12 6 16 15 8 7 13 14 10 11 Remove min return 2 3 4 6 9 5 12 10 16 15 8 7 13 14 11 Remove min return 3 4 5 6 9 7 12 10 16 15 8 11 13 14 [-, 4, 5, 6, 9, 7, 12, 10, 16, 15, 8, 11, 13, 14] Printed 2020/ 11 月/30 at 02: