Example #1
0
        public void TravIn_I2(BinNode <T> x, Action <BinNode <T> > action)
        {
            Stack <BinNode <T> > s = StackVectorImpl <BinNode <T> > .StackFactory();

            while (true)
            {
                if (x != null)
                {
                    s.Push(x);
                    x = x.LChild;
                }
                else
                {
                    if (s.Size != 0)
                    {
                        x = s.Pop();
                        action(x);
                        x = x.RChild;
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }
Example #2
0
        public static IList <string> PalceQueeu(int n)
        {
            Stack <Queen> solu = StackVectorImpl <Queen> .StackFactory();

            IList <String> solutions = List <String> .ListFactory();

            Queen q = new Queen(0, 0);

            do
            {
                if (n <= solu.Size || n <= q.Y)
                {
                    q = solu.Pop();
                    q.Y++;
                }
                else
                {
                    while (q.Y < n && 0 <= solu.Find(q))
                    {
                        q.Y++;
                    }
                    if (q.Y < n)
                    {
                        solu.Push(q);
                        if (n <= solu.Size)
                        {
                            solutions.InsertAsLast(PrintSolution(solu));
                        }
                        q = new Queen(q.X + 1, 0);
                    }
                }
            } while (0 < q.X || q.Y < n);
            return(solutions);
        }
Example #3
0
        public static void Caluc(string expression)
        {
            StringBuilder rpn  = new StringBuilder();
            Stack <int>   opnd = StackVectorImpl <int> .StackFactory();

            Stack <Char> optr = StackVectorImpl <Char> .StackFactory();

            optr.Push('\0');
            int counter = 0;

            while (!optr.Empty)
            {
                // if(counter>expression.Length) break;
                if (counter < expression.Length && Isdigit(expression[counter]))
                {
                    ReadNumber(ref counter, expression, opnd);
                    rpn.Append(opnd.Top);
                }
                else
                {
                    char compareOp = counter == expression.Length ? '\0' : expression[counter];
                    switch (OrderBetween(optr.Top, compareOp))
                    {
                    case '<':
                        optr.Push(expression[counter]);
                        counter++;
                        break;

                    case '=':
                        optr.Pop();
                        counter++;
                        break;

                    case '>':
                        char op = optr.Pop();
                        rpn.Append(op);
                        if ('!' == op)
                        {
                            int pOnd = opnd.Pop();
                            opnd.Push(Caluc(pOnd));
                        }
                        else
                        {
                            int pOnd2 = opnd.Pop();
                            int pOnd1 = opnd.Pop();
                            opnd.Push(Caluc(pOnd1, op, pOnd2));
                        }
                        break;
                    }
                }
            }
            Reuslt = opnd.Pop();
            Rpn    = rpn.ToString();
        }
Example #4
0
        /// <summary>
        /// 迭代先序遍历
        /// </summary>
        /// <param name="x">开始根节点</param>
        /// <param name="action">操作</param>
        public void TravPre_I2(BinNode <T> x, Action <BinNode <T> > action)
        {
            Stack <BinNode <T> > s = StackVectorImpl <BinNode <T> > .StackFactory();

            while (true)
            {
                VisitAlongLeftBranch(x, action, s);
                if (s.Size == 0)
                {
                    break;
                }
                x = s.Pop();
            }
        }
Example #5
0
        /// <summary>
        /// 中序遍历的迭代算法
        /// </summary>
        /// <param name="x"></param>
        /// <param name="action"></param>
        public void TravIn_I1(BinNode <T> x, Action <BinNode <T> > action)
        {
            var s = StackVectorImpl <BinNode <T> > .StackFactory();

            while (true)
            {
                GoAlongLeftBranch(x, s);
                if (s.Size == 0)
                {
                    break;
                }
                x = s.Pop();
                action(x);
                x = x.RChild;
            }
        }
Example #6
0
        public void TravPost_I(BinNode <T> x, Action <BinNode <T> > action)
        {
            Stack <BinNode <T> > s = StackVectorImpl <BinNode <T> > .StackFactory();

            if (x != null)
            {
                s.Push(x);
            }
            while (s.Size != 0)
            {
                if (s.Top == x.Parent)
                {
                    GotoHlvfl(s);
                }
                x = s.Pop();
                action(x);
            }
        }
Example #7
0
        /// <summary>
        /// 迭代版先序遍历
        /// </summary>
        /// <param name="x">开始节点</param>
        /// <param name="action">遍历操作,委托</param>
        public void TravePre_I1(BinNode <T> x, Action <BinNode <T> > action)
        {
            var s = StackVectorImpl <BinNode <T> > .StackFactory();

            if (x != null)
            {
                s.Push(x);
            }
            while (s.Size != 0)
            {
                x = s.Pop();
                action(x);
                if (x.HasRChild)
                {
                    s.Push(x.RChild);
                }
                if (x.HasLChild)
                {
                    s.Push(x.LChild);
                }
            }
        }
Example #8
0
        /// <summary>
        /// 拓扑排序
        /// </summary>
        /// <param name="s"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        public Stack <TV> TopoSort(int s)
        {
            Reset();
            int        clock = 0;
            int        v     = s;
            Stack <TV> stack = StackVectorImpl <TV> .StackFactory();

            do
            {
                if (VStatus.Undiscovered == Status(v))
                {
                    if (!TopoSort(v, ref clock, stack))
                    {
                        while (stack.Size != 0)
                        {
                            stack.Pop();
                        }
                        break;
                    }
                }
            } while (s != (v = (++v % N)));
            return(stack);
        }