If you're familiar with heaps, you've probably used them in priority queues, heapsort, or maybe even to tackle those tricky DSA problems. But have you ever heard of a Beap?

Beap stands for bi-parental heap β€” a clever structure introduced by Ian Munro and Hendra Suwanda in 1984. It's designed to make both insertion and search operations efficient, giving us O(√n) time complexity for both.

In this story, we'll explore:

πŸ†š Heap vs Beap: What’s the Difference?

Heaps are the go-to for priority queues and sorting, but they follow a strict binary tree model: each node has at most one parent and two children, and the heap property ensures the parent is smaller (in a min-heap) or larger (in a max-heap) than its children.

Beaps flip that idea by arranging elements in a triangular matrix, where nodes β€” except on the boundaries β€” can have two parents and two children. This structure enables an elegant, grid-like representation that makes search operations far more efficient.

Feature

Binary Heap

Beap

Structure

Binary Tree

Triangular Matrix

Parents per Node

1

Up to 2

Children per Node

Up to 2

Up to 2

Search Complexity

O(n)

O(√n)

Insert Complexity

O(log n)

O(√n)

Use Cases

Heapsort, PQs

Fast search + insert

This triangular matrix layout allows search to start from the bottom-left and proceed smartly based on comparisons.

πŸ“Œ Here's a diagram to help you visualize a beap:

πŸ“– Learn more on Wikipedia


πŸ”§ Core Operations (With GIFs)

Let’s walk through how each operation works β€” and yes, we’ve animated them so you don’t have to imagine the pointer gymnastics.

πŸ”Ό Insert

We insert an element into the next available triangle position, then bubble up by comparing it with its two parents (if they exist).


πŸ”½ Extract Min

In a min-beap, the smallest element is always at the top. Removing it means replacing it with the last element and then bubbling down.


Searching is where beaps shine. Start at the bottom-left, and depending on comparisons, move either up or right. Time complexity: O(√n).


⬇️ Min & ⬆️ Max


πŸŽ“ Wrapping Up

Beaps may not be part of the standard algorithm toolkit, but they offer a compelling blend of structure and performance. Their unique design and search mechanics make them worth exploring, whether you're deep into data structure theory or just curious about alternatives to the usual heap.

If you're interested in checking out the full code and visual demos, you can find them on GitHub.