Pertemuan 11.1 - Heap Tree
Pertemuan 11.1 - Heap Tree
Heaps
A heap is a
certain kind
of complete
binary tree.
Heaps
Roo
t
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
Left
Complete child
binary tree. of the
root
Right
Complete child
binary tree. of the
root
Complete
binary tree.
Complete
binary tree.
Complete
binary tree.
Complete
binary tree.
Complete
binary tree.
Heaps
45
A heap is a
certain kind 35 23
of complete
binary tree. 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 35 23
of complete
binary tree. 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 with 27 21 22 4
its parent until the new
node reaches an
19 42
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 with 42 21 22 4
its parent until the new
node reaches an
19 27
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 with 35 21 22 4
its parent until the new
node reaches an
19 27
acceptable location.
Adding a Node to a Heap
45
❑ The parent has a key
that is >= new node,
or 42 23
❑ The node reaches the
root. 35 21 22 4
❑ The process of
pushing the new node 19 27
upward is called
reheapification
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 19
downward is called
reheapification
downward.
Implementing a Heap
42
❑ We will store the
data from the 35 23
nodes in a
partially-filled
27 21
array.
An array of
data
Implementing a Heap
42
● Data from the root
goes in the 35 23
first
location
27 21
of the
array.
4
2
An array of
data
Implementing a Heap
42
● Data from the
next row goes in 35 23
the next two array
locations.
27 21
4 3 2
2 5 3
An array of
data
Implementing a Heap
42
● Data from the
next row goes in 35 23
the next two array
locations.
27 21
4 3 2 2 2
2 5 3 7 1
An array of
data
Implementing a Heap
42
● Data from the
next row goes in 35 23
the next two array
locations.
27 21
4 3 2 2 2
2 5 3 7 1
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 other 35 23
way.
● The only way we "know" that 27 21
"the array is a tree" is from
the way we manipulate the
data.
4 3 2 2 2
2 5 3 7 1
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 that 35 23
node's parent and children.
Formulas are given in the 27 21
book.
4 3 2 2 2
2 5 3 7 1
[1] [2] [3] [4] [5]
Summary