public HTNode(IHTNode right = null, IHTNode left = null, HTNode parent = null) { _Weight = 0; _Right = right; _Left = left; _Parent = parent; if (_Right != null) { _Right._Parent = this; } if (_Left != null) { _Left._Parent = this; } //if (_Parent == null) // code = ""; //else //{ // if (_Parent._Left == this) // code = "1" + _Parent.code; // else // code = "0" + _Parent.code; //} nodes.Add(this); }
static void ExchangeLeaf(HTLeaf l1, IHTNode l2) { object temp; temp = l1._Parent; l1._Parent = l2._Parent; l2._Parent = (HTNode)temp; temp = l1.code; }
public string Decode(string input) { string ret = ""; string temp = input.Substring(0, 12); input = input.Substring(12); char sim = (char)Convert.ToInt32(temp, 2); ret += sim; Add(sim); Search(sim).Increment(); UpdateNodes(); while (input != "") { IHTNode[] NodesAr = nodes.ToArray(); for (int i = 0; i < NodesAr.Length; i++) { IHTNode node = NodesAr[i]; if (!(node is HTNode)) { if (input.Length >= node.code.Length) { if (input.Substring(0, node.code.Length) == node.code) { input = input.Substring(node.code.Length); if (node is HTLeaf) { sim = (node as HTLeaf).sim; ret += sim; Search(sim).Increment(); } if (node is EscapeNode) { temp = input.Substring(0, 12); input = input.Substring(12); sim = (char)Convert.ToInt32(temp, 2); ret += sim; Add(sim); Search(sim).Increment(); continue; } UpdateNodes(); } } } } } return(ret); }
public void Increment() { IHTNode active = this; while (active != null) { active._Weight++; active = active._Parent; } IHTNode target; do { target = HTree.nodes.Find(l => (l as IHTNode).code.Length < code.Length && l._Weight < _Weight); if (target != null) { ExchangeLeaf(this, target); } } while (target != null); }