コード例 #1
0
        public List <BinaryTreeNode> LevelOrder()
        {
            if (_head == null)
            {
                return(null);
            }

            Queue961 <BinaryTreeNode> queue    = new Queue961 <BinaryTreeNode>();
            List <BinaryTreeNode>     sequence = new List <BinaryTreeNode>();

            queue.enqueue(_head);

            while (!queue.isEmpty())
            {
                var node = queue.dequeue();
                if (node.leftChildNode != null)
                {
                    queue.enqueue(node.leftChildNode);
                }
                if (node.rightChildNode != null)
                {
                    queue.enqueue(node.rightChildNode);
                }
                sequence.Add(node);
            }

            return(sequence);
        }
コード例 #2
0
        protected List <GraphNode> BFS(int nodeidx, int[] visitedflag)
        {
            if (nodeidx < 0 || nodeidx >= NodesCount || visitedflag[nodeidx] == 1)
            {
                return(null);
            }

            Queue961 <int> queue  = new Queue961 <int>();
            int            curidx = nodeidx;

            queue.enqueue(curidx);

            List <GraphNode> result = new List <GraphNode>();

            while (!queue.isEmpty())
            {
                curidx = queue.dequeue();
                if (visitedflag[curidx] == 1)
                {
                    continue;
                }
                visitedflag[curidx] = 1;
                result.Add(_nodesarray[curidx]);
                List <int> neighbours = getNeighbourNodes(curidx);

                for (int i = 0; i < neighbours.Count; i++)
                {
                    if (visitedflag[neighbours[i]] == 0)
                    {
                        queue.enqueue(neighbours[i]);
                    }
                }
            }

            return(result);
        }
コード例 #3
0
        public override List <GraphEdge> MiniDistance_BSF(int startidx, int destidx)
        {
            if (startidx < 0 && startidx >= NodesCount)
            {
                return(new List <GraphEdge>());
            }

            if (destidx < 0 && destidx >= NodesCount)
            {
                return(new List <GraphEdge>());
            }

            if (startidx == destidx)
            {
                return(new List <GraphEdge>());
            }

            Queue.Queue961 <int> queue = new Queue961 <int>();
            queue.enqueue(startidx);

            int[] visitednodes    = new int[NodesCount];
            int[] parentpathnodes = new int[NodesCount];
            int[] nodedistance    = new int[NodesCount];

            for (int i = 0; i < NodesCount; i++)
            {
                nodedistance[i]    = int.MaxValue;
                visitednodes[i]    = 0;
                parentpathnodes[i] = -1;
            }

            visitednodes[startidx] = 1;
            nodedistance[startidx] = 0;

            while (!queue.isEmpty())
            {
                var curidx          = queue.dequeue();
                var neighbourenodes = getNeighbourNodes(curidx);
                for (int i = 0; i < neighbourenodes.Count; i++)
                {
                    int nodeidx = neighbourenodes[i];
                    if (visitednodes[nodeidx] == 0)
                    {
                        visitednodes[nodeidx]    = 1; //已访问
                        nodedistance[nodeidx]    = nodedistance[curidx] + 1;
                        parentpathnodes[nodeidx] = curidx;

                        if (nodeidx == destidx) //找到跳出循环
                        {
                            queue.clear();
                            break;
                        }

                        queue.enqueue(nodeidx);
                    }
                }
            }


            List <GraphEdge> edges = new List <GraphEdge>();
            int pathendidx         = destidx;

            while (parentpathnodes[pathendidx] >= 0)
            {
                GraphEdge edge = new GraphEdge {
                    Weights = 1, StartNode = _nodesarray[parentpathnodes[pathendidx]], EndNode = _nodesarray[pathendidx]
                };
                pathendidx = parentpathnodes[pathendidx];
                edges.Insert(0, edge);
            }

            return(edges);
        }
コード例 #4
0
        static void testqueue()
        {
            int maxvals = 200;

            int[] vals = new int[maxvals];
            for (int i = 1; i < maxvals; i++)
            {
                vals[i] = i;
            }

            Queue961 <int> queue = new Queue961 <int>();

            //测试插入直至栈满
            Console.WriteLine("开始测试压队列:");
            for (int i = 1; i < maxvals; i++)
            {
                bool bsuccess = queue.enqueue(vals[i]);
                if (bsuccess)
                {
                    printQueueTop(queue);
                }
                else
                {
                    Console.WriteLine("");
                    Console.WriteLine("压栈失败: 当前压队列数{0},是否队列满{1},当前队列内元素数{2}", i, queue.isFull(), queue.Count());
                    break;
                }
            }

            //测试弹出直至栈空
            Console.WriteLine("开始测试出队列:");
            for (int i = 1; i < maxvals / 4; i++)
            {
                printQueueTop(queue);
                int?val = queue.dequeue();


                if (val == null)
                {
                    Console.WriteLine("");
                    Console.WriteLine("弹栈失败: 当前弹队列数{0},是否队列空{1},当前队列内元素数{2}", i, queue.isEmpty(), queue.Count());
                    break;
                }
            }

            //测试插入直至栈满
            Console.WriteLine("开始测试压队列:");
            for (int i = 1; i < maxvals; i++)
            {
                bool bsuccess = queue.enqueue(vals[i]);
                if (bsuccess)
                {
                    printQueueTop(queue);
                }
                else
                {
                    Console.WriteLine("");
                    Console.WriteLine("压栈失败: 当前压队列数{0},是否队列满{1},当前队列内元素数{2}", i, queue.isFull(), queue.Count());
                    break;
                }
            }
        }