- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Insertion and Deletion in Heaps in Data Sturcture

Here we will see how to insert and delete elements from binary heap data structures. Suppose the initial tree is like below −

## Insertion Algorithm

insert(heap, n, item):Begin if heap is full, then exit else n := n + 1 for i := n, i > 1, set i := i / 2 in each iteration, do if item <= heap[i/2], then break heap[i] = heap[i/2] done end if heap[i] := item End

## Example

Suppose we want to insert 30 into the heap −

## Deletion Algorithm

delete(heap, n):Begin if heap is empty, then exit else item := heap[1] last := heap[n] n := n – 1 for i := 1, j := 2, j <= n, set i := j and j := j * 2, do if j < n, then if heap[j] < heap[j + 1], then j := j + 1 end if if last >= heap[j], then break heap[i] := heap[j] done end if heap[i] := last End

## Example

Suppose we want to delete 30 from the final heap −

- Related Questions & Answers
- Insertion and Deletion in STL Set C++
- Insertion and Deletion in STL Set C++ program
- Circular queues-Insertion and deletion operations in C++
- Heterogeneous Arrays in Data Sturcture
- Binomial Heaps in Data Structure
- Fibonacci Heaps in Data Structure
- Interval Heaps in Data Structure
- B-tree Deletion in Data Structure
- B+ tree Deletion in Data Structure
- Python Program to Construct a Tree & Perform Insertion, Deletion, Display
- B-tree Insertion in Data Structure
- B+ tree Insertion in Data Structure
- Deletion from a Max Heap in Data Structure
- Pairing Heaps
- Soft Heaps

Advertisements