Vous êtes sur la page 1sur 34

1

HEAP
2 Full Binary Tree versus Complete
Binary Tree
A full binary tree (sometimes proper binary tree or 2-tree) is a tree
in which every node other than the leaves has two children.
3 Full Binary Tree versus Complete
Binary Tree
A complete binary tree is a binary tree in which every level,
except possibly the last, is completely filled, and all nodes are
as far left as possible.
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 Left child


tree. of the
root

The second node is


always the left child
of the root.
Heaps

Complete binary Right child


tree.
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.
12
Heaps

Complete binary
tree.
13 Heap

It is a binary tree with the following properties:


Property 1: it is a complete binary tree
Property 2: the value stored at a node is greater or equal to the
values stored at their children
14
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.
15
Heaps

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

27 21 22 4
Here, the value stored at a
node/parent is greater or equal
to the values stored at their
children 19
The "heap property"
requires that each
node's key is >= the
keys of its children
16
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
19 42
reaches an
acceptable location.
17
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
19 27
reaches an
acceptable location.
18
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
19 27
reaches an
acceptable location.
19
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 upward
is called 19 27
reheapification
upward.
20
Removing the Top of a Heap

45
Move the last node
onto the root.
42 23
Although 19 and 27 are
last nodes, we should
move only 27 not 19. 35 21 22 4
Because according to
complete binary tree
definition, all nodes are 19 27
as far left as possible. If
we remove 19 that will
violate binary tree
condition. Refer slide
number 5 diagram to
understand the
structure of binary
tree.
21
Removing the Top of a Heap

27
Move the last node
onto the root.
42 23

35 21 22 4

19
22
Removing the Top of a Heap

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

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

42
Move the last node
onto the root.
35 23
Push the out-of-
place node
downward, 27 21 22 4
swapping with its
larger child until the
new node reaches 19
an acceptable
location.
25
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.
26
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
27
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
28
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
29
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
30
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
31
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
"the array is a tree" is from the 27 21
way we manipulate the data.

42 35 23 27 21

An array of data
Important Points about the
32
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. 27 21

42 35 23 27 21

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


33 Difference between Min Heap and
Max Heap Data Structure

In min heap, for every pair of In max heap, for every pair of
parent and descendant child parent and descendant child
node, parent node have node, parent node have
always lower value than always greater value than
descended child node. descended child node.

34 Task

Develop Heap Implementation and deletion codes in


C++
Build a Heap H from the following list of Numbers:
44, 30, 50, 22, 60, 55, 77, 55 [ Ans: Refer Data
structures by Seymour Lipschutz e-book : page
number 7.60 ]
Delete a root node from the heap given in Data
structures by Seymour Lipschutz e-book : page
number 7.62

Vous aimerez peut-être aussi