Guest User

Untitled

a guest
Oct 22nd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.54 KB | None | 0 0
  1. using System;
  2. 002 using System.Collections.Generic;
  3. 003 using System.Linq;
  4. 004 using System.Text;
  5. 005 using System.Collections;
  6. 006  
  7. 007 namespace Huffman
  8. 008 {
  9. 009     class Node
  10. 010     {
  11. 011         public int frequency;
  12. 012         public string data;
  13. 013         public Node leftChild, rightChild;
  14. 014  
  15. 015         public Node(string data, int frequency)
  16. 016         {
  17. 017             this.data = data;
  18. 018             this.frequency = frequency;
  19. 019         }
  20. 020  
  21. 021         public Node(Node leftChild, Node rightChild)
  22. 022         {
  23. 023             this.leftChild = leftChild;
  24. 024             this.rightChild = rightChild;
  25. 025  
  26. 026             this.data = leftChild.data + ":" + rightChild.data;
  27. 027             this.frequency = leftChild.frequency + rightChild.frequency;
  28. 028         }
  29. 029     }
  30. 030  
  31. 031     class Program
  32. 032     {
  33. 033         static void Main(string[] args)
  34. 034         {
  35. 035             IList<Node> list = new List<Node>();
  36. 036  
  37. 037             //int[] array = new int[]{45, 13, 12, 16, 9, 5};
  38. 038             int[] array = new int[] { 18, 25, 21, 17, 5, 14 };
  39. 039  
  40. 040             for (int i = 0; i < array.Length; i++)
  41. 041             {
  42. 042                 list.Add(new Node("S" + (i + 1), array[i]));
  43. 043             }
  44. 044  
  45. 045             Stack<Node> stack = GetSortedStack(list);
  46. 046  
  47. 047             while (stack.Count > 1)
  48. 048             {
  49. 049                 Node leftChild = stack.Pop();
  50. 050                 Node rightChild = stack.Pop();
  51. 051  
  52. 052                 Node parentNode = new Node(leftChild, rightChild);
  53. 053  
  54. 054                 stack.Push(parentNode);
  55. 055  
  56. 056                 stack = GetSortedStack(stack.ToList<Node>());
  57. 057             }
  58. 058  
  59. 059             Node parentNode1 = stack.Pop();
  60. 060  
  61. 061             GenerateCode(parentNode1, "");
  62. 062  
  63. 063             DecodeData(parentNode1, parentNode1, 0, "0010011101001111");
  64. 064             //DecodeData(parentNode1, parentNode1, 0, "100");
  65. 065  
  66. 066             Console.ReadKey();
  67. 067         }
  68. 068  
  69. 069         public static Stack<Node> GetSortedStack(IList<Node> list)
  70. 070         {
  71. 071             for (int i = 0; i < list.Count; i++)
  72. 072             {
  73. 073                 for (int j = 0; j < list.Count; j++)
  74. 074                 {
  75. 075                     if (list[i].frequency > list[j].frequency)
  76. 076                     {
  77. 077                         Node tempNode = list[j];
  78. 078                         list[j] = list[i];
  79. 079                         list[i] = tempNode;
  80. 080                     }
  81. 081                 }
  82. 082             }
  83. 083  
  84. 084             Stack<Node> stack = new Stack<Node>();
  85. 085  
  86. 086             for (int j = 0; j < list.Count; j++)
  87. 087                 stack.Push(list[j]);
  88. 088  
  89. 089             return stack;
  90. 090         }
  91. 091  
  92. 092         public static void GenerateCode(Node parentNode, string code)
  93. 093         {
  94. 094             if (parentNode != null)
  95. 095             {
  96. 096                 GenerateCode(parentNode.leftChild, code + "0");
  97. 097  
  98. 098                 if (parentNode.leftChild == null && parentNode.rightChild == null)
  99. 099                     Console.WriteLine(parentNode.data + "{" + code + "}");
  100. 100  
  101. 101                 GenerateCode(parentNode.rightChild, code + "1");
  102. 102             }
  103. 103         }
  104. 104  
  105. 105         public static void DecodeData(Node parentNode, Node currentNode, int pointer, string input)
  106. 106         {
  107. 107             if (input.Length == pointer)
  108. 108             {
  109. 109                 if (currentNode.leftChild == null && currentNode.rightChild == null)
  110. 110                 {
  111. 111                     Console.WriteLine(currentNode.data);
  112. 112                 }
  113. 113  
  114. 114                 return;
  115. 115             }
  116. 116             else
  117. 117             {
  118. 118                 if (currentNode.leftChild == null && currentNode.rightChild == null)
  119. 119                 {
  120. 120                     Console.WriteLine(currentNode.data);
  121. 121                     DecodeData(parentNode, parentNode, pointer, input);
  122. 122                 }
  123. 123                 else
  124. 124                 {
  125. 125                     if (input.Substring(pointer, 1) == "0")
  126. 126                     {
  127. 127                         DecodeData(parentNode, currentNode.leftChild, ++pointer, input);
  128. 128                     }
  129. 129                     else
  130. 130                     {
  131. 131                         DecodeData(parentNode, currentNode.rightChild, ++pointer, input);
  132. 132                     }
  133. 133                 }
  134. 134             }
  135. 135         }
  136. 136     }
  137. 137 }
Add Comment
Please, Sign In to add comment