What is a Heap?
💻 coding

What is a Heap?

1 min read 241 words
1 min read
ShareWhatsAppPost on X
  • 1A heap is a tree structure that maintains a specific order between parent and child nodes, known as the heap property.
  • 2Heaps can be classified into two types: min heaps, where parent nodes are less than or equal to children, and max heaps, where they are greater.
  • 3Binary heaps, which are complete binary trees, are commonly represented using arrays to optimize space and access efficiency.

AI-generated summary · May not capture all nuances

Key Insight
AskGif

"A heap is a tree structure that maintains a specific order between parent and child nodes, known as the heap property."

What is a Heap?

A heap is a tree with some special properties. The basic requirement of a heap is that the value of

a node must be > (or <) than the values of its children. This is called heap property. A heap also

has the additional property that all leaves should be at h or h – 1 levels (where h is the height of

the tree) for some h > 0 (complete binary trees). That means heap should form a complete binary

tree (as shown below).

Types of Heaps?

Based on the property of a heap we can classify heaps into two types:

• Min heap: The value of a node must be less than or equal to the values of its

children

• Max heap: The value of a node must be greater than or equal to the values of its

children

Binary Heaps

In binary heap each node may have up to two children. In practice, binary heaps are enough and

we concentrate on binary min heaps and binary max heaps for the remaining discussion.

Representing Heaps: Before looking at heap operations, let us see how heaps can be

represented. One possibility is using arrays. Since heaps are forming complete binary trees, there

will not be any wastage of locations. For the discussion below let us assume that elements are

stored in arrays, which starts at index 0.

source: Data Structures and Algorithms Made Easy in Java - Narasimha Karumanchi

Enjoyed this article?

Share it with someone who'd find it useful.

ShareWhatsAppPost on X

AskGif

Published on 3 January 2019 · 1 min read · 241 words

Part of AskGif Blog · coding

You might also like