2013-05-04 4 views
5

में बीएफएस लागू करना मैं जावा में एक नौसिखिया हूं, और मुझे कुछ मदद की ज़रूरत है।जावा

मैं चौड़ाई पहले खोज एल्गोरिथ्म लागू करने के लिए (अनब्लॉक मेरे Android पर एक खेल) एक पहेली खेल को हल करने की कोशिश कर रहा हूँ। मैं जीयूआई के साथ किया गया है, लेकिन मैं एल्गोरिदम के साथ फंस गया हूँ।

अब तक मैं प्रत्येक ब्लॉक, जो रूट नोड के बच्चों नोड्स होना चाहिए की उपलब्ध चाल भरोसा कर सकते हैं। प्रत्येक नोड (लिंक्डलिस्ट) में प्रत्येक ब्लॉक की स्थिति होती है, और सभी नोड्स को सेट में संग्रहीत किया जा रहा है।

मुझे अब जो चाहिए वह प्रत्येक नोड को विज़िट के रूप में चिह्नित करना है, इसलिए मुझे लूप में नहीं मिलता है।

मैं मदद के किसी भी प्रकार की सराहना करेंगे, और कृपया मुझे सही कर अगर मैं कुछ भी साथ गलत कर रहा हूँ।

अग्रिम :) धन्यवाद

उत्तर

6
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(); 
} 

यह जावा में एक चौड़ाई पहले खोज का एक उदाहरण अगर आप कुछ कोड हमें आपके अनुकूल

+1

यदि आप लिंक की गई सूची पर 'डेक' इंटरफ़ेस का उपयोग करते हैं, तो आप आसानी से उस बीएफएस को डीएफएस (यदि आवश्यक हो) भी संशोधित कर सकते हैं। http://docs.oracle.com/javase/7/docs/api/java/util/Deque.html –

+1

विधियां 'printNode() 'और' विज़िट()' परिभाषित कहां हैं? मैं 'विज़िट' का अनुकरण कैसे कर सकता हूं? – Growler

3
public static void bfs(BinaryTree.Node<Integer> root) 
{ 
    BinaryTree.Node<Integer> temp; //a binary tree with a inner generic node class 
    Queue<BinaryTree.Node<Integer>> queue = new LinkedList<>(); //can't instantiate a Queue since abstract, so use LLQueue 
    if (root == null) 
    { 
     return; 
    } 
    queue.add(root); 
    while (!queue.isEmpty()) 
    { 
     temp = queue.poll(); //remove the node from the queue 
     //process current node 
     System.out.println(temp.data); 
     //process the left child first 
     if (temp.left != null) 
     { 
      queue.add(temp.left); 
     } 
     //process the right child after the left if not null 
     if (temp.right != null) 
     { 
      queue.add(temp.right); 
     } 
    } 
} 
1

@Growler मैं मदद कर सकते हैं देना है हारून के लिंक पर टिप्पणी नहीं कर सका (पर्याप्त प्रतिनिधि नहीं) लेकिन दौरा किया गया क्षेत्र नोड वर्ग में एक सार्वजनिक क्षेत्र का सदस्य है।

public class Node{ 
    public boolean visited; 
    public Object data; 
    //other fields 

     public Node(Object data){ 
      this.visited = false; 
      this.data = data; 
     } 
} 

प्रिंट नोड के रूप में, मैं aaronman सिर्फ प्रिंट विधि के लिए नोड वस्तु गुजर रहा है और यह सिर्फ जो कुछ डेटा नोड वर्ग पकड़ सकता है

public void print(Node node){ 
    System.out.println(node.data); 
} 
1

कृपया इस कोशिश प्रदर्शित कर रहा है मान:

import java.util.ArrayList; 
import java.util.List; 

public class TreeTraverse { 

    static class Node{ 
     Node(int data){ 
      this.data = data; 
      this.left = null; 
      this.right = null; 
      this.visited = false; 
     } 
     int data; 
     Node left; 
     Node right; 
     boolean visited; 
    } 

    public static void main(String[] args) { 
     //The tree: 
     // 1 
     ///\ 
     // 7 9 
     // \/\ 
     // 8 2 3 

     Node node1 = new Node(1); 
     Node node7 = new Node(7); 
     Node node9 = new Node(9); 
     Node node8 = new Node(8); 
     Node node2 = new Node(2); 
     Node node3 = new Node(3); 
     node1.left = node7; 
     node1.right = node9; 
     node7.right = node8; 
     node9.right = node3; 
     node9.left = node2; 
     System.out.println("BFS: "); 
     breadthFirstSearch(node1); 

    } 

    private static void breadthFirstSearch(Node node){ 
     List<Node> al = new ArrayList<>(); 
     al.add(node); 
     while(!al.isEmpty()){ 
      node = al.get(0); 
      if(node.left != null){ 
       int index = al.size(); 
       al.add(index,node.left); 
      } 
      if(node.right != null){ 
       int index = al.size(); 
       al.add(index,node.right); 
      } 
      System.out.print(al.get(0).data+" "); 
      al.remove(0); 


     } 
    } 

} 

अधिक जानकारी के लिए, कृपया देखें: https://github.com/m-vahidalizadeh/foundations/blob/master/src/algorithms/TreeTraverse.java