コード例 #1
0
        protected static int StampGrow(Id i, Event e)
        {
            int cost;

            if (i.IsLeaf
                && i.Value == 1
                && e.IsLeaf)
            {
                e.SetValue(e.Value + 1);
                return 0;
            }

            if (e.IsLeaf)
            {
                e.SetAsNode();
                cost = StampGrow(i, e);
                return cost + 1000;
            }

            if (i.IsLeaf == false
                && i.Left.IsLeaf
                && i.Left.Value == 0)
            {
                cost = StampGrow(i.Right, e.Right);
                return cost + 1;
            }

            if (i.IsLeaf == false
                && i.Right.IsLeaf
                && i.Right.Value == 0)
            {
                cost = StampGrow(i.Left, e.Left);
                return cost + 1;
            }

            if (i.IsLeaf == false)
            {
                var el = Event.Clone(e.Left);
                var er = Event.Clone(e.Right);
                var costr = StampGrow(i.Right, e.Right);
                var costl = StampGrow(i.Left, e.Left);
                if (costl < costr)
                {
                    e.SetRight(er);
                    return costl + 1;
                }

                e.SetLeft(el);
                return costr + 1;
            }

            throw new StampOperationException(string.Format("Grow failed: id: {0}, event: {1}", i, e));
        }