110 likes | 131 Views
Introduction to Data Structure. Chapter 14 Ming Li Department of Computer Science California State University, Fresno Fall 2006. Binary Heap.
E N D
Introduction to Data Structure Chapter 14 Ming Li Department of Computer Science California State University, Fresno Fall 2006
Binary Heap Binary heaps are a particularly simple kind of heap data structure created using a binary tree. It can be seen as a binary tree with two additional constraints: The shape property: the tree is either a perfectly balanced binary tree (all leaves are at the same level), or, if the last level of the tree is not complete, the nodes are filled from left to right. The heap property: each node is greater than or equal to each of its children according to some comparison predicate which is fixed for the entire data structure.
Maximum and Minimum Heaps Example 55 40 52 30 50 15 11 10 10 25 5 20 22 (A) Maximum Heap (9 nodes) (B) Maximum Heap (4 nodes)
Maximum and Minimum Heaps Example 10 5 30 15 50 10 40 52 11 20 55 25 22 ( C) Minimum Heap (9 nodes) (D) Minimum Heap (4 nodes)
Heap Sort • Sort a list of objects by using a heap data structure. • All elements to be sorted are inserted into a heap • The heap organizes the elements added to it in such a way that either the largest value or the smallest value can be quickly extracted. • This gives us the elements in order. • More info: http://en.wikipedia.org/wiki/Heapsort