Chapter 21 The Binary Heap - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

Chapter 21 The Binary Heap

Description:

Chapter 21 The Binary Heap Bernard Chen Spring 2006 What s priority queue Problem: find MIN and Delete Possible methods: unsorted array sorted array Binary ... – PowerPoint PPT presentation

Number of Views:73
Avg rating:3.0/5.0
Slides: 33
Provided by: csGsuEdu
Learn more at: http://www.cs.gsu.edu
Category:
Tags: binary | chapter | heap

less

Transcript and Presenter's Notes

Title: Chapter 21 The Binary Heap


1
Chapter 21The Binary Heap
  • Bernard Chen
  • Spring 2006

2
Whats priority queue
3
Problem find MIN and Delete
  • Possible methods
  • unsorted array
  • sorted array
  • Binary Search tree

4
Whats Binary Heap
  • The Binary Heap supports the insertion of new
    items and delete of MIN item in logarithmic
    worst-case time.
  • It uses only an array to implement. (Instead of
    linked list)
  • It is the classic method used to implement
    priority queues

5
Structure Property
  • A COMPLETE BINARY TREE is a tree that complete
    filled.

6
Complete Binary Tree
7
Advantages of Complete Tree
  • The height of a complete binary tree is at most
    logN
  • Left and right pointers are not needed

8
Advantages of Complete Tree
  • The parent is in position i/2
  • The left child is in position 2i
  • The right child is in position 2i1

9
Heap-Order property
  • In a Heap, for every node X with parent P, the
    key in P is smaller than or equal to the key in X

10
21.2 Implementation of the Basic Operations
  • Insert operation
  • Delete operation

11
21.2.1 The INSERT operation
  • Insertion is implemented by creating a hole at
    the next available location and then percolating
    it up until the new item can be placed in it
    without violating a heap order.
  • Insert takes constant time on average but
    logarithmic time in worst case.

12
Insert step 1
13
Insert step 2
14
Insert step 3
15
Insert step 4
16
Insert step 5
17
21.2.2 The DeleteMIN Operation
  • Deletion of the min involves placing the former
    last item in a hole that is created at the root.
  • The hole is percolated down the tree through min
    children until the item can be placed without
    violating the heap order property.

18
DeleteMIN step 1
19
DeleteMIN step 2
20
DeleteMIN step 3
21
DeleteMIN step 4
22
DeleteMIN step 5
23
21.3 the buildHeap OperationLinear-Time Heap
construction
  • The buildHeap operation can be done in linear
    time by applying a percolate down routine to
    nodes in reverse order

24
BuildHeap step 1
25
BuildHeap step 2
26
BuildHeap step 3
27
BuildHeap step 4
28
BuildHeap step 5
29
BuildHeap step 6
30
BuildHeap step 7
31
BuildHeap step 8
32
BuildHeap step 8
Write a Comment
User Comments (0)
About PowerShow.com