Skip to content

Instantly share code, notes, and snippets.

@gennad
Created January 23, 2011 09:14

Revisions

  1. gennad revised this gist Jan 23, 2011. 1 changed file with 38 additions and 40 deletions.
    78 changes: 38 additions & 40 deletions BFSDFS.java
    Original file line number Diff line number Diff line change
    @@ -1,53 +1,51 @@
    Class Main {

    public void bfs()
    {
    // BFS uses Queue data structure
    Queue queue = new LinkedList();
    queue.add(this.rootNode);
    printNode(this.rootNode);
    rootNode.visited = true;
    while(!queue.isEmpty()) {
    Node node = (Node)queue.remove();
    Node child=null;
    while((child=getUnvisitedChildNode(node))!=null) {
    child.visited=true;
    printNode(child);
    queue.add(child);
    public void bfs()
    {
    // BFS uses Queue data structure
    Queue queue = new LinkedList();
    queue.add(this.rootNode);
    printNode(this.rootNode);
    rootNode.visited = true;
    while(!queue.isEmpty()) {
    Node node = (Node)queue.remove();
    Node child=null;
    while((child=getUnvisitedChildNode(node))!=null) {
    child.visited=true;
    printNode(child);
    queue.add(child);
    }
    }
    // Clear visited property of nodes
    clearNodes();
    }
    // Clear visited property of nodes
    clearNodes();
    }

    public void dfs() {
    // DFS uses Stack data structure
    Stack stack = new Stack();
    stack.push(this.rootNode);
    rootNode.visited=true;
    printNode(rootNode);
    while(!stack.isEmpty())
    {
    Node node = (Node)s.peek();
    Node child = getUnvisitedChildNode(n);
    if(child != null) {
    child.visited = true;
    printNode(child);
    s.push(child);
    }
    else {
    s.pop();
    public void dfs() {
    // DFS uses Stack data structure
    Stack stack = new Stack();
    stack.push(this.rootNode);
    rootNode.visited=true;
    printNode(rootNode);
    while(!stack.isEmpty()) {
    Node node = (Node)s.peek();
    Node child = getUnvisitedChildNode(n);
    if(child != null) {
    child.visited = true;
    printNode(child);
    s.push(child);
    }
    else {
    s.pop();
    }
    }
    // Clear visited property of nodes
    clearNodes();
    }
    // Clear visited property of nodes
    clearNodes();
    }


    Class Node
    {
    Class Node {
    Char data;
    Public Node(char c) {
    this.data=c;
    }
    }
    }
  2. gennad created this gist Jan 23, 2011.
    53 changes: 53 additions & 0 deletions BFSDFS.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    Class Main {

    public void bfs()
    {
    // BFS uses Queue data structure
    Queue queue = new LinkedList();
    queue.add(this.rootNode);
    printNode(this.rootNode);
    rootNode.visited = true;
    while(!queue.isEmpty()) {
    Node node = (Node)queue.remove();
    Node child=null;
    while((child=getUnvisitedChildNode(node))!=null) {
    child.visited=true;
    printNode(child);
    queue.add(child);
    }
    }
    // Clear visited property of nodes
    clearNodes();
    }

    public void dfs() {
    // DFS uses Stack data structure
    Stack stack = new Stack();
    stack.push(this.rootNode);
    rootNode.visited=true;
    printNode(rootNode);
    while(!stack.isEmpty())
    {
    Node node = (Node)s.peek();
    Node child = getUnvisitedChildNode(n);
    if(child != null) {
    child.visited = true;
    printNode(child);
    s.push(child);
    }
    else {
    s.pop();
    }
    }
    // Clear visited property of nodes
    clearNodes();
    }


    Class Node
    {
    Char data;
    Public Node(char c) {
    this.data=c;
    }
    }