示例#1
0
        public int Search <T>(BfsNode.Node <T> root, T elementToFind)
        {
            var dist  = new Dictionary <T, int>();
            var queue = new Queue <BfsNode.Node <T> >();

            root.Marked = true;
            queue.Enqueue(root);
            dist.Add(root.Name, 0);

            while (queue.Count > 0)
            {
                var elem = queue.Dequeue();

                if (elem.Name.Equals(elementToFind))
                {
                    return(dist[elem.Name]);
                }

                foreach (BfsNode.Node <T> n in elem.Children)
                {
                    if (!n.Marked)
                    {
                        n.Marked = true;
                        queue.Enqueue(n);
                        if (!dist.ContainsKey(n.Name))
                        {
                            dist.Add(n.Name, dist[elem.Name] + 1);
                        }
                    }
                }
            }

            return(-1);
        }
        public BfsNode.Node <T> Search <T>(BfsNode.Node <T> root, T elementToFind)
        {
            var queue = new Queue <BfsNode.Node <T> >();

            root.Marked = true;
            queue.Enqueue(root);

            while (queue.Count > 0)
            {
                var elem = queue.Dequeue();
                if (elem.Name.Equals(elementToFind))
                {
                    return(elem);
                }

                foreach (BfsNode.Node <T> n in elem.Children)
                {
                    if (!n.Marked)
                    {
                        n.Marked = true;
                        queue.Enqueue(n);
                    }
                }
            }

            return(null);
        }