Skip to content

Commit 3416ed2

Browse files
sayoojkkarunfabiobaltieri
authored andcommitted
doc: kernel: data_structures: Add documentation for min-heap
Includes explanation of the min-heap’s properties, constraints and explains about the uses cases of min-heap. It also contains references to Sample Application and min-heap API Signed-off-by: Sayooj K Karun <[email protected]>
1 parent fc6f9e2 commit 3416ed2

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

doc/kernel/data_structures/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,4 @@ structures are thread safe in specific usage scenarios (see
3838
ring_buffers.rst
3939
mpsc_lockfree.rst
4040
spsc_lockfree.rst
41+
min_heap.rst
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
.. _min_heap_api:
2+
3+
Min-Heap Data Structure
4+
#######################
5+
6+
.. contents::
7+
:local:
8+
:depth: 2
9+
10+
The Min-Heap implementation provides an efficient data structure for
11+
managing a dynamically changing list of elements while maintaining the ability
12+
to quickly extract the minimum value. It's a tree-based data structure that
13+
satisfies the heap property and supports common operations such as insertion,
14+
removal and popping the minimum element from the Min-Heap
15+
16+
This section explains the motivation behind the implementation, its internal
17+
structure, API usage, and example scenarios for embedded systems and real-time
18+
environments.
19+
20+
Heap Structure
21+
**************
22+
23+
The heap is maintained as a complete binary tree stored in an array.
24+
Each node satisfies the **min-heap** property:
25+
26+
- The value of each node is less than or equal to the values of its children.
27+
28+
This property ensures that the **minimum element is always at the root**
29+
(index 0).
30+
31+
.. code-block:: text
32+
33+
Index: 0 1 2 3 4 5 6
34+
Values: [1, 3, 5, 8, 9, 10, 12]
35+
36+
1
37+
/ \
38+
3 5
39+
/ \ / \
40+
8 9 10 12
41+
42+
For any node at index ``i``, its children are at indices:
43+
44+
- Left child: :math:`2*i + 1`
45+
46+
- Right child: :math:`2*i + 2`
47+
48+
Its parent is at index:
49+
50+
- Parent: :math:`(i - 1) / 2`
51+
52+
Use Cases
53+
*********
54+
55+
MinHeap is well suited for:
56+
57+
- Implementing priority queues
58+
- Sorting data incrementally
59+
- Resource arbitration (e.g., lowest-cost resource selection)
60+
- Scheduling in cooperative multitasking systems
61+
- Managing timeouts and delay queues
62+
- Priority-based sensor or data sampling
63+
64+
In RTOS environments like Zephyr, this heap can be used in kernel-level or
65+
application-level modules where minimal latency to obtain the highest priority
66+
resource is needed.
67+
68+
Samples
69+
*******
70+
71+
:zephyr:code-sample:`min-heap` sample demos the API usage of Min-Heap
72+
implementation in Zephyr RTOS.
73+
74+
API Reference
75+
*************
76+
77+
.. doxygengroup:: min_heap_apis

0 commit comments

Comments
 (0)