Ejemplo n.º 1
0
        /// <summary>
        /// 层序遍历(Level Order)
        /// </summary>
        public void LevelOrder(BiNode <T> root)
        {
            if (root == null)
            {
                return;
            }

            CSeqQueue <BiNode <T> > sq = new CSeqQueue <BiNode <T> >(100);

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

            while (!sq.IsEmpty())
            {
                BiNode <T> tmp = sq.Out();

                Console.WriteLine("{0}", tmp.Data);

                if (tmp.LeftChild != null)
                {
                    sq.In(tmp.LeftChild);
                }

                if (tmp.RightChild != null)
                {
                    sq.In(tmp.RightChild);
                }
            }
        }
Ejemplo n.º 2
0
        public IEnumerable <VexNode <T> > BreadthFirst(int index)
        {
            _visited[index] = 1;
            yield return(this[index]);

            var cq = new CSeqQueue <int>(_visited.Length);

            cq.In(index);
            while (!cq.IsEmpty())
            {
                int k = cq.Out();
                var p = AdjList[k].FirstAdj;
                while (p != null)
                {
                    if (_visited[p.AdjVex] == 0)
                    {
                        _visited[p.AdjVex] = 1;
                        yield return(this[p.AdjVex]);

                        cq.In(p.AdjVex);
                    }
                    p = p.Next;
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 层序遍历
        /// </summary>
        /// <param name="root"></param>
        public void LevelOrder(Node <T> root)
        {
            if (root == null)
            {
                return;
            }

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

            //根结点入队
            sq.In(root);
            //队列非空,结点没有处理完
            while (!sq.IsEmpty())
            {
                //结点出队
                Node <T> tmp = sq.Out();
                //处理当前结点
                Console.WriteLine("{0}", tmp);
                //将当前结点的左孩子结点入队
                if (tmp.LChild != null)
                {
                    sq.In(tmp.LChild);
                }
                //将当前结点的右孩子结点入队
                if (tmp.RChild != null)
                {
                    sq.In(tmp.RChild);
                }
            }
        }
Ejemplo n.º 4
0
        /*
         * 【例3-4】编程判断一个字符串是否是回文。
         * 回文是指一个字符序列以中间字符为基准两边字符完全相同,
         * 如字符序列"ACBDEDBCA"是回文。
         * 算法思想:判断一个字符序列是否是回文,就是把第一个字符与最后一个字符相比较,
         * 第二个字符与倒数第二个字符比较,依次类推,第i个字符与第n-i个字符比较。
         * 如果每次比较都相等,则为回文,如果某次比较不相等,就不是回文。
         * 因此,可以把字符序列分别入队列和栈,
         * 然后逐个出队列和出栈并比较出队列的字符和出栈的字符是否相等,
         * 若全部相等则该字符序列就是回文,否则就不是回文。
         */
        public static void palindrome()
        {
            SeqStack <char>  s   = new SeqStack <char>(50);
            CSeqQueue <char> q   = new CSeqQueue <char>(50);
            string           str = Console.ReadLine(); //手动输入字符串

            for (int i = 0; i < str.Length; ++i)       //循环入
            {
                s.Push(str[i]);
                q.In(str[i]);
            }
            while (!s.IsEmpty() && !q.IsEmpty())//循环出
            {
                if (s.Pop() != q.Out())
                {
                    break;
                }
            }
            if (!s.IsEmpty() || !q.IsEmpty())//非空
            {
                Console.WriteLine("这不是回文!");
            }
            else//空
            {
                Console.WriteLine("这是回文!");
            }
        } //public static void Main()
Ejemplo n.º 5
0
        public bool IsCBT(BiNode <T> root)
        {
            bool flag = false;

            if (root == null)
            {
                return(false);
            }
            CSeqQueue <BiNode <T> > sq = new CSeqQueue <BiNode <T> >(100);

            sq.In(root); // 根节点入队
            while (!sq.IsEmpty())
            {
                BiNode <T> p = sq.Out();
                if (flag)
                {
                    if (p.LeftChild != null || p.RightChild != null)
                    {
                        return(false);
                    }
                }
                else
                {
                    if (p.LeftChild != null && p.RightChild != null)
                    {
                        sq.In(p.LeftChild);
                        sq.In(p.RightChild);
                    }
                    else if (p.RightChild != null) // 只有右结点
                    {
                        return(false);
                    }
                    else if (p.LeftChild != null) // 只有左结点
                    {
                        sq.In(p.LeftChild);
                        flag = true;
                    }
                    else // 没有结点
                    {
                        flag = true;
                    }
                }
            }
            return(true);
        }
Ejemplo n.º 6
0
        //从某个顶点出发进行广度优先遍历
        public void BFSAL(int i)
        {
            visited[i] = 1;
            CSeqQueue <int> cq = new CSeqQueue <int>(visited.Length);

            cq.In(i);

            while (!cq.IsEmpty())
            {
                int             k = cq.Out();
                AdjListNode <T> p = adjList[k].FirstAdj;

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