HEAPS - PowerPoint PPT Presentation

About This Presentation
Title:

HEAPS

Description:

Insertion of a new element into the heap. Deletion of the largest element from the heap ... Construct (initialize) a max heap object using data elements of an array A' ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 9
Provided by: Bar38
Category:

less

Transcript and Presenter's Notes

Title: HEAPS


1
HEAPS
Definition A max tree is a tree in which the key
value in each node is no smaller than the key
values in its children (if any). A max heap is
a complete binary tree that is also a max
tree Definition A min tree is a tree in which
the key value in each node is no larger than the
key values in its children (if any). A min heap
is a complete binary tree that is also a min tree.
2
Heaps as a Abstract Data Type
  • When viewed as an ADT, a max heap is very simple.
    In particular the only basic operations are
  • Creation of an empty heap
  • Insertion of a new element into the heap
  • Deletion of the largest element from the heap

3
Samples of Max Heaps
1
1
2
2
3
4
4
Samples of Min Heaps
1
1
2
2
3
4
1
2
3
4
5
6
5
Example Max Heap Object Methods
  • Construct (initialize) a max heap object using
    data elements of an array A
  • Build a max heap object from an array A of n
    data elements
  • Destroy (delete) a heap object and an array A
  • Compare data elements
  • Swap data elements
  • Rebuild a max heap
  • Add/insert an element into a max heap object
  • Delete an element from a max heap object
  • Do a heap sort for an array A using a max heap
    object
  • Display (print) a heap object at each iteration
    of heap sort
  • Print a max heap object

6
Creation of a Heap
  • define MAX_ELEMENTS 200 / maximum heap
    size 1 /
  • define HEAP_FULL(n) (n MAX_ELEMENTS 1)
  • define HEAP_EMPTY(n) (!n)
  • typedef struct (
  • int key
  • / other fields /
  • ) element
  • element heap(MAX_ELEMENTS)
  • int n 0

7
Insertion into a max heap
2
4
(a) Heap before insertion
(b) Initial location of new node
5
20
4
4
(c) Insert 5 into heap (a)
(d) Insert 21 into heap (a)
8
  • Void insert_max_heap(element item, int n) /
    insert item into a max heap of current size n
    / int i if (HEAP_FULL(n)) fprintf(stderr,
    The heap is full. \n) exit(1) i
    (n) while((i ! 1) (item.key gt
    heapi/2.key)) heapi heapi/2
  • i / 2 heapi item
Write a Comment
User Comments (0)
About PowerShow.com