Title: Trees IV:
1Trees IV
2Heap
- Like a binary search tree, a heap is a binary
tree in which the data entries can be compared
using total order semantics - Defining qualities of a heap
- the data entry contained in any node is greater
than or equal to the data entry contained in
either of its children - the tree is always complete
3Heap Example
45
32
41
19
7
28
40
10
12
4Heap Applications
- Heap can be used to implement a sorting algorithm
called heapsort - Heap is also a handy way to implement a priority
queue -- well use this application to illustrate
how heaps work
5Heap Implementation
- Because a heap is by definition a complete tree,
it is easy to use an array-based implementation - Heap operations reHeapUp and reHeapDown are used
to maintain the other defining quality of a heap
-- each nodes data entry gt data entries of its
children
6ReHeapUp operation
45
When a new node is added to the tree, it is
always added at the leftmost open position in the
bottom row
32
41
19
7
28
40
This maintains completeness of tree, but can
violate the other defining characteristic of a
heap
10
12
35
7ReHeapUp operation
Parent/child data swapping continues until the
heap condition is restored
ReHeapUp restores the second heap condition a
parent nodes data is always greater than or
equal to the data in either of its child nodes
45
32
41
35
19
7
28
40
35
32
The operation is accomplished by swapping the new
nodes data entry with that of its parent
10
12
35
7
8ReHeapDown operation
In a priority queue, the highest-priority item is
always dequeued first -- this item would be the
top item of the heap
Since this is going to be an array
implementation, well perform the usual trick
of swapping the last array entry with the
first, so we can
minimize the amount
of copying to be done
45
7
32
41
35
19
7
28
40
35
32
10
12
35
7
7
45
9ReHeapDown operation
Now we can effectively remove the item by simply
diminishing the used portion of our array by 1
(already done here)
We are once again faced with the same problem
-- the heap is the right shape, but the data
values are in the wrong positions
45
7
41
32
41
35
7
40
19
7
28
40
35
32
7
We solve this problem, as before, by swapping
data between nodes. In this case, we swap the
parent nodes data with the largest data entry of
the two children, continuing until the heap is
restored.
10
12
10Example Priority Queue with heap implementation
- Priority queue has all operations associated with
a queue enqueue, dequeue and helper functions
(e.g. size( ), is_empty( ), etc.) - Heap is a good fit for priority queue because
highest-priority item should always be dequeued
first, and this item would always be found at the
top of a heap
11Code for Priority Queue
- template ltclass thinggt
- class PQheap
-
- public
- PQheap( )
- enum SIZE30
- void enqueue (const thing entry)
- thing dequeue( )
- size_t size( )const return numItems
- ...
12Code for Priority Queue
- private
- thing dataSIZE
- size_t numItems
- void reHeapUp(size_t n)
- void reHeapDown(size_t n)
- size_t parent(size_t n) const return
(n-1)/2 - size_t left_child(size_t n) const return
(2n)1 - size_t right_child(size_t n) const return
(2n)2 - void Swap (thing x, thing y)
13Code for Priority Queue
- template ltclass thinggt
- PQheapltthinggtPQheap()
-
- numItems0
-
14Code for Priority Queue
- template ltclass thinggt
- void PQheapltthinggtenqueue (const thing entry)
-
- assert (size( ) lt SIZE)
- datanumItems entry
- reHeapUp(numItems)
- numItems
15Code for Priority Queue
- templateltclass thinggt
- void PQheapltthinggtreHeapUp(size_t n)
-
- size_t x n
- while (xgt0 datax gt dataparent(x))
-
- Swap(datax, dataparent(x))
- xparent(x)
-
16Code for Priority Queue
- template ltclass thinggt
- void PQheapltthinggtSwap (thing x, thing y)
-
- thing temp x
- x y
- y temp
17Code for Priority Queue
- template ltclass thinggt
- thing PQheapltthinggtdequeue()
-
- thing valuedata0 // save return value
- numItems--
- data0datanumItems // swap top bottom
- reHeapDown(numItems) // restore heap
- return value
18Code for Priority Queue
- template ltclass thinggt
- void PQheapltthinggtreHeapDown(size_t n)
-
- size_t current 0, big_child, heap_ok 0
- while ((!heap_ok) (left_child(current) lt
n)) -
- if (right_child(current) gt n)
- big_child left_child(current)
- else if (dataleft_child(current) gt
dataright_child(current)) - big_child left_child(current)
- else
- big_child right_child(current)
- ...
-
19Code for Priority Queue
- if (datacurrent lt databig_child)
-
- Swap(datacurrent, databig_child)
- current big_child
-
- else
- heap_ok 1
- // end of while loop
- // end of function