Docsity

Assignment 6 on Algorithms and Data Structures | CSE 331, Assignments of Computer Science

Toggle sidebar
bg1
4b example. Prove that we can create a 4-element heap in at
most three comparisons.
Assume we have 4 elements: a, b, c, and d
Step 1: Compare a and b. Without loss of generality (WLOG),
assume that a < b. Then we have the heap:
a
b
Step 2: Compare c and d. Without loss of generality (WLOG),
assume that c < d. Then we have the heap:
c
d
Step 3: Compare a and c. WLOG, assume that a < c. Then
we have the heap:
a
b
c
d
This is a heap of size four. Note that we can switch the children
around since there is no difference between being a left or right
child in a heap structure. You can extend this idea to create a
size 8 heap. It may be helpful for you to create a non-binary (i.e.
> 2 children heap as an intermediate step and figure out how you
can use one or more comparisons to fix it into a binary heap.

Partial preview of the text

Download Assignment 6 on Algorithms and Data Structures | CSE 331 and more Assignments Computer Science in PDF only on Docsity!

4b example. Prove that we can create a 4-element heap in at most three comparisons. Assume we have 4 elements: a, b, c, and d Step 1: Compare a and b. Without loss of generality (WLOG), assume that a < b. Then we have the heap: a b Step 2: Compare c and d. Without loss of generality (WLOG), assume that c < d. Then we have the heap: c d Step 3: Compare a and c. WLOG, assume that a < c. Then we have the heap: a c b d This is a heap of size four. Note that we can switch the children around since there is no difference between being a left or right child in a heap structure. You can extend this idea to create a size 8 heap. It may be helpful for you to create a non-binary (i.e.

2 children heap as an intermediate step and figure out how you can use one or more comparisons to fix it into a binary heap.