Download Heaps - WordPress.com

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Linked list wikipedia , lookup

Lattice model (finance) wikipedia , lookup

Quadtree wikipedia , lookup

Red–black tree wikipedia , lookup

B-tree wikipedia , lookup

Interval tree wikipedia , lookup

Binary tree wikipedia , lookup

Binary search tree wikipedia , lookup

Transcript
Uses of Heaps
There are two main uses of heaps.
The first is as a way of implementing a special kind of queue, called a priority queue.
Recall that in an ordinary queue, elements are added at one end of the queue and
removed from the other end, so that the elements are removed in the same order they
are added (FIFO). In a priority queue, each element has a priority; when an element is
removed it must be the element on the queue with the highest priority.
A very efficient way to implement a priority queue is with a heap ordered by priority each node is higher priority than everything below it. The highest priority element,
then, is at the top of the heap. We've just seen that the top element can be retrieved
with a single delete operation - O(logN) - and that inserting a new element is also
O(logN).
The second application is sorting. HeapSort uses the heap data structure to sort values
in exactly the same way as TreeSort used a binary search tree. To sort an array, or list,
containing N values there are two steps:
1. insert each value into a heap (initially empty)
2. remove each value form the heap in ascending order (this is done by N
successive calls to get_smallest).
What is the complexity of the HeapSort algorithm?
(N insert operations) + (N delete operations)
Each insert and delete operation is O(logN) at the very worst - the heap does not
always have all N values in it. So, the complexity is certainly no greater than
O(NlogN). This is better than the worst-case for TreeSort, which, because you might
build a degenerate binary search tree, is O(N*N).
Heap:
A heap is a certain kind of complete binary tree.
Binary Heap Properties:
1. Structure Property
2. Ordering Property
Heap Structure Property:
A binary heap is a complete binary tree.
A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled,
and all nodes are as far left as possible.
AA
BB
CC
DD
HH
EE
II
JJ
FF
KK
Figure 13.8 A
A complete
complete binary
binary tree
tree
Heap Ordering Property:
Min heap and Max Heap.
GG
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 reaches an acceptable
location.
 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.
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.
 The children all have keys <= the out-of-place node, or
 The node reaches the leaf.
 The process of pushing the new node downward is called reheapification downward.
MIN-MAX Heaps:


A double-ended priority queue is a data structure that supports the following operations:

Insert an element with arbitrary key

Delete an element with the largest key

Delete an element with the smallest key
Min heap or Max heap:


Min-Max heap:


Only insertion and one of the two deletion operations are supported
Supports all of the operations just described.
A mix-max heap is a complete binary tree such that if it is not empty, each element has
a field called key.


Alternating levels of this tree are min levels and max levels, respectively.

Let x be any node in a min-max heap. If x is on a min (max) level then the element in x
has the minimum (maximum) key from among all elements in the sub tree with root x.
We call this node a min (max) node.
Insertion into a min-max heap (at a “max” level)

If it is smaller/greater than its father (a “min”), then it must be smaller/greater than all
“max”/“min” above. So simply check the “min”/“max” ancestors

There exists a similar approach at a “min” level