예제 #1
0
        public static void Sum(Id i1, Id i2)
        {
            // this becomes the sum between i1 and i2

            //sum(0, X) -> X;
            //sum(X, 0) -> X;
            //sum({L1,R1}, {L2, R2}) -> norm_id({sum(L1, L2), sum(R1, R2)}).

            if (i1.IsLeaf
                && i1.Value == 0)
            {
                i1.Copy(i2);
                return;
            }

            if (i2.IsLeaf
                && i2.Value == 0)
            {
                return;
                //i1 is the result
            }

            if (!i1.IsLeaf
                && !i2.IsLeaf)
            {
                Sum(i1.Left, i2.Left);
                Sum(i1.Right, i2.Right);
                i1.Normalize();
                return;
            }

            throw new IdOperationException("Sum failed", i1, i2);
        }