Lably
Skip to content

Min-max heap

Computer data structure

  • 9 min read
  • 3 chapters

Related on Lably

Through Computer science, Binary tree and Data structure

The min-max heap property is: each node at an even level in the tree is less than all of its descendants, while each node at an odd level in the tree is greater than all of its descendants.

The structure can also be generalized to support other order-statistics operations efficiently, such as find-median, delete-median,find(k) (determine the kth smallest value in the structure) and the operation delete(k) (delete the kth smallest value in the structure), for any fixed value (or set of values) of k. These last two operations can be implemented in constant and logarithmic time, respectively. The notion of min-max ordering can be extended to other structures based on the max- or min-ordering, such as leftist trees, generating a new (and more powerful) class of data structures. A min-max heap can also be useful when implementing an external quicksort.

Description

  • A min-max heap is a complete binary tree containing alternating min (or even) and max (or odd) levels. Even levels are for example 0, 2, 4, etc, and odd levels are respectively 1, 3, 5, etc. We assume in the next points that the root element is at the first level, i.e., 0.
  • Each node in a min-max heap has a data member (usually called key) whose value is used to determine the order of the node in the min-max heap.
  • The root element is the smallest element in the min-max heap.
  • One of the two elements in the second level, which is a max (or odd) level, is the greatest element in the min-max heap
  • Let x be any node in a min-max heap.
    • If x is on a min (or even) level, then x.key is the minimum key among all keys in the subtree with root x.
    • If x is on a max (or odd) level, then x.key is the maximum key among all keys in the subtree with root x.
  • A node on a min (max) level is called a min (max) node.

A max-min heap is defined analogously; in such a heap, the maximum value is stored at the root, and the smallest value is stored at one of the root's children.

Sign in to keep reading

2 more chapters are free with a Lably account.

More in Computing and communication