You are on page 1of 6

import java.util.

Comparator;

import java.util.HashMap;

import java.util.PriorityQueue;

import java.util.Scanner;

class Huffman {

public static HashMap<Character, String> huffmanCodes = new HashMap<>();

// recursive function to print the

// huffman-code through the tree traversal.

// Here s is the huffman - code generated.

public static void printCode(HuffmanNode root, String s) {

// base case; if the left and right are null

// then it's a leaf node and we print

// the code s generated by traversing the tree.

if (root.left == null && root.right == null && Character.isLetter(root.c)) {

// c is the character in the node

System.out.println(root.c + ":" + s);

huffmanCodes.put(root.c, s); // Store the code for this character

return;

// if we go to left then add "0" to the code.

// if we go to the right add "1" to the code.

// recursive calls for left and

// right sub-tree of the generated tree.

printCode(root.left, s + "0");

printCode(root.right, s + "1");

}
// main function

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the number of characters: ");

int n = scanner.nextInt();

char[] charArray = new char[n];

double[] charfreq = new double[n];

for (int i = 0; i < n; i++) {

System.out.print("Enter character " + (i + 1) + ": ");

charArray[i] = scanner.next().charAt(0);

System.out.print("Enter frequency for character " + charArray[i] + ": ");

charfreq[i] = scanner.nextDouble();

PriorityQueue<HuffmanNode> q = new PriorityQueue<>(n, new MyComparator());

for (int i = 0; i < n; i++) {

HuffmanNode hn = new HuffmanNode();

hn.c = charArray[i];

hn.data = charfreq[i];

hn.left = null;

hn.right = null;

q.add(hn);

HuffmanNode root = null;


while (q.size() > 1) {

HuffmanNode x = q.peek();

q.poll();

HuffmanNode y = q.peek();

q.poll();

HuffmanNode f = new HuffmanNode();

f.data = x.data + y.data;

f.c = '-';

f.left = x;

f.right = y;

root = f;

q.add(f);

// print the codes by traversing the tree

printCode(root, "");

// Calculate and print the average code length

double totalWeightedLength = 0.0;

double totalFrequency = 0.0;

for (int i = 0; i < n; i++) {

char character = charArray[i];

double frequency = charfreq[i];

if (huffmanCodes.containsKey(character)) {

String code = huffmanCodes.get(character);

int codeLength = code.length();

double weightedLength = frequency * codeLength;

totalWeightedLength += weightedLength;
totalFrequency += frequency;

double averageCodeLength = totalWeightedLength / totalFrequency;

System.out.println("Average Code Length: " + averageCodeLength);

class HuffmanNode {

double data;

char c;

HuffmanNode left;

HuffmanNode right;

class MyComparator implements Comparator<HuffmanNode> {

public int compare(HuffmanNode x, HuffmanNode y) {

return Double.compare(x.data, y.data);

}
import java.util.Arrays;

public class StringDiscreteWaveletTransform {

public static void main(String[] args) {

String inputString = "hello";

int[] signal = convertStringToSignal(inputString);

System.out.println("Original Signal: " + Arrays.toString(signal));

int[] transformedSignal = dwtHaar(signal);

System.out.println("Transformed Signal: " + Arrays.toString(transformedSignal));

private static int[] convertStringToSignal(String inputString) {

int[] signal = new int[inputString.length()];

for (int i = 0; i < inputString.length(); i++) {

signal[i] = (int) inputString.charAt(i);

return signal;

private static int[] dwtHaar(int[] signal) {

int n = signal.length;

int[] transformedSignal = new int[n];

for (int i = 0; i < n / 2; i++) {

int j = i * 2;

transformedSignal[i] = (signal[j] + signal[j + 1]) / 2;

transformedSignal[i + n / 2] = (signal[j] - signal[j + 1]) / 2;

}
return transformedSignal;

You might also like