Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

i want the Fsm & the Fsmd for the Shannon-Fano algorithm this is the code thank

ID: 3842584 • Letter: I

Question

i want the Fsm & the Fsmd for the Shannon-Fano algorithm this is the code thank you

import java.util.ArrayList;

import java.util.HashMap;

import java.util.Iterator;

import java.util.LinkedHashMap;

import java.util.List;

import java.util.Map;

public class ShannonFano { public HashMap compress(HashMap freq) { HashMap result = new HashMap(); List charList = new ArrayList(); Iterator entries = freq.entrySet().iterator(); while( entries.hasNext() ) { Map.Entry entry = (Map.Entry)entries.next(); charList.add(entry.getKey()); } addBit(result, charList, true); return result; } private void addBit(HashMap result, List charList, boolean up) { String bit = ""; if( !result.isEmpty() ) { bit = (up) ? "0" : "1"; } for( Character c : charList ) { String s = (result.get(c) == null) ? "" : result.get(c); result.put(c, s + bit); } if( charList.size() >= 2 ) { int separator = (int)Math.floor((float)charList.size()/2.0); List upList = charList.subList(0, separator); addBit(result, upList, true); List downList = charList.subList(separator, charList.size()); addBit(result, downList, false); } } }

Explanation / Answer

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class ShannonFano
{
public HashMap compress(HashMap freq)
{
HashMap result = new HashMap();
List charList = new ArrayList();
Iterator entries = freq.entrySet().iterator();
while( entries.hasNext() )
{ Map.Entry entry = (Map.Entry)entries.next();
charList.add(entry.getKey());
}
addBit(result, charList, true);
return result;
}
private void addBit(HashMap result, List charList, boolean up)
{
String bit = ""; if( !result.isEmpty() )
{
bit = (up) ? "0" : "1";
}
for( Character c : charList )
{
String s = (result.get(c) == null) ? "" :
result.get(c);
result.put(c, s + bit);
}
if( charList.size() >= 2 )
{
int separator = (int)Math.floor((float)charList.size()/2.0);
List upList = charList.subList(0, separator);
addBit(result, upList, true);
List downList = charList.subList(separator, charList.size());
addBit(result, downList, false);
}
}
}