Amazon Interview Question
Software Engineer / DevelopersHope this code will help you. It will not used any additional packages.
-----------------------------------------------------
public class HuffmanTree {
static int totalChars = 0;
public static void main(String[] args) {
// String str = "abcdedaca";
// String str = "abcdedacafghiabcdefg";
String str = "abcde";
String res = constructTreeWith(str);
System.out.println("encrypted value ...");
System.out.println(res);
}
public static String constructTreeWith(String str)
{
String encodeStr = "";
Node[] nodes = new Node[1000];
countChars(nodes, str);
sortNodes(nodes);
for(int i = 0; i < totalChars; i++)
{
System.out.println(nodes[i].ch + " = " + nodes[i].frequency);
}
createTree(nodes);
for(int i = 0; i < totalChars; i++)
{
Node temp = nodes[i];
nodes[i].tempCh = "";
while(temp.parent != null)
{
nodes[i].tempCh = temp.nodeValue + nodes[i].tempCh ;
temp=temp.parent;
}
}
for(int i = 0; i < totalChars; i++)
{
System.out.println(nodes[i].ch + " = " + nodes[i].tempCh);
}
for(int j=0;j<str.length(); j++)
{
for(int i = 0; i < totalChars; i++)
{
if(nodes[i].ch == str.charAt(j))
{
encodeStr += nodes[i].tempCh;
break;
}
}
}
return encodeStr;
}
public static void createTree(Node[] nodes)
{
int i = -1;
Node p1 = nodes[++i];
Node p2 = nodes[++i];
Node p3 = nodes[++i];
while(i < totalChars )
{
if(p1 != null && p3 != null && p1.frequency > p3.frequency)
{
Node tmp1 = new Node();
tmp1.frequency = p2.frequency + p3.frequency;
tmp1.tempCh = "Node :: " + p2.ch + p3.ch;;
tmp1.nodeValue = 1;
p2.nodeValue = 0;
p2.parent = tmp1;
p3.nodeValue = 1;
p3.parent = tmp1;
Node tmp2 = new Node();
tmp2.frequency = p1.frequency + tmp1.frequency;
tmp2.tempCh = "Node :: " + p1.ch + tmp1.ch;
tmp2.nodeValue = 0;
tmp1.parent = tmp2;
p1.parent = tmp2;
p1 = tmp2;
p2 = nodes[++i];
p3 = nodes[++i];
}
if( p1 != null && p2 != null)
{
Node tmp1 = new Node();
tmp1.frequency = p1.frequency + p2.frequency;
tmp1.tempCh = "Node :: " + p1.ch + p2.ch;
tmp1.nodeValue = 0;
p1.nodeValue = 0;
p1.parent = tmp1;
p2.nodeValue = 1;
p2.parent = tmp1;
p1=tmp1;
p2=p3;
p3=nodes[++i];
}
}
}
public static void countChars(Node[] nodes, String str)
{
for(int i = 0; i < str.length(); i++)
{
boolean isCharAvailable = false;
for(int j = 0; j < totalChars -1; j++)
{
if(nodes[j].ch == str.charAt(i))
{
nodes[j].frequency += 1;
isCharAvailable = true;
break;
}
}
if(!isCharAvailable)
{
nodes[totalChars] = new Node();
nodes[totalChars].ch = str.charAt(i);
nodes[totalChars].frequency = 1;
totalChars+=1;
}
}
}
public static void sortNodes(Node[] nodes)
{
int j = 0;
Node tmp = null;
for(int i=0;i<totalChars;i++){
j = i;
for(int k = i;k<totalChars;k++){
if(nodes[j].frequency>nodes[k].frequency){
j = k;
}
}
tmp = nodes[i];
nodes[i] = nodes[j];
nodes[j] = tmp;
}
}
}
class Node{
char ch;
int nodeValue;
int frequency;
Node parent;
String tempCh;
}
First build the table of letter -> frequency info.
Then build the Huffman tree, greedy select the two lowest cost trees and merge until you only have one left.
DFS will let you build the letter -> huffman code table.
Then the Letters in 'alphabet' all have their codes set.
- Anonymous October 29, 2010