Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Depth first travesal java I need a code please that follows this diagram and If

ID: 3810333 • Letter: D

Question

Depth first travesal java

I need a code please that follows this diagram and If possible could you please comment what each line does.

***Screenshot code or paste it please***

Step Traversal Description Initialize the stack. Stack Mark S as visited and put it onto the stack. Explore any unvisited adjacent node from S. We have three nodes and we can pick any of them. For this example, we shall take the node in an alphabetical order. top Stack Mark A as visited and put it onto the stack. Explore any unvisited adjacent node from A. Both S and D are adjacent to A but we are concerned for unvisited nodes only. top A Stack Visit D and mark it as visited and put onto the stack. Here, we have B and C nodes, which are adjacent to D and both are c top D unvisited. However, we shall again choose in an alphabetical order. Stack

Explanation / Answer

import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.Stack;

public class DepthFirstTravesal {
   public void DFS(Node root) {
       Stack<Node> s = new Stack<Node>();
       s.add(root);
       while (s.isEmpty() == false)
{
           Node x = s.pop();
           if(x.r!=null) s.add(x.r);
           if(x.l!=null) s.add(x.l);          
           System.out.print(" " + x.data);
       }
   }
   public static void main(String args[]){
       Node root = new Node(1);
       root.l = new Node(2);
       root.l.l = new Node(4);
       root.l.r = new Node(5);
       root.r = new Node(3);
       root.r.l = new Node(6);
       root.r.r = new Node(7);
      
       DepthFirstTravesal d = new DepthFirstTravesal();
       System.out.println("Depth-First-Search : ");
       d.DFS(root);
   }
}

class Node {
   int data;
   Node l;
   Node r;

   public Node(int data) {
       this.data = data;
       l = null;
       r = null;
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote