Ejemplo n.º 1
0
        //层序遍历
        public void LevelNode(Node <T> root)
        {
            //根节点为空
            if (root == null)
            {
                return;
            }

            //设置一个队列保存层序遍历的节点
            CSeqQueue1 <Node <T> > sq = new CSeqQueue1 <Node <T> >(50);

            //根节点入队
            sq.In(root);

            //队列非空,节点没有处理完
            while (!sq.IsEmpty())
            {
                Node <T> temp = sq.GetOut();

                //处理当前节点
                Console.WriteLine(temp.Data);

                if (temp.LChild != null)
                {
                    sq.In(temp.LChild);
                }

                if (temp.RChild != null)
                {
                    sq.In(temp.RChild);
                }
            }
        }
Ejemplo n.º 2
0
        //从某个顶点出发进行广度优先遍历
        public void BFSAL(int i)
        {
            visited[i] = 1;
            CSeqQueue1 <int> cq = new CSeqQueue1 <int>(visited.Length);

            cq.In(i);
            while (!cq.IsEmpty())
            {
                int             k = cq.GetOut();
                adjListNode <T> p = adjList[k].FirstAdj;

                while (p != null)
                {
                    if (visited[p.Adjvex] == 0)
                    {
                        visited[p.Adjvex] = 1;
                        cq.In(p.Adjvex);
                    }
                    p = p.Next;
                }
            }
        }