Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Homework4 HuffmanCoding Due11:59PMMondayMarch27 HuffmanCoding • ImplementHuffmanEncodingandDecoding andtheclassesshownonthefollowingslides. YouwillalsoneedtousetheStackclassandthe LinkedListorArrayListclass. • HuffmanEncodeexpects2commandline arguments:thenameofthesourcefile(thefile tobecompressed)andthenameofthe desOnaOonfile(thefiletowhichthe compressedfilemustbewriPen) HuffmanCoding • HuffmanDecodeexpects2commandline arguments:thenameofthesourcefile(afile thatcontainsacompressedfilecreatedby yourencoder)andthenameofthe desOnaOonfile(thefiletowhichthe uncompressedfilemustbewriPen) • Ifyourprogramsworkthecontentsof decoder’sdesOnaOonfileshouldbeexactly thesameastheoriginalsourcefileusedby encoder HuffmanCoding • Iwillpostsomelargefilesforyoutotestwith butyoushouldfirsttestwithsmallfilesyou create • YouwilldemonstrateyourimplementaOonto me. HuffmanCoding importjava.io.*; publicabstractclassBitInputStream{ protectedDataInputStreamd; publicBitInputStream(Stringfilename){ try{ d=newDataInputStream(newFileInputStream(filename)); } catch(IOExcepOone){ } } publicabstractintreadBit(); publicabstractvoidclose(); } HuffmanCoding importjava.io.*; publicabstractclassBitOutputStream{ protectedDataOutputStreamd; publicBitOutputStream(Stringfilename){ try{ d=newDataOutputStream(newFileOutputStream(filename)); } catch(IOExcepOone){ } } publicabstractvoidwriteBit(intbit); publicabstractvoidclose(); } HuffmanCoding importjava.io.*; publicclassHuffmanInputStreamextendsBitInputStream{ privateStringtree; privateinttotalChars; publicHuffmanInputStream(Stringfilename){ super(filename); try{ tree=d.readUTF(); totalChars=d.readInt(); } catch(IOExcepOone){} } } HuffmanCoding HuffmanInputStream(conOnued) publicintreadBit(){ } } publicStringgetTree(){ } publicinttotalChars(){ } publicvoidclose(){ } HuffmanCoding importjava.io.*; publicclassHuffmanOutputStreamextendsBitOutputStream{ publicHuffmanOutputStream(Stringfilename,Stringtree,inttotalChars){ super(filename); try{ d.writeUTF(tree); d.writeInt(totalChars); } catch(IOExcepOone){ } } publicvoidwriteBit(intbit){ //PREbit==0||bit==1 } publicvoidclose(){ } } HuffmanCoding importjava.uOl.*; publicclassHuffmanTree{ privateclassNode{ privateNodeled; privatechardata; privateNoderight; privateNode(NodeL,chard,NodeR){ led=L; data=d; right=R; } } privateNoderoot; privateNodecurrent; HuffmanCoding HuffmanTree(conOnued) publicHuffmanTree(){ root=null; current=null; } publicHuffmanTree(chard){ } publicHuffmanTree(Stringt,charnonLeaf){ //AssumestrepresentsapostorderrepresentaOonofthetree //whereanodeiseitheraleaforhastwochildren.nonLeaf //isthecharvalueofthedatainthenon-leafnodes } publicHuffmanTree(HuffmanTreeb1,HuffmanTreeb2,chard){ } HuffmanCoding HuffmanTree(conOnued) //Thefollowingmethodsallowauserobjecttofollowapathinthetree. //EachmethodexceptatLeafandcurrentchangesthevalueofcurrent //atLeafreturnstrueofthecurrentposiOonisaleaf,otherwiseitreturnsfalse //currentreturnsthedatavalueinthecurrentNode publicvoidmoveRoot(){ } publicvoidmoveLed(){ } publicvoidmoveRight(){ } publicbooleanatLeaf(){ } publiccharcurrent(){ } HuffmanCoding HuffmanTree(conOnued) //Innerclasstocreateaniterator.Theiteratorallowstheuserclasstofindallpathsfrom //theroottoaleaf.Thepathsareseqeuncesof0sand1s.0meansledand1meansright //Youwillfinditeasiertofindallthepathswhentheiteratoriscreated. publicclassPathIteratorimplementsIterator<String>{ publicPathIterator(){ } } publicbooleanhasNext(){ } publicStringnext(){ } publicvoidremove(){ //opOonalmethodnotimplemented } HuffmanCoding HuffmanTree(conOnued) publicIterator<String>iterator(){ //returnaPathIteratorobject } publicStringtoString(){ } } HuffmanCoding importjava.io.*; importjava.uOl.*; publicclassHuffmanEncode{ publicHuffmanEncode(Stringin,Stringout){ //Implementsthehuffmanencodingalgorithm //Addprivatemethodsasneeded } publicstaOcvoidmain(Stringargs[]){ newHuffmanEncode(args[0],args[1]); } } HuffmanCoding importjava.io.*; importjava.uOl.*; publicclassHuffmanDecode{ publicHuffmanDecode(Stringin,Stringout){ //Implementsthehuffmandecodingalgorithm //Addprivatemethodsasneeded } publicstaOcvoidmain(Stringargs[]){ newHuffmanDecode(args[0],args[1]); } } HuffmanCoding • InaddiOontotheclassesontheprevious slidesyoushouldmodifytheBinaryHeapclass Idiscussedinclasssoitcanbeusedasa priorityqueuetobuildtheHuffmantree. Homework4Submission • Youwilldemonstrateyourprogramtomeeitheron yourownmachineoronamachineintheCSlab • IfyoudemobetweenMondayMar.27andThursday Mar.30theprogramisonOme.Iwillnotbeableto dodemosforeveryoneonthelastdaysoyoushould notwaitunOlthelastdaytodemo. • YoucandemofromFridayMar.31unOlThursday Apr.6forupto50%ofthepoints.