Ejemplo n.º 1
0
        public int[] encode(TNode tree)
        {
            int n = TNode.CountVertices(tree);

            if (n <= 2)
            {
                return(null);
            }

            TNode treeToDecode = tree;

            int[] result = new int[n - 2];
            for (int x = 0; x < n - 2; x++)
            {
                TNode lowestNode      = treeToDecode.FindLowestLeaf();
                TNode nearestNeighbor = lowestNode.Parent == null ? lowestNode.Children[0] : lowestNode.Parent;
                result[x] = (int)nearestNeighbor.label;

                if (nearestNeighbor.Parent == lowestNode)
                {
                    nearestNeighbor.Parent = null;
                }
                else
                {
                    nearestNeighbor.RemoveChild(lowestNode);
                }
                if (lowestNode.Parent != null)
                {
                    lowestNode.Parent.RemoveChild(lowestNode);
                }
                else if (lowestNode == treeToDecode)
                {
                    treeToDecode = treeToDecode.Children[0];
                }
            }
            return(result);
        }