Skip to content

Commit cc180a1

Browse files
authored
Merge pull request #1219 from dhairyagothi/main
Heaps documentation added
2 parents b50475f + a9f7fad commit cc180a1

File tree

3 files changed

+146
-0
lines changed

3 files changed

+146
-0
lines changed

docs/dsa/Heaps/_category_.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"label": "Heaps",
3+
"position": 10,
4+
"link": {
5+
"type": "generated-index",
6+
"description": "Heaps are a type of binary tree-based data structure that satisfy the heap property. They are commonly used to implement priority queues and efficiently find the maximum or minimum element. Heaps can be either max heaps or min heaps, depending on whether the parent nodes are greater or smaller than their children. Operations like insertion, deletion, and retrieval of the maximum or minimum element can be performed efficiently on heaps."
7+
}
8+
9+
}
10+
11+
12+

docs/dsa/Heaps/heaps.md

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
---
2+
id: heaps-in-dsa
3+
title: Heaps Data Structure
4+
sidebar_label: Heaps
5+
sidebar_position: 1
6+
description: "Heaps are a type of binary tree-based data structure commonly used in computer science. They are often used to implement priority queues, where elements with higher priority are dequeued first. Heaps have two main variations: max heaps, where the parent node is always greater than or equal to its children, and min heaps, where the parent node is always less than or equal to its children. Heaps have efficient insertion and deletion operations, making them suitable for applications that require efficient priority-based processing."
7+
tags: [dsa, data-structures, heaps]
8+
---
9+
10+
11+
12+
## Python - Heaps
13+
14+
Heap is a special tree structure in which each parent node is less than or equal to its child node. Then it is called a Min Heap. If each parent node is greater than or equal to its child node then it is called a max heap. It is very useful is implementing priority queues where the queue item with higher weightage is given more priority in processing.
15+
16+
A detailed discussion on heaps is available in our website here. Please study it first if you are new to heap data structure. In this chapter we will see the implementation of heap data structure using python.
17+
18+
![alt text](image.png)
19+
## Create a Heap
20+
21+
A heap is created by using python’s inbuilt library named heapq. This library has the relevant functions to carry out various operations on heap data structure. Below is a list of these functions.
22+
23+
- heapify − This function converts a regular list to a heap. In the resulting heap the smallest element gets pushed to the index position 0. But rest of the data elements are not necessarily sorted.
24+
25+
- heappush − This function adds an element to the heap without altering the current heap.
26+
27+
- heappop − This function returns the smallest data element from the heap.
28+
29+
- heapreplace − This function replaces the smallest data element with a new value supplied in the function.
30+
31+
## Creating a Heap
32+
A heap is created by simply using a list of elements with the heapify function. In the below example we supply a list of elements and the heapify function rearranges the elements bringing the smallest element to the first position.
33+
34+
35+
**Python - Heaps**
36+
37+
Heap is a special tree structure in which each parent node is less than or equal to its child node. Then it is called a Min Heap.
38+
39+
If each parent node is greater than or equal to its child node then it is called a max heap.
40+
41+
It is very useful is implementing priority queues where the queue item with higher weightage is given more priority in processing.
42+
43+
A detailed discussion on heaps is available in our website here. Please study it first if you are new to heap data structure. In this chapter we will see the implementation of heap data structure using python.
44+
45+
**Example**
46+
``` python
47+
import heapq
48+
49+
H = [21,1,45,78,3,5]
50+
# Use heapify to rearrange the elements
51+
heapq.heapify(H)
52+
print(H)
53+
```
54+
55+
## Output
56+
When the above code is executed, it produces the following result −
57+
``` python
58+
[1, 3, 5, 78, 21, 45]
59+
```
60+
## Inserting into heap
61+
62+
Inserting a data element to a heap always adds the element at the last index. But you can apply heapify function again to bring the newly added element to the first index only if it smallest in value. In the below example we insert the number 8.
63+
64+
**Example**
65+
66+
``` python
67+
import heapq
68+
69+
H = [21,1,45,78,3,5]
70+
# Covert to a heap
71+
heapq.heapify(H)
72+
print(H)
73+
74+
# Add element
75+
heapq.heappush(H,8)
76+
print(H)
77+
```
78+
79+
## Output
80+
81+
When the above code is executed, it produces the following result −
82+
```python
83+
[1, 3, 5, 78, 21, 45]
84+
[1, 3, 5, 78, 21, 45, 8]
85+
```
86+
## Removing from heap
87+
88+
You can remove the element at first index by using this function. In the below example the function will always remove the element at the index position 1.
89+
90+
**Example**
91+
```python
92+
import heapq
93+
94+
H = [21,1,45,78,3,5]
95+
# Create the heap
96+
97+
heapq.heapify(H)
98+
print(H)
99+
100+
# Remove element from the heap
101+
heapq.heappop(H)
102+
103+
print(H)
104+
Output
105+
When the above code is executed, it produces the following result −
106+
```python
107+
[1, 3, 5, 78, 21, 45]
108+
[3, 21, 5, 78, 45]
109+
```
110+
## Replacing in a Heap
111+
112+
The heap replace function always removes the smallest element of the heap and inserts the new incoming element at some place not fixed by any order.
113+
114+
**Example**
115+
``` python
116+
import heapq
117+
118+
H = [21,1,45,78,3,5]
119+
# Create the heap
120+
121+
heapq.heapify(H)
122+
print(H)
123+
124+
# Replace an element
125+
heapq.heapreplace(H,6)
126+
print(H)
127+
```
128+
129+
## Output
130+
When the above code is executed, it produces the following result −
131+
```python
132+
[1, 3, 5, 78, 21, 45]
133+
[3, 6, 5, 78, 21, 45]
134+
```

docs/dsa/Heaps/image.png

178 KB
Loading

0 commit comments

Comments
 (0)