MakeContributions/DSA

<depth first search]--> in <!--[JAVA-->

karthiknadar1204 opened this issue · 4 comments

Information about Algorithm

The given algorithm implements stack data structure.

(Type here)

Have you read the Contributing.md and Code of conduct

  • [0] Yes
  • No

Other context

(Type here)
import java.util.List;
import java.util.ArrayList;
import java.util.Stack;

public class DepthFirstSearch {

private static class Node {
    int data;
    List<Node> children = new ArrayList<>();

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

public static void dfs(Node root) {
    Stack<Node> stack = new Stack<>();
    stack.push(root);

    while (!stack.isEmpty()) {
        Node current = stack.pop();
        System.out.println(current.data);

        for (int i = current.children.size() - 1; i >= 0; i--) {
            stack.push(current.children.get(i));
        }
    }
}

public static void main(String[] args) {
    Node a = new Node(1);
    Node b = new Node(2);
    Node c = new Node(3);
    Node d = new Node(4);
    Node e = new Node(5);
    Node f = new Node(6);

    a.children.add(b);
    a.children.add(c);
    b.children.add(d);
    c.children.add(e);
    d.children.add(f);

    dfs(a);
}

}

Thanks for opening your first issue here! Be sure to follow the issue template!

I can implement DFS in java. Please assign me this issue

i would like to take this issue, could you assign me ?

i have already contributed to this issue,waiting for it to be reviewed