Exemple #1
0
        private void Bfs(int startIndex, ref int clock)
        {
            Queue <Int32> queue = QueueListImpl <Int32> .QueueFacotry();

            Status(startIndex, VStatus.Discovered);
            queue.Enqueue(startIndex);
            DTime(startIndex, ++clock);
            while (queue.Size != 0)
            {
                int v = queue.Dequeue();
                for (int u = FirstNbr(v); u > -1; u = NextNbr(v, u))
                {
                    if (Status(u) == VStatus.Undiscovered)
                    {
                        Status(u, VStatus.Discovered);
                        Status(v, u, EStatus.Tree);
                        queue.Enqueue(u);
                        Parent(u, v);
                    }
                    else
                    {
                        Status(v, u, EStatus.Cross);
                    }
                }
                FTime(v, ++clock);
                Status(v, VStatus.Visited);
            }
        }
Exemple #2
0
        /// <summary>
        /// 层次遍历
        /// </summary>
        /// <param name="action">the action</param>
        public void TravLevel(Action <T> action)
        {
            Queue <BinNode <T> > q = QueueListImpl <BinNode <T> > .QueueFacotry();

            q.Enqueue(this);
            while (q.Size != 0)
            {
                var x = q.Dequeue();
                action(x.Data);
                if (x.HasLChild)
                {
                    q.Enqueue(x.LChild);
                }
                if (x.HasRChild)
                {
                    q.Enqueue(x.RChild);
                }
            }
        }