广度和广度深度第一次导线实施

时间:2017-02-08 15:14:31

标签: c#

我真的很难实现这两种方法。我已经得到了代码,只是努力将其付诸实践。 ref给了我一个问题,我最后一次使用PreOrderPostOrderInOrder,但我使用了string buffer。我怎样才能使这段代码起作用 -

这是两种方法

        public void DepthFirstTraverse(T startID, ref List<GraphNode<T>> visited)
        {
            LinkedList<T> adj;
            Stack<T> toVisit = new Stack<T>();
            GraphNode<T> current;
            toVisit.Push(startID); //push the first id onto the stack

            while (toVisit.Count != 0)
            {
                current = GetNodeByID(toVisit.Peek());
                adj = current.GetAdjList();
                visited.Add(current);

                foreach (T type in adj)
                {
                    if (!toVisit.Contains(type) && !visited.Contains(GetNodeByID(type)))
                    {
                        toVisit.Push(type);
                    }
                }
            }
        }
        public void BreadthFirstTraverse(T startID, ref List<GraphNode<T>> visited)
        {
            LinkedList<T> adj;
            Queue<T> toVisit = new Queue<T>();
            GraphNode<T> current;
            toVisit.Enqueue(startID);

            while (toVisit.Count != 0)
            {
                //get it off from the list
                T currentID = toVisit.Dequeue();
                current = GetNodeByID(currentID);
                adj = current.GetAdjList();
                //add the current to the visited list, so we know where we have been
                visited.Add(current);
                foreach (T ID in adj)
                {
                    if (!toVisit.Contains(ID) && !visited.Contains(GetNodeByID(ID)))
                    {
                        toVisit.Enqueue(ID);
                    }
                }
            }
        }

这是我未能获得输出 -

   Console.WriteLine(string.Join, ',', myGraph.BreadthFirstTraverse(myGraph, ref 'a'));

我一直收到此错误消息 -

ref或out参数必须是可赋值变量

我所有的其他方法都很完美; IsEmptyGraph()ContainsGraph(GraphNode<T> node)IsAdjacent(GraphNode<T> from, GraphNode<T> to)AddNode(T id)GraphNode<T> GetNodeByID(T id)AddEdge(T from, T to)。真的很想知道如何让它发挥作用。

请有人帮忙解释一下。

我有代码,它只是执行它。

编辑 - 我只是不知道要传递给参数的内容。

1 个答案:

答案 0 :(得分:0)

myGraph.BreadthFirstTraverse(myGraph, ref 'a')
  

ref或out参数必须是可赋值变量

您不能将常量作为ref参数传递。如果你必须这样做,那就先创建它。

char youCanPassMeAsRef ='a';
myGraph.BreadthFirstTraverse(myGraph, ref youCanPassMeAsRef)
相关问题