Heaps
A heap data structure is a specialization of a tree in which the nodes are ordered in a specific way. A heap is a data structure where each data elements satisfies a heap
property, and the heap
property states that there must be a certain relationship between a parent node and its child nodes. According to this certain relationship in the tree, the heaps can be of two types, in other words, max
heaps and min
heaps. In a max
heap, each parent node value must always be greater than or equal to all its children. In this kind of tree, the root
node must be the greatest value in the tree. For example, see Figure 7.1 showing the max
heap in which all the nodes have greater values compared to their children:
Figure 7.1: An example of a max heap
In a min
heap, the relationship between parent and children is that the value of the parent node must always be less than or equal to its children. This rule should be followed by all the nodes in the tree. In the min
heap, the...