• No results found

Week09_Heap P1.ppt

N/A
N/A
Protected

Academic year: 2020

Share "Week09_Heap P1.ppt"

Copied!
28
0
0

Loading.... (view fulltext now)

Full text

(1)
(2)

Heaps

A heap is a certain kind of complete binary tree.

A heap is a binary tree with the following two properties

1. The value of each node is not less than the values stored in each of its children.

(3)

Heaps

A heap is a certain kind of complete binary tree.

The "heap property" requires that each node's key is >= the

keys of its children

19 4 22 21 27 23 45 35 Each node in a heap

contains a key that can be compared to

other nodes' keys.

(4)

Insert into a heap (STEPs)

First adjoin ITEM at the end of tree so that

tree is still a complete tree , but not

necessarily a heap.

Then let ITEM rise to its “appropriate

(5)

Adding a Node to a Heap

 Put the new node in

the next available spot.

 Push the new node

upward, swapping with its parent until the new node

(6)

Adding a Node to a Heap

 Put the new node in

the next available spot.

 Push the new node

upward, swapping with its parent until the new node

(7)

Adding a Node to a Heap

 Put the new node in

the next available spot.

 Push the new node

upward, swapping with its parent until the new node

(8)

Adding a Node to a Heap

 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. 19

4 22

21 35

23 45

42

(9)

Deleting The Root Of A Heap

(STEPs)

 Assign the root R to some variable ITEM

 Replace the deleted node R by the last node

“L” of Heap tree so that tree is still a complete tree, but not necessarily a heap

 (Reheap) Let L sink to its appropriate place in

(10)

Removing the Top of a Heap

 Move the last node

onto the root.

19

4 22

21 35

23 45

42

(11)

Removing the Top of a Heap

 Move the last node

onto the root.

19

4 22

21 35

23

27

(12)

Removing the Top of a Heap

 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.

19

4 22

21 35

23

27

(13)

Removing the Top of a Heap

 Move the last node

onto the root.

 Push the

out-of-place node downward,

(14)

Removing the Top of a Heap

 Move the last node

onto the root.

 Push the

out-of-place node downward,

(15)

Removing the Top of a Heap

 The children all have

keys <= the out-of-place node, or

 The node reaches

the leaf.

 The process of

pushing the new

(16)

Implementing a Heap

We will store the

data from the

nodes in a

partially-filled

array.

An array of data

21 27

23 42

(17)

Implementing a Heap

Data from the root goes

in the first location of the array.

An array of data

21 27

23 42

35

(18)

Implementing a Heap

 Data from the next row

goes in the next two

array locations.

An array of data

21 27

23 42

35

(19)

Implementing a Heap

 Data from the next row goes in the next two array locations.

An array of data

21 27

23 42

35

(20)

Implementing a Heap

 Data from the next row

goes in the next two

array locations.

An array of data

21 27

23 42

35

42 35 23 27 21

We don't care what's in

We don't care what's in

this part of the array.

(21)

Important Points about the

Implementation

 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.

An array of data

21 27

23 42

35

(22)

Important Points about the

Implementation

 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.

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

21 27

23 42

35

(23)

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

(24)
(25)

Heap Sort STEPs

 Heap sort thus starts from the heap, puts the

largest element at the end of the array and restores the heap that now has one less element

From the new heap, the largest element is removed

and put in its final position, and then the heap property is restored for the remaining elements

 Thus, in each round, one element of the array ends

(26)

Heap Sort Algorithm/Pseudocode

heapsort(data[], n)

Transform data into a heap

swap the root with the element in last position

restore the heap property for the tree data[0], . . . , data[n-1]

(27)
(28)

References

Related documents

Target market: Your children Under the influence: Parents feel pressure exerted by boys and girls influenced by advertisements.. The Atlanta Journal the Atlanta Constitution

First using daily prices the study, investigates whether significant abnormal returns can be generated in the Colombo Stock Market in the period surrounding

The clinical management of a patient will be assessed by one of our dento-legal advisers and/or an independent dental expert as appropriate (see section 7.6 Working with

por una equivalente que después se combina con otro equipo por una equivalente que después se combina con otro equipo aplicando la combinación Serie para obtener el. aplicando

A few months prior to the arrival of the interviews, The Getty Research Institute (GRI) had appointed a Born- Digital Materials Steering Committee “to define,

Results show that by increasing the inlet ow frequency, Strouhal number increases slightly and the oscillation amplitude of drag coecient increases considerably.. However, the

To grasp the theoretical perspective of the issue, this undertaking employs securitisation theory as a method to demonstrate whether the changing perception of Islam as a matter