2009-09-24 12 views
14

Abbiamo una struttura ad albero implementata usando lo DefaultMutableTreeNode specificato in Java.Albero di attraversamento fatto da DefaultMutableTreeNode

C'è qualche modo di attraversarlo, che è integrato?

In caso contrario, suggerire altre tecniche.

+0

Cosa intendi analizzando? Tipicamente si dovrebbe analizzare un'espressione per costruire una rappresentazione interna (come la struttura ad albero che si ha già). Vuoi semplicemente attraversare l'albero? – Adamski

+0

Scusate per quello. Sì, intendevo attraversarlo. – fixxxer

risposta

14

Se si intende attraversare l'albero, è possibile chiamare breadthFirstEnumeration() o depthFirstEnumeration() per iterare su tutti i nodi dell'albero.

Esempio:

DefaultMutableTreeNode root = ... 

Enumeration en = root.depthFirstEnumeration(); 
while (en.hasMoreElements()) { 

    // Unfortunately the enumeration isn't genericised so we need to downcast 
    // when calling nextElement(): 
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) en.nextElement(); 
} 
+0

Come accedere a ciascun nodo restituito dall'algoritmo di ricerca? Puoi indicarmi una buona risorsa, per favore. – fixxxer

+0

Ho appena aggiunto un codice di esempio. – Adamski

+0

Finalmente quello di cui avevo bisogno! Stavo usando un ArrayList a cui ho aggiunto i nodi mentre li ho creati, il che andava bene, ma dal momento che sto usando voci duplicate in diverse cartelle avevo bisogno di un nuovo array per ogni cartella e poi diventa molto veloce. Anche grazie alla risposta qui sotto posso elaborarli 2 volte più velocemente! – Daedric

0

corretta, breadtFirst è simmetrico. Preorder (prima radice, poi i bambini) è supportato pure (preorderEnumeration)

16

Hai in teoria quattro modi di camminare l'albero da un nodo (DefaultMutableTreeNode):

  • breadthFirstEnumeration
  • depthFirstEnumeration
  • preorderEnumeration
  • postorderEnumeration

ma in realtà la profondità prima è implementata come postorder.
JavaDoc è un po 'laconico sulle differenze su questi metodi. Sono venuto qui in cerca di una risposta, ma ho finito per fare il test stesso, con un codice che sembra:

TreeModel model = tree.getModel(); 

    DefaultMutableTreeNode rootNode = (DefaultMutableTreeNode) model.getRoot(); 
    // Just changing enumeration kind here 
    Enumeration<DefaultMutableTreeNode> en = rootNode.preorderEnumeration(); 
    while (en.hasMoreElements()) 
    { 
    DefaultMutableTreeNode node = en.nextElement(); 
    TreeNode[] path = node.getPath(); 
    System.out.println((node.isLeaf() ? " - " : "+ ") + path[path.length - 1]); 
    } 

avrei potuto raffinato con rientro proporzionale al livello, ma era solo un rapido hack.

Quindi, quali sono le differenze?

  • preorderEnumeration = dalla cima dell'albero verso il basso, come se si stesse usando la freccia verso il basso per camminare
  • postorderEnumeration = depthFirstEnumeration = primo elencare le più profonde foglie del primo percorso, allora il loro genitore, quindi il più profondo foglie del secondo percorso, ecc.
  • breadthFirstEnumeration = elencare gli elementi al primo livello, gli elementi al secondo livello, e così via

Per essere più concreti:

+ Root 
    + Folder 1 
    - Leaf F1 
    - Leaf F1 
+ Folder 2 
    + Sub-folder 1 
     - Leaf SF1 
     - Leaf SF1 
    + Sub-folder 2 
     - Leaf SF2 
     - Leaf SF2 

♦ Preorder: come indicato sopra
♦ DepthFirst/Postorder:
Leaf F1, Leaf F1, Folder 1
Leaf SF1, Leaf SF1, Sottocartella 1
Leaf SF 2, Leaf SF2, Sottocartella 2 , Cartella 2, Radice
♦ BreathFirst:
Root
Cartella 1, Cartella 2
Foglia F1, Foglia F1, Sotto-cartella 1, Sotto-cartella 2
Foglia SF 1, Foglia SF 1, Foglia SF 2 , Leaf SF 2

1

Ecco un'altra descrizione dei 3 metodi di enumerazione, che possono essere più facili da capire.

en = root.breadthFirstEnumeration(); 
//Enumeration lists all nodes at depth 0 (aka root) 
//Then all nodes at depth 1 (aka root's children, top to bottom ordering) 
//Then all nodes at depth 2, and so on till max depth reached 

en = root.preorderEnumeration(); 
//Imagine your JTree is fully expanded (where each node = a row) 
//Enumeration will list nodes from top to bottom (regardless of leaf or not) 

en = root.postorderEnumeration(); //Equivalent to root.depthFirstEnumeration(); 
//Imagine a fully expanded copy of your JTree (where each node = a row) 
//This will allow you to visualize what Enumeration List will look like 
while(treecopy.hasNodes()) { 
list 1st leaf sighted going from top to bottom, then remove that leaf } 
//as the leafs are removed, branches then become leafs, and root is last enumerated. 
+0

Inizialmente non ho detto cosa intendevo, lo intendevo come aiuto per la visualizzazione, ma l'ho detto per sbaglio letteralmente. L'ho cambiato ora, si spera sia più chiaro, grazie per il feedback. – neokyle

Problemi correlati