Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- public class BinaryTree
- {
- Node tree;
- public BinaryTree() { tree = null; }
- public void Solve(string s)
- {
- List<Node> tree = new List<Node>();
- SortedDictionary<char, int> cnt = new SortedDictionary<char, int>();
- for (int i = 0; i < s.Length; ++i)
- {
- if (cnt.ContainsKey(s[i]))
- cnt[s[i]]++;
- else
- cnt.Add(s[i], 1);
- }
- foreach (var v in cnt)
- {
- Node cur = new Node(v.Value, v.Key);
- tree.Add(cur);
- }
- while (tree.Count != 1)
- {
- tree.Sort((a, b) => a.inf.CompareTo(b.inf));
- Node l = tree[0];
- tree.RemoveAt(0);
- Node r = tree[0];
- tree.RemoveAt(0);
- Node parent = new Node(ref l, ref r);
- tree.Add(parent);
- }
- Node root = tree[0];
- Node.Print(ref root);
- Node.Build(ref root);
- List<int> path = new List<int>();
- Node.dfs(root, path);
- }
- public static SortedDictionary<string, char> table = new SortedDictionary<string, char>();
- public class Node
- {
- public int inf;
- public Node left;
- public Node right;
- char c;
- int code = -1;
- public static void dfs(Node root, List<int> path)
- {
- if (root.c != '~')
- {
- StringBuilder code = new StringBuilder(path.Count + 1);
- Console.Write($"{root.c} = ");
- for (int i = 0; i < path.Count; ++i)
- {
- Console.Write(path[i]);
- code.Append(path[i]);
- }
- Console.WriteLine(root.code);
- code.Append(root.code);
- table.Add(code.ToString(), root.c);
- }
- if (root.code != -1)
- path.Add(root.code);
- if (root.left != null)
- dfs(root.left, path);
- if (root.right != null)
- dfs(root.right, path);
- if (path.Count > 0)
- path.RemoveAt(path.Count - 1);
- }
- static public void Print(ref Node root, int depth = 0)
- {
- if (root == null)
- return;
- if (root.c != '~')
- {
- for (int i = 0; i < depth; i++)
- Console.Write(".");
- Console.WriteLine(root.c);
- }
- else depth++;
- Print(ref root.left, depth);
- Print(ref root.right, depth);
- }
- public Node(int nodeInf, char nodeChar)
- {
- inf = nodeInf;
- c = nodeChar;
- left = null;
- right = null;
- }
- public Node() { left = right = null; }
- public Node(ref Node L, ref Node R)
- {
- left = L;
- right = R;
- inf = L.inf + R.inf;
- c = '~';
- }
- public static void Build(ref Node root)
- {
- if (root.left != null)
- {
- root.left.code = 0;
- Build(ref root.left);
- }
- if (root.right != null)
- {
- root.right.code = 1;
- Build(ref root.right);
- }
- }
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- string s = "abbccc";
- BinaryTree tree = new BinaryTree();
- tree.Solve(s);
- string rev_s = "101111000";
- StringBuilder t = new StringBuilder();
- ICollection<string> a = BinaryTree.table.Keys;
- for (int i = 0; i < rev_s.Length; i++)
- {
- t.Append(rev_s[i]);
- if (a.Contains(t.ToString()))
- {
- Console.Write(BinaryTree.table[t.ToString()]);
- t.Clear();
- }
- }
- Console.WriteLine();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement