Exemplo n.º 1
0
        //Itterative DFS traversa
        public void TraverseDFSWithStack()
        {
            //Set Stack Last in Last Out
            Stack <TreeNode <T> > stack = new Stack <TreeNode <T> >();

            //Put Root
            stack.Push(this.root);
            while (stack.Count > 0)
            {
                //Remove Last element that was added. First Cycle this means root;
                TreeNode <T> currentNode = stack.Pop();
                //Returnt/Write Result
                Console.Write("{0} ", currentNode.Value);
                //Add all children
                for (int i = 0; i < currentNode.ChildrenCount; i++)
                {
                    TreeNode <T> childNode = currentNode.GetChild(i);
                    stack.Push(childNode);
                }

                //First Cycle - Last element added (the one that is poped) is root. It is displayed. All its children are added to the stack.
                //Second Cycle - Last element added is the last child of root. It is displayed and all its children are added.
                //Trid cycl - Last element added is the previously pooped element(last child of the last child of root) and so on. Untill stack is empty.
            }
        }
Exemplo n.º 2
0
        public void TraverseDFSWithStack()
        {
            Stack <TreeNode <T> > stack = new Stack <TreeNode <T> >();

            stack.Push(this.root);
            while (stack.Count > 0)
            {
                TreeNode <T> currentNode = stack.Pop();
                Console.Write("{0} ", currentNode.Value);
                for (int i = 0; i < currentNode.ChildrenCount; i++)
                {
                    TreeNode <T> childNode = currentNode.GetChild(i);
                    stack.Push(childNode);
                }
            }
        }
Exemplo n.º 3
0
        public void TraverseBFS()
        {
            Queue <TreeNode <T> > queue = new Queue <TreeNode <T> >();

            queue.Enqueue(this.root);
            while (queue.Count > 0)
            {
                TreeNode <T> currentNode = queue.Dequeue();
                Console.Write("{0} ", currentNode.Value);
                for (int i = 0; i < currentNode.ChildrenCount; i++)
                {
                    TreeNode <T> childNode = currentNode.GetChild(i);
                    queue.Enqueue(childNode);
                }
            }
        }
Exemplo n.º 4
0
        private void PrintDFS(TreeNode <T> node, string spaces)
        {
            if (Root == null)
            {
                return;
            }
            Console.WriteLine(spaces + node.Value);

            TreeNode <T> child = null;

            for (int i = 0; i < node.ChildrenCount; i++)
            {
                child = node.GetChild(i);
                PrintDFS(child, spaces + "   ");
            }
        }
Exemplo n.º 5
0
        private void TraverseDFS(TreeNode <T> root, string spaces)
        {
            if (this.root == null)
            {
                return;
            }

            Console.WriteLine(spaces + root.Value);

            TreeNode <T> child = null;

            for (int i = 0; i < root.ChildrenCount; i++)
            {
                child = root.GetChild(i);
                TraverseDFS(child, spaces + "   ");
            }
        }
Exemplo n.º 6
0
        //recursive dfs
        private void TraverseDFS(TreeNode <T> root, string spaces)
        {
            //bottom comes when method tries to travers the children of a node with no children
            if (root == null)
            {
                return;
            }

            //writes out values
            Console.WriteLine(spaces + root.Value);

            //get children, which form subtrees and repeat the algorithm for them
            TreeNode <T> child = null;

            for (int i = 0; i < root.ChildrenCount; i++)
            {
                child = root.GetChild(i);
                this.TraverseDFS(child, spaces + "   ");
            }
            //when algorithm is reaped for all children then this subtree is traverse
            //So when we get to a node with no children the method will simply return, go up one step and continues with the next child
        }
Exemplo n.º 7
0
        //BFS with a queue
        public void TraverseBFS()
        {
            //set up queue, First in First Out
            Queue <TreeNode <T> > queue = new Queue <TreeNode <T> >();

            //Add the root
            queue.Enqueue(this.root);
            while (queue.Count > 0)
            {
                //Detach the node in first position, which for first cycle is the root
                TreeNode <T> currentNode = queue.Dequeue();
                //Write value
                Console.Write("{0} ", currentNode.Value);
                //Add all cildren
                for (int i = 0; i < currentNode.ChildrenCount; i++)
                {
                    TreeNode <T> childNode = currentNode.GetChild(i);
                    queue.Enqueue(childNode);
                }
                //In the next cycle the first child will be teh first element. All its children will be added.
                //Third cycle second child will be first element and then its children wiil be added to the back of the queue
            }
        }