0% found this document useful (0 votes)
12 views48 pages

14 Heaps-Heapsort

Java - Heaps & Heapsort

Uploaded by

Ikram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views48 pages

14 Heaps-Heapsort

Java - Heaps & Heapsort

Uploaded by

Ikram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

Heaps and Heapsort

15 August 2024 OSU CSE 1


Heaps
• A heap is a binary tree of T that
satisfies two properties:
– Global shape property: it is a complete
binary tree
– Local ordering property: the label in each
node is “smaller than or equal to” the label in
each of its child nodes

15 August 2024 OSU CSE 2


Heaps
• A heap is a binary tree of T that
satisfies two properties:
– Global shape property: it is a complete
binarybinary
A complete tree tree is one in which
all–levels
Localare “full” except
ordering possiblythe
property: thelabel in each
bottom level, with any nodes on the
node is “smaller than or equal
bottom level as far left as possible.
to” the label in
each of its child nodes

15 August 2024 OSU CSE 3


Heaps
•Also
A heap is a binary
in the picture tree of T that
is (as with BSTs,
sorting, etc.) two
satisfies a totalproperties:
preorder that
makes this notion precise.
– Global shape property: it is a complete
binary tree
– Local ordering property: the label in each
node is “smaller than or equal to” the label in
each of its child nodes

15 August 2024 OSU CSE 4


Simplification
• For simplicity in the following illustrations,
we use only one kind of example:
– T = integer
– The ordering is ≤
• Because heaps are used in sorting, where
duplicate values may be involved, we
allow that multiple nodes in a heap may
have the same labels (i.e., we will not
assume that the labels are unique)
15 August 2024 OSU CSE 5
The Big Picture

15 August 2024 OSU CSE 6


The Big Picture
This tree’s root
label y satisfies
x ≤ y

15 August 2024 OSU CSE 7


The Big Picture
Observe: This tree is also
a heap; and for each
node in this tree with
label z, we have:
x ≤ y ≤ z.
x

15 August 2024 OSU CSE 8


The Big PictureThis tree’s root
label y satisfies
x ≤ y

15 August 2024 OSU CSE 9


The Big Picture
Observe: This tree is also
a heap; and for each
node in this tree with
label z, we have:
x ≤ y ≤ z.
x

15 August 2024 OSU CSE 10


Examples of Heaps
7 1 1

2 8 1 8

5 8 2

15 August 2024 OSU CSE 11


Non-Examples of Heaps
1 1 1

2 2 5 2 1

8 1 8

15 August 2024 OSU CSE 12


Non-Examples of Heaps
1 1 1

2 2 5 2 1

8 1 8

Shape property is violated here: not 5


all nodes at the bottom level are as
far left as possible.

15 August 2024 OSU CSE 13


Non-Examples of Heaps
1 1 1

2 2 5 2 1

8 1 8

Ordering property is violated here: 5


value is out of order with that of its
right child.

15 August 2024 OSU CSE 14


Non-Examples of Heaps
1 1 1

2 2 5 2 1

8 1 8

Shape property is violated: two 5


levels are not “full”.

15 August 2024 OSU CSE 15


Heapsort
• A heap can be used to represent the
values in a SortingMachine, as follows:
– In changeToExtractionMode, arrange all
the values into a heap
– In removeFirst, remove the root, and adjust
the slightly mutilated heap to make it a heap
again

15 August 2024 OSU CSE 16


Heapsort
Why should this work?
• A heap can be used to represent the
values in a SortingMachine, as follows:
– In changeToExtractionMode, arrange all
the values into a heap
– In removeFirst, remove the root, and adjust
the slightly mutilated heap to make it a heap
again

15 August 2024 OSU CSE 17


How removeFirst Can Work
• If the root is the only node in the heap,
then after removing it, what remains is
already a heap; nothing left to do
• If the root is not the only node, then
removing it leaves an “opening” that must
be filled by moving some other value in the
heap into the opening

15 August 2024 OSU CSE 18


How removeFirst Can Work
The question is:
• If the root is the only node in
thewhich
heap,
one?
then after removing it, what remains is
already a heap; nothing left to do
• If the root is not the only node, then
removing it leaves an “opening” that must
be filled by moving some other value in the
heap into the opening

15 August 2024 OSU CSE 19


Example: A First Attempt

2 5

4 3

15 August 2024 OSU CSE 20


Example: A First Attempt
If we remove the
root, leaving this
opening ...

2 5

4 3

15 August 2024 OSU CSE 21


Example: A First Attempt
... we might move
up the smaller
child ...
2

4 3

15 August 2024 OSU CSE 22


