Publicité  Prochain SlideShare Binary Heap Tree, Data Structure
Chargement dans ... 3
1 sur 2
Publicité

### Algorithm for checking whether an array is a min heap- A binary heap i.docx

1. Algorithm for checking whether an array is a min heap. A binary heap is a heap data structure created using a binary tree. It can be seen as a binary tree with two additional constraints: A bimaseap data structure created using a binary tanb seen as a binary tree with two I. Shape property A binary heap is a complete binary tree, that is, all levels of the tree, except possibly the last one (deepest) are fully filled, and, if the last level of the tree is not complete, the nodes of that level are filled from left to right. II. Heap property All nodes are either greater than or equal to or less than or equal to each of their children, according to a comparison predicate defined for the heap. Heaps with a mathematical "greater than or equal to" () comparison predicate are called max-heaps those with a mathematical "less than or equal to" comparison predicate are called min-heaps. Min-heaps are often used to implement priority queues. The figure below shows a complete binary tree along with its list representation. Solution ALGORITHM: For i = 1, 2, â€¦, [n/2], check whether H[i] max {H [2i], H [2i+1]} (Of course, if 2i+1 > n, just H[i] H [2i] needs to be satisfied) If the inequality does not hold for some i, stop â€“ the array is not a heap. If it holds for every I = 1, 2, â€¦., [n/2], it is a heap. PSEUDO CODE: For i = 1 to length (H) do: begin if 2 * i + 1 <= length (H) IF (H[i] smaller than H[2i] ) OR (H[i] smaller than H[2i+1]):
2. return false else if 2 * i <= lenght(H): if H[i] smaller than H[2i]: return false else break; end return true
Publicité