0% found this document useful (0 votes)
35 views29 pages

Pertemuan 13 (Materi Heap)

A heap is a complete binary tree where each node's entry is greater than or equal to its children's entries. To add an entry, place it at the next available spot and perform reheapification upward. To remove the biggest entry, move the last node to the root and perform reheapification downward.

Uploaded by

Muh tito
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)
35 views29 pages

Pertemuan 13 (Materi Heap)

A heap is a complete binary tree where each node's entry is greater than or equal to its children's entries. To add an entry, place it at the next available spot and perform reheapification upward. To remove the biggest entry, move the last node to the root and perform reheapification downward.

Uploaded by

Muh tito
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/ 29

Heaps

A heap is a certain kind


of complete binary
tree.
Heaps
Root

A heap is a certain kind


of complete binary
tree.

When a complete
binary tree is built,
its first node must be
the root.
Heaps

Complete binary tree. Left child


of the
root

The second node is


always the left child
of the root.
Heaps

Complete binary tree. Right child


of the
root

The third node is


always the right child
of the root.
Heaps

Complete binary tree.

The next nodes


always fill the next
level from left-to-right.
Heaps

Complete binary tree.

The next nodes


always fill the next
level from left-to-
right.
Heaps

Complete binary tree.

The next nodes


always fill the next
level from left-to-
right.
Heaps

Complete binary tree.

The next nodes


always fill the next
level from left-to-
right.
Heaps

Complete binary tree.


Heaps

45
A heap is a certain kind
of complete binary
tree. 35 23

27 21 22 4

19
Each node in a heap
contains a key that
can be compared to
other nodes' keys.
Heaps

45
A heap is a certain kind
of complete binary
tree. 35 23

27 21 22 4

19
The "heap property"
requires that each
node's key is >= the
keys of its children
Adding a Node to a Heap

45
 Put the new node in
the next available
spot. 35 23
 Push the new node
upward, swapping 27 21 22 4
with its parent until
the new node reaches
19 42
an acceptable
location.
Adding a Node to a Heap

45
 Put the new node in
the next available
spot. 35 23
 Push the new node
upward, swapping 42 21 22 4
with its parent until
the new node reaches
19 27
an acceptable
location.
Adding a Node to a Heap

45
 Put the new node in
the next available
spot. 42 23
 Push the new node
upward, swapping 35 21 22 4
with its parent until
the new node reaches
19 27
an acceptable
location.
Adding a Node to a Heap

45
 The parent has a key
that is >= new node, or
 The node reaches the
42 23
root.
 The process of pushing 35 21 22 4
the new node upward
is called
reheapification 19 27
upward.
Removing the Top of a Heap

45
 Move the last node
onto the root.
42 23

35 21 22 4

19 27
Removing the Top of a Heap

27
 Move the last node
onto the root.
42 23

35 21 22 4

19
Removing the Top of a Heap

27
 Move the last node
onto the root.
42 23
 Push the out-of-place
node downward,
swapping with its 35 21 22 4
larger child until the
new node reaches an
19
acceptable location.
Removing the Top of a Heap

42
 Move the last node
onto the root.
27 23
 Push the out-of-place
node downward,
swapping with its 35 21 22 4
larger child until the
new node reaches an
19
acceptable location.
Removing the Top of a Heap

42
 Move the last node
onto the root.
35 23
 Push the out-of-place
node downward,
swapping with its 27 21 22 4
larger child until the
new node reaches an
19
acceptable location.
Removing the Top of a Heap

42
 The children all have
keys <= the out-of-
place node, or 35 23
 The node reaches the
leaf.
27 21 22 4
 The process of
pushing the new node
downward is called 19
reheapification
downward.
Implementing a Heap

42
 We will store the data
from the nodes in a
partially-filled array. 35 23

27 21

An array of data
Implementing a Heap

42
 Data from the root goes in
the first
location of the 35 23
array.

27 21

42

An array of data
Implementing a Heap

42
 Data from the next row
goes in the next two array
locations. 35 23

27 21

42 35 23

An array of data
Implementing a Heap

42
 Data from the next row
goes in the next two array
locations. 35 23

27 21

42 35 23 27 21

An array of data
Implementing a Heap

42
 Data from the next row
goes in the next two array
locations. 35 23

27 21

42 35 23 27 21

An array of data
We don't care what's in
this part of the array.
Important Points about the
Implementation
42
 The links between the tree's
nodes are not actually
stored as pointers, or in any 35 23
other way.
 The only way we "know" 27 21
that "the array is a tree" is
from the way we
manipulate the data.
42 35 23 27 21

An array of data
Important Points about the
Implementation
42
 If you know the index of a
node, then it is easy to
figure out the indexes of 35 23
that node's parent and
children. Formulas are given 27 21
in the book.

42 35 23 27 21

[1] [2] [3] [4] [5]


Summary

• A heap is a complete binary tree, where


the entry at each node is greater than or
equal to the entries in its children.
• To add an entry to a heap, place the new
entry at the next available spot, and
perform a reheapification upward.
• To remove the biggest entry, move the last
node onto the root, and perform a
reheapification downward.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy