DEV Community

Carlos Almonte
Carlos Almonte

Posted on

Max Heap

Turns out you can use math to turn a simple list of things into a binary tree. Here is how to find the parent of each item in the list, Math.floor((index — 1) / 2). Here is how to find the left child: 2 x index + 1. And here is how to find the right child: 2 x index + 2. Using this principle you can create something called a Max Heap. A data structure used to have the maximum value at the top of the tree. This allows for fast access to the maximum value, useful for:

Operating systems scheduling highest-priority processes — cancelling an ongoing process is more important that the process itself
Show top 10 trending hashtags on Twitter — there is a numerous amount of hashtags, keep the trending ones at the top
The following array: [100, 90, 80, 70, 60, 75, 65, 50, 55, 40] can be represented as the following tree.

Max Heap

Top comments (0)