Huffman encoding

Can you please send the heap.java file used in huffmanEncoding code video as it has not been done before in any videoi

@hemantkrjain2003_41a7cca4cdd1056f I am sending you my .java for Huffman Encoding you can use this. Download files from the below G-Drive link :
https://drive.google.com/drive/folders/1PgeV4vh64I4pyEX58nJIpPs9uYjm1N7I?usp=share_link

Brother i want the source code of the heap file used in this code to initialize the heap

@hemantkrjain2003_41a7cca4cdd1056f This file contains everything bro …Look for a comment : //Making min heap
That’s where the code for minheap starts.

In the video of huffman encoding ‘HeapminHeap=new Heap<>(true);’
this statement is used and i want the source code of this Heap working mentioned in the quoted line

@hemantkrjain2003_41a7cca4cdd1056f :

import java.util.ArrayList;

public class Heap<T extends Comparable<T>> {
ArrayList<T> data = new ArrayList<>();
	
	public void add(T item) {
		data.add(item);
		upheapify(data.size() - 1);
		
	}
	private void upheapify(int ci) {
		//We dont need any basecase because whenever we are in top element of heap it will give pi = ci therefore condition will be false
		int pi = (ci - 1)/2;
		
		if(data.get(ci).compareTo(data.get(pi))<0) {
			swap(pi,ci);
			upheapify(pi);
		}
		
	}
	private void swap(int i, int j) {
		
		T ith = data.get(i);
		T jth = data.get(j);
		
		data.set(i, jth);
		data.set(j, ith);
		
	}
	public void display() {
		System.out.println(data);
	}
	public int size() {
		return this.data.size();
	}
	public boolean isEmpty() {
		return this.size() == 0;
	}
	public T remove() {
		//highest priority element will be deleted
		swap(0,size()-1);
		T ans = data.remove(size()-1);
		downHeapify(0);
		return ans;
	}
	private void downHeapify(int pi) {
		//in add we used ci but here we have pi
		int rci = 2*pi + 2;
		int lci = 2*pi + 1;
		int mini = pi;
		//We are comparing value of rc and lc with parent because parent can also be less than rc or lc therefore at last
		//Both rc and lc are compared then if mini = pi this means parent is right in its position
		//no need to do anything else
		//otherwise make a recursive call
		//lci < size()is also a check for base case. since there will not be any code for pi = mini
		if(lci < this.data.size() && this.data.get(lci).compareTo(data.get(mini)) > 0) {
			mini = lci;
		}
		if(rci < this.data.size() && this.data.get(rci).compareTo(data.get(mini)) > 0) {
			mini = rci;
		}  
		if(pi != mini) {
			
			//if mini != pi that means one of the if condition has updated the value of mini by any child
			swap(mini,pi);
			downHeapify(mini);
		}
	}
	public T get() {
		return this.data.get(0);
	}
}

Bro i want the source code of this file used in video

@hemantkrjain2003_41a7cca4cdd1056f Look at it carefully it the same code that I sent you.

1 Like