Advertisement
Guest User

lzw

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