-
Notifications
You must be signed in to change notification settings - Fork 858
/
HuffmanDecoding.java
37 lines (34 loc) · 828 Bytes
/
HuffmanDecoding.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
*
* Problem Statement-
* [Huffman Decoding](https://www.hackerrank.com/challenges/tree-huffman-decoding/problem)
*
*/
package com.javaaid.hackerrank.solutions.datastructures.trees;
/**
* @author Kanahaiya Gupta
*
*/
public class HuffmanDecoding {
class Node {
public int frequency; // the frequency of this tree
public char data;
public Node left, right;
}
void decode(String S, Node root) {
Node currentRoot = root;
if (root == null)
return;
char[] a = S.toCharArray();
for (int i = 0; i < a.length; i++) {
if (a[i] == '0')
currentRoot = currentRoot.left;
else
currentRoot = currentRoot.right;
if (currentRoot.left == null && currentRoot.right == null) {
System.out.print(currentRoot.data);
currentRoot = root;
}
}
}
}