|
1
|
- Chapter 10 has several programming projects, including a project that
uses heaps.
- This presentation shows you what a heap is, and demonstrates two of the
important heap algorithms.
|
|
2
|
- A heap is a certain kind of complete binary tree.
|
|
3
|
- A heap is a certain kind of complete binary tree.
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
|
|
11
|
- A heap is a certain kind of complete binary tree.
|
|
12
|
- A heap is a certain kind of complete binary tree.
|
|
13
|
- Put the new node in the next available spot.
- Push the new node upward, swapping with its parent until the new node
reaches an acceptable location.
|
|
14
|
- Put the new node in the next available spot.
- Push the new node upward, swapping with its parent until the new node
reaches an acceptable location.
|
|
15
|
- Put the new node in the next available spot.
- Push the new node upward, swapping with its parent until the new node
reaches an acceptable location.
|
|
16
|
- The parent has a key that is
>= new node, or
- The node reaches the root.
- The process of pushing the new node upward is called reheapification upward.
|
|
17
|
- Move the last node onto the root.
|
|
18
|
- Move the last node onto the root.
|
|
19
|
- Move the last node onto the root.
- Push the out-of-place node downward, swapping with its larger child
until the new node reaches an acceptable location.
|
|
20
|
- Move the last node onto the root.
- Push the out-of-place node downward, swapping with its larger child
until the new node reaches an acceptable location.
|
|
21
|
- Move the last node onto the root.
- Push the out-of-place node downward, swapping with its larger child
until the new node reaches an acceptable location.
|
|
22
|
- The children all have keys <= the out-of-place node, or
- The node reaches the leaf.
- The process of pushing the new node
downward is called reheapification downward.
|
|
23
|
- We will store the data from the nodes in a partially-filled array.
|
|
24
|
- Data from the root goes in the
first location of the array.
|
|
25
|
- Data from the next row goes in the next two array locations.
|
|
26
|
- Data from the next row goes in the next two array locations.
|
|
27
|
- Data from the next row goes in the next two array locations.
|
|
28
|
- The links between the tree's nodes are not actually stored as pointers,
or in any other way.
- The only way we "know" that "the array is a tree" is
from the way we manipulate the data.
|
|
29
|
- If you know the index of a node, then it is easy to figure out the
indexes of that node's parent and children. Formulas are given in the
book.
|
|
30
|
- 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.
|
|
31
|
|