binary heaps text read weiss, §21.1 - 21.4 binary heap one-array representation of a tree complete...

20
Binary Heaps Text Read Weiss, §21.1 - 21.4 Binary Heap One-array representation of a tree Complete trees Building a Binary Heap Insert Delete

Upload: sherman-pitts

Post on 21-Jan-2016

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Binary Heaps Text Read Weiss, §21.1 - 21.4 Binary Heap One-array representation of a tree Complete trees Building a Binary Heap Insert Delete

Binary HeapsText• Read Weiss, §21.1 - 21.4

Binary Heap • One-array representation of a tree• Complete trees

Building a Binary Heap• Insert• Delete

Page 2: Binary Heaps Text Read Weiss, §21.1 - 21.4 Binary Heap One-array representation of a tree Complete trees Building a Binary Heap Insert Delete

Motivation

• Development of a data structure which allows efficient inserts and efficient deletes of the minimum value (minheap) or maximum value (maxheap)

Page 3: Binary Heaps Text Read Weiss, §21.1 - 21.4 Binary Heap One-array representation of a tree Complete trees Building a Binary Heap Insert Delete

Implementation

• One-array implementation of a binary tree

• Root of tree is at element 1 of the array

• If a node is at element i of the array, then its children are at elements 2*i and 2*i+1

• If a node is at element i of the array, then its parent is at element floor(i/2)=└ i/2┘

Page 4: Binary Heaps Text Read Weiss, §21.1 - 21.4 Binary Heap One-array representation of a tree Complete trees Building a Binary Heap Insert Delete

Implementation

29 45 35 31 21

4

125

26 25 14 15

i 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

array __ 4 5 12 26 25 14 15 29 45 35 31 21 __ __ __

currentsize = 12

Page 5: Binary Heaps Text Read Weiss, §21.1 - 21.4 Binary Heap One-array representation of a tree Complete trees Building a Binary Heap Insert Delete

Implementatation

• Heap must be a complete tree

• all leaves are on the lowest two levels

• nodes are added on the lowest level, from left to right

• nodes are removed from the lowest level, from right to left

Page 6: Binary Heaps Text Read Weiss, §21.1 - 21.4 Binary Heap One-array representation of a tree Complete trees Building a Binary Heap Insert Delete

Inserting a Value

29 45 35 31 21

4

125

26 25 14 15

i 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

array __ 4 5 12 26 25 14 15 29 45 35 31 21 3 __ __

currentsize = 13Insert 3

3 insert here to keep tree complete

Page 7: Binary Heaps Text Read Weiss, §21.1 - 21.4 Binary Heap One-array representation of a tree Complete trees Building a Binary Heap Insert Delete

Inserting a Value

29 45 35 31 21

4

125

26 25 14 15

Insert 3

save new value in a temporary location: tmp 3

Page 8: Binary Heaps Text Read Weiss, §21.1 - 21.4 Binary Heap One-array representation of a tree Complete trees Building a Binary Heap Insert Delete

Inserting a Value

29 45 35 31 21

4

125

26 25 14 15

Insert 3

14

tmp 3

copy 14 down because 14 > 3

Page 9: Binary Heaps Text Read Weiss, §21.1 - 21.4 Binary Heap One-array representation of a tree Complete trees Building a Binary Heap Insert Delete

Inserting a Value

29 45 35 31 21

4

125

26 25 12 15

Insert 3

14

tmp 3

copy 12 down because 12 > 3

Page 10: Binary Heaps Text Read Weiss, §21.1 - 21.4 Binary Heap One-array representation of a tree Complete trees Building a Binary Heap Insert Delete

Inserting a Value

29 45 35 31 21

4

45

26 25 12 15

Insert 3

14

tmp 3

copy 4 down because 4 > 3

Page 11: Binary Heaps Text Read Weiss, §21.1 - 21.4 Binary Heap One-array representation of a tree Complete trees Building a Binary Heap Insert Delete

Inserting a Value

29 45 35 31 21

3

45

26 25 12 15

Insert 3

14

insert 3

Page 12: Binary Heaps Text Read Weiss, §21.1 - 21.4 Binary Heap One-array representation of a tree Complete trees Building a Binary Heap Insert Delete

Heap After Insert

29 45 35 31 21

3

45

26 25 12 15

14

Page 13: Binary Heaps Text Read Weiss, §21.1 - 21.4 Binary Heap One-array representation of a tree Complete trees Building a Binary Heap Insert Delete

Deleting a Value (note new tree!)

29 45 35 31 21

3

75

26 25 12 15

14

Delete 3

Page 14: Binary Heaps Text Read Weiss, §21.1 - 21.4 Binary Heap One-array representation of a tree Complete trees Building a Binary Heap Insert Delete

Deleting a Value

29 45 35 31 21

75

26 25 12 15

14

Delete 3

3save root value … tmp

Page 15: Binary Heaps Text Read Weiss, §21.1 - 21.4 Binary Heap One-array representation of a tree Complete trees Building a Binary Heap Insert Delete

Deleting a Value

29 45 35 31 21

75

26 25 12 15

14

Delete 3

3tmp

X

copy value of last node in complete tree into temporary location; decrement currentsize

14

Page 16: Binary Heaps Text Read Weiss, §21.1 - 21.4 Binary Heap One-array representation of a tree Complete trees Building a Binary Heap Insert Delete

Deleting a Value

29 45 35 31 21

75

26 25 12 15

Delete 3

3tmp

push down root …

compare children select smaller

14

Page 17: Binary Heaps Text Read Weiss, §21.1 - 21.4 Binary Heap One-array representation of a tree Complete trees Building a Binary Heap Insert Delete

Deleting a Value

29 45 35 31 21

5

7

26 25 12 15

Delete 3

3tmp

push down root …

copy smaller value into parent

14

Page 18: Binary Heaps Text Read Weiss, §21.1 - 21.4 Binary Heap One-array representation of a tree Complete trees Building a Binary Heap Insert Delete

Deleting a Value

29 45 35 31 21

5

7

26 25 12 15

Delete 3

3tmp

push down root … 14

compare children select smaller (25)

Page 19: Binary Heaps Text Read Weiss, §21.1 - 21.4 Binary Heap One-array representation of a tree Complete trees Building a Binary Heap Insert Delete

Deleting a Value

29 45 35 31 21

5

714

26 25 12 15

Delete 3

3tmp

push down root …

copy 14 into parent because 14 < smaller child

Page 20: Binary Heaps Text Read Weiss, §21.1 - 21.4 Binary Heap One-array representation of a tree Complete trees Building a Binary Heap Insert Delete

Deleting a Value

29 45 35 31 21

5

714

26 25 12 15

Delete 3

3return