Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 2 of 2 FirstFirst 12
Results 26 to 36 of 36

Thread: Help Huffman Coding>>

  1. #26
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,143
    Thanks
    48
    Thanked 2,157 Times in 2,130 Posts

    Default Re: Help Huffman Coding>>

    The code you posted on post#13 does not compile.
    Please don't post code that has compiler errors. I assume that you are trying to get this project working and are not just posting junk code hoping someone will fix it.
    Either fix the errors or ask a question about what the problem is.

    If the code you are working on is different from what you posted in #13, you're wasting my time.

  2. #27
    Member
    Join Date
    Jul 2011
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help Huffman Coding>>

    no i'm just trying to fix these codes in post 13# and i have already posted the algorithm for it
    and this is a forum for what's wrong with my code
    if u have comment on this code or how to fix it then write it
    if not thank you for your time

  3. #28
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,143
    Thanks
    48
    Thanked 2,157 Times in 2,130 Posts

    Default Re: Help Huffman Coding>>

    Please copy and paste here the compiler error messages you are getting.
    Without them I can't help you fix your program.

  4. #29
    Member
    Join Date
    Jul 2011
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help Huffman Coding>>

    Exception in thread "main" java.lang.NullPointerException
    at HuffmanApp.main(HuffmanApp.java:26)
    Exception in thread "main" java.lang.NullPointerException
    at HuffmanApp.main(HuffmanApp.java:29)

    Exception in thread "main" java.lang.NullPointerException
    at HuffmanApp.main(HuffmanApp.java:32)

  5. #30
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,143
    Thanks
    48
    Thanked 2,157 Times in 2,130 Posts

    Default Re: Help Huffman Coding>>

    Those are execution time errors. That means that your code compiles. The code you posted on post#13 does NOT compile. More confusion????
    Post the code that you have for the makeCodeTable method.


    A NullPointerException means that the variable being used has a null value. Your code must give values to variables to prevent that error. Look at lines: 26, 29 and 32. What variable on each of those lines is null?
    Use a println to show the variable's value if you can't see which one is null.

  6. #31
    Member
    Join Date
    Jul 2011
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help Huffman Coding>>

    These are the classes that i'm working on now
    PHP Code:
    import java.io.*;
    import java.util.*;     
    class 
    Huffman
       
    {
       private 
    PriorityQ theQueue;
       private 
    String inString;        // input from user
       
    private int strlen;
       private 
    String capsString;      // converted to all caps
       
    private Tree[] treeArray;
       private 
    Tree huffTree;          // Huffman tree
       
    private int freqTable[];        // letter frequencies
       
    private int alphabetSize;       // size of frequency table
       
    private String codeTable[];     // code for each letter
       
    private String codedMsg;        // binary string
       
    private String decodedMsg;      // back to original msg
    // -------------------------------------------------------------
       
    Huffman(String s)                     // constructor
          
    {
          
    inString s;
          
    strlen inString.length();
          
    alphabetSize 28;                 // 26 letters, cr, lf
          
    theQueue = new PriorityQ(alphabetSize); // make the queue
          
    capsString "";
          
    codeTable = new String[alphabetSize];   // make code table

          
    makeFreqTable();          // construct frequency table
          
    queueTrees();             // put one-node trees in queue
          
    makeHuffTree();           // construct Huffman tree
          
    }  // end constructor
    // -------------------------------------------------------------
       
    public void displayTree()
          {
          if(
    huffTree != null)
             
    huffTree.display();
          }
    // -------------------------------------------------------------
      
    private void makeFreqTable()
          {
          
    freqTable = new int[alphabetSize];
          
    int temp;
          
    char sp 32;                      // space
          
    for(int j=0j<strlenj++)        // for every char
             
    {                               // of user's input msg
             
    char ch inString.charAt(j);
             if(
    ch>96 && ch<123)             // lowercase (97-122)
                
    {
                
    temp = (int)ch 32;         // convert to upper
                
    ch = (char)temp;             //    (65-90)
                
    }
             if(
    ch==32)                      // space becomes 91
                
    ch 91;
             else if(
    ch==10)                 // linefeed becomes 92
                
    ch 92;
             else if(
    ch<65 || ch>92)         // if not uppercase
                
    continue;                    // letter, ignore it
             
    capsString capsString ch;   // add to caps string
             
    int index = (int)ch 65;       // convert to 0 to 27
             
    freqTable[index]++;             // keep count in
             
    }  // end for                   //    frequency table
          
    System.out.println(capsString);    // all-caps string
          
    for(int j=0j<28j++)            // row of characters
             
    System.out.print( (char)(j+65) + " ");
          
    System.out.println("");
          for(
    int j=0j<28j++)            // row of frequencies
             
    System.out.print(freqTable[j] + " ");
          
    System.out.println("");
          }  
    // end makeFreqTable()
    // -------------------------------------------------------------
       
    private void queueTrees()             // put trees in queue
          
    {
           
    HuffmanApp a = new HuffmanApp();
          for (
    int i =0;i<freqTable.length;i++){
              if (
    freqTable[i]==0)
                  continue;
              else{
                  
    char l codeTable[i].charAt(i);
                  
    Node newNode = new Node (l,freqTable[i]);
                  
                  
    Tree t1 = new Tree(newNode);
                  
    theQueue.insert(t1);
              }
          }
         
    // for each char in frequency table forget unused characters
             
    // make node
             
    // make tree
    // put  the tree in queue
             
          
    }  // end queueTrees()
    // -------------------------------------------------------------
       
    private void makeHuffTree()           // make Huffman tree
          
    {
           for (
    int i=1;i<alphabetSize;i++){
               
    Tree a=theQueue.remove();
               
    int freq1=a.getFreq();
               
    Tree b =theQueue.remove();
               
    int freq2=b.getFreq();
               
    int newfreq =freq1+freq2;
               
    Node newNode = new Node(' ',newfreq);
               
    Tree c = new Tree(newNode);
               
    theQueue.insert(c);
           }
          

          }  
    // end makeHuffTree()
    // -------------------------------------------------------------
       
    private void makeCodeTable(Node ndString bc)
          {
           if (
    nd.leftChild!=null && nd.rightChild!=null){
               
    makeCodeTable(nd.leftChild,bc+"0");
               
    makeCodeTable(nd.rightChild,bc+"1");
           }
         
    //  else 
            //   codeTable=codeTable+bc;

      
    }  // end makeCodeTable()
    // -------------------------------------------------------------
       
    public void code()                    // encode the msg
          
    {
          
    makeCodeTable(huffTree.root"");
          for(
    int j=0j<alphabetSizej++)  // display code table
             
    {
             if(
    codeTable[j] == null)
                continue;
             
    char ch = (char)(j+65);
             
    System.out.println(ch " " codeTable[j]);
             }
          
    codedMsg "";                     // encode the msg
          
    for(int j=0j<capsString.length(); j++)   // for each char
             
    {                                       // get the code
             
    int index = (int)capsString.charAt(j) - 65;
             
    codedMsg codedMsg codeTable[index]; // add to msg
             
    }
          
    System.out.println("Coded message:\n" codedMsg);
          }  
    // end code()
    // -------------------------------------------------------------
       
    public void decode()
          {
          }  
    // end decode()
    // -------------------------------------------------------------
       
    }  // end class Huffman 
    PHP Code:
    import java.io.*;
    import java.util.*;     

    class 
    HuffmanApp
       
    {
       public static 
    void main(String[] argsthrows IOException
          
    {
          
    Huffman huff null;
          
    int value;
          
    String str;

          while(
    true)
             {
             
    System.out.print("Enter first letter of ");
             
    System.out.print("enter, show, code, or decode: ");
             
    int choice getChar();
             switch(
    choice)
                {
                case 
    'e':
                   
    System.out.println(
                      
    "Enter text lines, terminate with $");
                   
    str getText();
                   
    huff = new Huffman(str);
                   break;
                case 
    's':
                   
    huff.displayTree();
                   break;
                case 
    'c':
                   
    huff.code();
                   break;
                case 
    'd':
                   
    huff.decode();
                   break;
                default:
                   
    System.out.print("Invalid entry\n");
                }  
    // end switch
             
    }  // end while
          
    }  // end main()
    // -------------------------------------------------------------
       
    public static String getString() throws IOException
          
    {
          
    InputStreamReader isr = new InputStreamReader(System.in);
          
    BufferedReader br = new BufferedReader(isr);
          
    String s br.readLine();
          return 
    s;
          }
    // -------------------------------------------------------------
       
    public static String getText() throws IOException
          
    {
          
    String outStr=""str "";
          while(
    true)
             {
             
    str getString();
             if( 
    str.equals("$") )
                return 
    outStr;
             
    outStr outStr str "\n";
             }
          }  
    // end getText()
    // -------------------------------------------------------------
       
    public static char getChar() throws IOException
          
    {
          
    String s getString();
          return 
    s.charAt(0);
          }
    //-------------------------------------------------------------
       
    public static int getInt() throws IOException
          
    {
          
    String s getString();
          return 
    Integer.parseInt(s);
          }
    // -------------------------------------------------------------
       
    }  // end class HuffmanApp 
    PHP Code:
    // huffman.java
    // demonstrates Huffman trees
    // to run this program: C>java HuffmanApp
    import java.io.*;
    import java.util.*;               // for Stack class
    ////////////////////////////////////////////////////////////////
    class Node
       
    {
       public 
    char ch;                // letter
       
    public int freq;               // instances of this letter
       
    public Node leftChild;         // this node's left child
       
    public Node rightChild;        // this node's right child
    // -------------------------------------------------------------
       
    Node(char cint f)            // constructor
          
    {
          
    ch c;
          
    freq f;
          }
    // -------------------------------------------------------------
       
    public void displayNode()      // display ourself
          
    System.out.print( ch "" freq ); }
       }  
    // end class Node
    ////////////////////////////////////////////////////////////////
    class Tree
       
    {
       public 
    Node root;              // first node of tree
    // -------------------------------------------------------------
       
    public Tree(Node nd)           // constructor
          
    root nd; }              // root of tree from argument
    // -------------------------------------------------------------
       
    public int getFreq()           // returns node's word freq
          
    { return root.freq; }
    // -------------------------------------------------------------
       
    public void display()
          {
          
    Stack globalStack = new Stack();
          
    globalStack.push(root);
          
    int nBlanks 32;
          
    boolean isRowEmpty false;
          
    System.out.println(
          
    "......................................................");
          while(
    isRowEmpty==false)
             {
             
    Stack localStack = new Stack();
             
    isRowEmpty true;

             for(
    int j=0j<nBlanksj++)
                
    System.out.print(' ');

             while(
    globalStack.isEmpty()==false)
                {
                
    Node temp = (Node)globalStack.pop();
                if(
    temp != null)
                   {
                   
    temp.displayNode();
                   
    localStack.push(temp.leftChild);
                   
    localStack.push(temp.rightChild);

                   if(
    temp.leftChild != null ||
                                       
    temp.rightChild != null)
                      
    isRowEmpty false;
                   }
                else
                   {
                   
    System.out.print("--");
                   
    localStack.push(null);
                   
    localStack.push(null);
                   }
                for(
    int j=0j<nBlanks*2-2j++)
                   
    System.out.print(' ');
                }  
    // end while globalStack not empty
             
    System.out.println();
             
    nBlanks /= 2;
             while(
    localStack.isEmpty()==false)
                
    globalStack.pushlocalStack.pop() );
             }  
    // end while isRowEmpty is false
          
    System.out.println(
          
    "......................................................");
          }  
    // end displayTree()
    // -------------------------------------------------------------
       
    }  // end class Tree
    ////////////////////////////////////////////////////////////////
    class PriorityQ
       
    {
       
    // array in sorted order, from max at 0 to min at size-1
       
    private int maxSize;
       private 
    Tree[] queArray;
       private 
    int nItems;
    //-------------------------------------------------------------
       
    public PriorityQ(int s)          // constructor
          
    {
          
    maxSize s;
          
    queArray = new Tree[maxSize];
          
    nItems 0;
          }
    //-------------------------------------------------------------
       
    public void insert(Tree item)    // insert item
          
    {
          
    int j;

          if(
    nItems==0)                         // if no items,
             
    queArray[nItems++] = item;         // insert at 0
          
    else                                  // if items,
             
    {
             for(
    j=nItems-1j>=0j--)      // start at end,
                
    {                            // if new item larger,
                
    if( item.root.freq queArray[j].root.freq )
                   
    queArray[j+1] = queArray[j]; // shift upward
                
    else                            // if smaller,
                   
    break;                       // done shifting
                
    }  // end for
             
    queArray[j+1] = item;              // insert it
             
    nItems++;
             }  
    // end else (nItems > 0)
          
    }  // end insert()
    //-------------------------------------------------------------
       
    public Tree remove()             // remove minimum item
          
    { return queArray[--nItems]; }
    //-------------------------------------------------------------
       
    public Tree peekMin()            // peek at minimum item
          
    { return queArray[nItems-1]; }
    //-------------------------------------------------------------
       
    public boolean isEmpty()         // true if queue is empty
          
    { return (nItems==0); }
    //-------------------------------------------------------------
       
    public boolean isFull()          // true if queue is full
          
    { return (nItems == maxSize); }
    //-------------------------------------------------------------
       
    public int getSize()
          { return 
    nItems; }
    //-------------------------------------------------------------
       
    }  // end class PriorityQ
    //////////////////////////////////////////////////////////////// 
    thx for your help

  7. #32
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Help Huffman Coding>>

    Sorry, I must have missed where you posted up your attempt at the decode method - which post was it?

  8. #33
    Member
    Join Date
    Jul 2011
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help Huffman Coding>>

    Quote Originally Posted by cool_97 View Post
    queueTrees()
    makeHuffTree()
    makeCodeTable
    but not sure if it's correct
    didn't complete it yet trying to solve these three first

  9. #34
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,143
    Thanks
    48
    Thanked 2,157 Times in 2,130 Posts

    Default Re: Help Huffman Coding>>

    This code is NOT the same as post#13.
    You are working with different code than you have posted.
    Sorry, but my patience is gone. This is a big waste of time if you don't post the correct code.

    Good luck with your project. Maybe some other helper will come and pick this up.
    I'm done.
      private void makeCodeTable(Node nd, String bc) 
          { 
           if (nd.leftChild!=null && nd.rightChild!=null){ 
               makeCodeTable(nd.leftChild,bc+"0"); 
               makeCodeTable(nd.rightChild,bc+"1"); 
           } 
         //  else                <<<<<<<<<<<<<<<<<<HERE
            //   codeTable=codeTable+bc;  <<<<<<<<<<<<HERE
     
      }  // end makeCodeTable()

    Post #13 code
    private void makeCodeTable(Node nd, String bc) 
          { 
           if (nd.leftChild!=null && nd.rightChild!=null){ 
               makeCodeTable(nd.leftChild,bc+"0"); 
               makeCodeTable(nd.rightChild,bc+"1"); 
           } 
           else  
               codeTable=codeTable+bc; 
     
      }  // end makeCodeTable()

  10. #35
    Member
    Join Date
    Jul 2011
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help Huffman Coding>>

    no i'm not
    i'm working on it now
    and i check if it's an error i put // to avoid the error in compiling
    because this line codeTable=codeTable+bc; was underlined by red color



    by the way if you don't want to help then it's your call
    but i'm being honest
    take care and thanks for nothing

  11. #36
    Member
    Join Date
    Jul 2011
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help Huffman Coding>>

    yes another thing
    please understand the following i don't have the right code if i have it i wouldn't post here these functions were empty that was the assignment to complete them and compile

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Huffman Tree
    By Aberforth in forum Java Theory & Questions
    Replies: 2
    Last Post: November 9th, 2010, 04:49 AM
  2. Coding error
    By chemy G in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 2nd, 2010, 08:36 AM
  3. Help me in java coding
    By smallmac in forum Java Theory & Questions
    Replies: 5
    Last Post: August 2nd, 2010, 09:50 AM
  4. Java coding help
    By Javalover1 in forum The Cafe
    Replies: 0
    Last Post: April 12th, 2010, 08:11 PM
  5. Sudoku coding help...please
    By sketch_flygirl in forum Algorithms & Recursion
    Replies: 2
    Last Post: December 5th, 2009, 09:07 PM