Example: A First Attempt
... now creating
another opening
...
2

4 3

15 August 2024 OSU CSE 23


Example: A First Attempt
... so, we might
move up the
smaller child.
2

3 5

15 August 2024 OSU CSE 24


Example: A First Attempt
Is the result
necessarily a
heap?
2

3 5

15 August 2024 OSU CSE 25


Example: A Second Attempt

2 5

4 3

15 August 2024 OSU CSE 26


Example: A Second Attempt
This time, let’s
maintain the
shape property ...

2 5

4 3

15 August 2024 OSU CSE 27


Example: A Second Attempt
... by promoting
the last node on
the bottom level.
3

2 5

15 August 2024 OSU CSE 28


Example: A Second Attempt
Now, we can “sift
down” the root into
its proper place ...
3

2 5

15 August 2024 OSU CSE 29


Example: A Second Attempt
... by swapping it
with its smaller
child ...
2

3 5

15 August 2024 OSU CSE 30


Example: A Second Attempt
... and then “sifting
down” the root of
that subtree.
2

3 5

15 August 2024 OSU CSE 31


Example: A Second Attempt
Is the result
necessarily a
heap?
2

3 5

15 August 2024 OSU CSE 32


Pseudo-Contract
/**
* Restores a complete binary tree to be a heap
* if only the root might be out of place.
* @updates t
* @requires
* [t is a complete binary tree] and
* [both subtrees of the root of t are heaps]
* @ensures
* [t is a heap with the same values as #t]
*/
public static void siftDown (BinaryTree<T> t)
{...}

15 August 2024 OSU CSE 33


Building a Heap In the First Place
• Suppose we have n values in a complete
binary tree, but they are arranged without
regard to the heap ordering
• How can we “heapify” it?

15 August 2024 OSU CSE 34


Pseudo-Contract
/**
* Makes a complete binary tree into a heap.
* @updates t
* @requires
* [t is a complete binary tree]
* @ensures
* [t is a heap with the same values as #t]
*/
public static void heapify (BinaryTree<T> t)
{...}

15 August 2024 OSU CSE 35


Hint
• To see how you might implement
heapify, compare the contracts of
siftDown and heapify
• The only difference: before we can call
siftDown to make a heap, both subtrees
of the root must already be heaps
– Once they are heaps, just a call to siftDown
will finish the job

15 August 2024 OSU CSE 36


Hint How do we make
the subtrees into
• To see how you might implementheaps?
heapify, compare the contracts of
siftDown and heapify
• The only difference: before we can call
siftDown to make a heap, both subtrees
of the root must already be heaps
– Once they are heaps, just a call to siftDown
will finish the job

15 August 2024 OSU CSE 37


Example

5 2

1 4

15 August 2024 OSU CSE 38


Example
First, recursively
“heapify” the left
subtree.
3

1 2

5 4

15 August 2024 OSU CSE 39


Example
Then, recursively
“heapify” the right
subtree.
3

1 2

5 4

15 August 2024 OSU CSE 40


Example
Then “sift down” the
root, because now
only the root might
3 be out of place.

1 2

5 4

15 August 2024 OSU CSE 41


Embedding a Heap in an array
• While one could represent a heap using a
BinaryTree<T> (as suggested in the
pseudo-contracts above), it is generally
not done this way
• Instead, a heap is usually represented
“compactly” using an array of T

15 August 2024 OSU CSE 42


Interpreting an array as a Heap

10

20 50

40 30

15 August 2024 OSU CSE 43


Interpreting an array as a Heap
Because it’s a complete
binary tree, the nodes
can be numbered top-to-
0
10 bottom, left-to-right.

1 2
20 50

3 4
40 30

15 August 2024 OSU CSE 44


0
10

1 2
20 50

3 4
40 30

10 20 50 40 30

0 1 2 3 4
15 August 2024 OSU CSE 45
At what index in the
array is the left child of 0
the node at index i? 10

1 2
20 50

3 4
40 30

10 20 50 40 30

0 1 2 3 4
15 August 2024 OSU CSE 46
At what index in the
array is the right child 0
of the node at index i? 10

1 2
20 50

3 4
40 30

10 20 50 40 30

0 1 2 3 4
15 August 2024 OSU CSE 47
Resources
• Wikipedia: Heapsort
– https://en.wikipedia.org/wiki/Heapsort
• Wikipedia: Heap (data structure)
– https://en.wikipedia.org/wiki/Heap_(data_structure)
• Big Java (4th ed), Sections 16.8, 16.9
– https://library.ohio-state.edu/record=b8540788~S7

15 August 2024 OSU CSE 48

You might also like