Esempio n. 1
0
        public static void FindLadderLength(string source, string dest, HashSet <string> dict)
        {
            var que = new Utils.Queue <string>();

            que.EnQueue(source);

            while (!que.IsEmpty())
            {
                var temp = que.DeQueue();
                Console.WriteLine(temp);

                foreach (var item in dict)
                {
                    if (IsOneLetterAway(temp, item))
                    {
                        que.EnQueue(item);
                    }
                }

                if (dict.Contains(temp))   //Like visited checking
                {
                    dict.Remove(temp);
                }
            }
        }
Esempio n. 2
0
        public bool Route(Graph <int> grph, GraphNode <int> start, GraphNode <int> end)
        {
            if (grph == null || start == null || end == null)
            {
                return(false);
            }
            var que = new Utils.Queue <GraphNode <int> >();

            que.EnQueue(start);
            start.Visited = true;

            while (!que.IsEmpty())
            {
                var temp = que.DeQueue();
                foreach (var item in temp.adjacent)
                {
                    if (item.Visited == false)
                    {
                        if (item.data == end.data)
                        {
                            return(true);
                        }
                        item.Visited = true;
                        que.EnQueue(item);
                    }
                }
            }

            return(false);
        }
Esempio n. 3
0
        public static void BFSTraverse <T>(TreeNode <T> node) //Level Order Traversal
        {
            if (node == null)
            {
                return;
            }
            var queu = new Utils.Queue <TreeNode <T> >();

            queu.EnQueue(node);

            while (!queu.IsEmpty())
            {
                var temp = queu.DeQueue();
                Console.WriteLine(temp.data);
                temp.Visited = true;
                if (temp.left != null)
                {
                    if (!temp.left.Visited)
                    {
                        temp.left.Visited = true;
                        queu.EnQueue(temp.left);
                    }
                }
                if (temp.right != null)
                {
                    if (!temp.right.Visited)
                    {
                        temp.right.Visited = true;
                        queu.EnQueue(temp.right);
                    }
                }
            }
        }