Coders Packet

A Java program to implement a Heap Tree.

By Naman Panpaliya

A Java program to implement a Heap Tree of 10 nodes and perform insertion and deletion on it.

In this blog, you will come to know about Heap Tree and how to implement a Heap tree of 10 nodes and to perform insertion and deletion on it.

 

Heap Tree

A Heap Tree is a Complete Binary tree where Heap is a  special Tree-based data structure. Heaps can be of two types:

  1. Max-Heap: In Max-heap the value of the root node is greater than or equal to either of its children nodes. 
  2. Min-Heap: In Min-heap the value of the root node is less than or equal to either of its children nodes.

 

Process of Deletion in Heap

As we all know that deleting an element at an intermediary position in heap is difficult and costly so to avoid the cost of the project and to minimize the difficulty of it we simply replace the element which is to be deleted by the last element and then delete the last element.

  • Firstly replace the element which is to be deleted by the last element.
  • Then delete the last element from the Heap.
  • Now the last element is at the position of the root node. So, the element may not follow the property of the heap.
  • Therefore, we have to heapify the last node which is placed at the position of the root.

Therefore this is the process of deletion in the heap.

Process of Insertion in Heap :

To insert the elements in the heap the one can use a similar approach as we used for deletion. 

  • Firstly to insert the new element in the heap we have to increase the heap size by 1, so that it can store the new element.
  • Then we can insert a new element at the end of the Heap.
  • Now the element which we store in the heap may not follow the properties of the heap. So, heapify this newly inserted element by following the bottom-up approach.

 

 

Here is the output for the code 

Output

Download project

Reviews Report

Submitted by Naman Panpaliya (Naman9999)

Download packets of source code on Coders Packet