Advertisement
Guest User

LZW

a guest
Sep 17th, 2013
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.78 KB | None | 0 0
  1. import java.io.FileInputStream;
  2. import java.io.FileNotFoundException;
  3. import java.io.IOException;
  4.  
  5. class LZWTree
  6. {
  7.     private class Node
  8.     {
  9.         public Node()
  10.         {
  11.             this.letter = '\\';
  12.         }
  13.        
  14.         public Node(char b)
  15.         {
  16.             this.letter = b;
  17.         }
  18.        
  19.         public char getLetter()
  20.         {
  21.             return this.letter;
  22.         }
  23.        
  24.         public Node getLeftZeroChild()
  25.         {
  26.             return this.leftZeroChild;
  27.         }
  28.        
  29.         public Node getRightOneChild()
  30.         {
  31.             return this.rightOneChild;
  32.         }
  33.        
  34.         public void addLeftZeroChild(Node newLeftZeroChild)
  35.         {
  36.             this.leftZeroChild = newLeftZeroChild;
  37.         }
  38.        
  39.         public void addRightOneChild(Node newRightOneChild)
  40.         {
  41.             this.rightOneChild = newRightOneChild;
  42.         }
  43.            
  44.         private char letter;
  45.         private Node leftZeroChild, rightOneChild;     
  46.     }
  47.    
  48.     public LZWTree()
  49.     {
  50.         currentNode = root;
  51.     }
  52.    
  53.     public void insert(char b)
  54.     {
  55.         if ('0' == b)
  56.         {
  57.             if (null == currentNode.getLeftZeroChild())
  58.             {
  59.                 currentNode.addLeftZeroChild(new Node('0'));
  60.                 currentNode = root;
  61.             }
  62.             else
  63.                 currentNode = currentNode.getLeftZeroChild();
  64.         }
  65.         else
  66.         {
  67.             if (null == currentNode.getRightOneChild())
  68.             {
  69.                 currentNode.addRightOneChild(new Node('1'));
  70.                 currentNode = root;
  71.             }
  72.             else
  73.                 currentNode = currentNode.getRightOneChild();
  74.         }
  75.     }
  76.    
  77.     public void print(java.io.Writer output) throws IOException
  78.     {
  79.         depth = 0;
  80.         print(root, output);
  81.     }
  82.    
  83.     public int getDepth()
  84.     {
  85.         depth = maxDepth = 0;
  86.         _getDepth(root);
  87.         return maxDepth - 1;
  88.     }
  89.    
  90.     public double getAverage()
  91.     {
  92.         depth = averageSum = averageCount = 0;
  93.         _getAverage(root);
  94.         average = ((double) averageSum) / averageCount;
  95.         return average;
  96.     }
  97.    
  98.     public double getVariance()
  99.     {
  100.         average = getAverage();
  101.         varianceSum = 0.0;
  102.         depth = averageCount = 0;
  103.        
  104.         _getVariance(root);
  105.        
  106.         if (averageCount - 1 > 0)
  107.             variance = Math.sqrt(varianceSum / (averageCount - 1));
  108.         else
  109.             variance = Math.sqrt(varianceSum);
  110.         return variance;
  111.     }
  112.    
  113.     private void print(Node node, java.io.Writer output) throws IOException
  114.     {
  115.         if (null != node)
  116.         {
  117.             ++depth;
  118.             print(node.getRightOneChild(), output);
  119.            
  120.             for(int i=0; i < depth; ++i)
  121.                 output.write(new String("---"));
  122.             output.write(node.getLetter() + "(" + (depth - 1) + ")" + '\n');
  123.            
  124.             print(node.getLeftZeroChild(), output);
  125.             --depth;
  126.         }
  127.     }
  128.    
  129.     protected void _getDepth(Node node)
  130.     {
  131.          if (null != node)
  132.          {
  133.                  ++depth;
  134.                  
  135.                  if(depth > maxDepth)        
  136.                      maxDepth = depth;
  137.                  
  138.                  _getDepth(node.getRightOneChild());
  139.                  _getDepth(node.getLeftZeroChild());
  140.                  --depth;
  141.          }
  142.     }
  143.    
  144.     protected void _getAverage(Node node)
  145.     {
  146.         if (null != node)
  147.         {
  148.                 ++depth;
  149.                 _getAverage(node.getRightOneChild());
  150.                 _getAverage(node.getLeftZeroChild());
  151.                 --depth;
  152.                 if( null == node.getRightOneChild() && null == node.getLeftZeroChild() )
  153.                 {
  154.                         ++averageCount;
  155.                         averageSum += depth;
  156.                 }
  157.         }              
  158.     }
  159.    
  160.     protected void _getVariance(Node node)
  161.     {
  162.         if(null != node)
  163.         {
  164.                 ++depth;
  165.                 _getVariance(node.getRightOneChild());
  166.                 _getVariance(node.getLeftZeroChild());
  167.                 --depth;
  168.                 if( null == node.getRightOneChild() && null == node.getLeftZeroChild() )
  169.                 {
  170.                         ++averageCount;
  171.                         varianceSum += ((depth - average) * (depth - average));
  172.                 }
  173.         }
  174.     }
  175.    
  176.     private Node currentNode;
  177.     private int depth, averageSum, averageCount;
  178.     private double varianceSum;
  179.     protected Node root = new Node();
  180.     protected int maxDepth;
  181.     protected double average, variance;
  182. }
  183.  
  184. public class Program
  185. {
  186.     public static void main(String[] args) throws FileNotFoundException, IOException
  187.     {
  188.         String usage = "Usage: lzwtree in_file -o out_file";
  189.        
  190.         if (args.length < 3)
  191.         {
  192.             System.out.println(usage);
  193.             System.exit(-1);
  194.         }
  195.         if (!"-o".equals(args[1]))
  196.         {
  197.             System.out.println(usage);
  198.             System.exit(-1);
  199.         }
  200.        
  201.         java.io.InputStream input;
  202.         java.io.Writer output = new java.io.FileWriter(args[2]);
  203.        
  204.         try
  205.         {
  206.             input = new FileInputStream(args[0]);
  207.         }
  208.         catch (FileNotFoundException e)
  209.         {
  210.             System.out.println("cannot find input file...");
  211.             System.exit(-1);
  212.         }  
  213.        
  214.         input = new FileInputStream(args[0]);
  215.         char ch;
  216.         LZWTree lzw = new LZWTree();
  217.        
  218.  
  219.         while(0 < input.available())
  220.         {
  221.             ch = (char)input.read();
  222.             if (0x0a == ch) break;
  223.         }
  224.        
  225.         boolean inComment = false;
  226.         while(0 < input.available())
  227.         {
  228.             ch = (char)input.read();
  229.  
  230.                     if(0x3e == ch)
  231.                     {
  232.                             inComment = true;
  233.                             continue;
  234.                     }
  235.                     if(0x0a == ch)
  236.             {
  237.                             inComment = false;
  238.                             continue;
  239.                     }
  240.                     if(inComment) continue;
  241.                     if(0x4e == ch) continue;
  242.  
  243.                     for(int i=0; i<8; i++)
  244.             {
  245.                             if((ch & 0x80)!=0)
  246.                                     lzw.insert('1');
  247.                             else
  248.                                     lzw.insert('0');
  249.                             ch <<=1 ;
  250.                     }
  251.         }
  252.              
  253.              lzw.print(output);    
  254.              output.write("depth = " + lzw.getDepth() + '\n');
  255.              output.write("mean = " + lzw.getAverage() + '\n');
  256.              output.write("var = " + lzw.getVariance() + '\n');
  257.  
  258.              input.close();
  259.              output.close();   
  260.     }
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement