遍歷樹節點(多層)的方法(java)


前序遍歷,后序遍歷,廣度遍歷,深度遍歷,遍歷一級節點.以及按鈕如何響應點擊事件。

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.tree.*;
 
public class Tree_03 extends JFrame{
  private DefaultMutableTreeNode root; //定義根節點.
  //主方法.
  public static void main(String[] args){
//實例化本類.
 Tree_03 frame=new Tree_03();
 frame.setVisible(true);
  }
  //構造方法.
  public Tree_03(){
 super();  //繼承父類.
 setTitle("遍歷節點方法"); //設置窗口標題.
 setBounds(100,100,300,300); //設置絕對位置.
 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //設置窗口關閉方法.
 
 root=new DefaultMutableTreeNode("ROOT"); //設置根節點名.
 DefaultMutableTreeNode noteA=new DefaultMutableTreeNode("FirstA"); //定義及設置第一個子節點.
 root.add(noteA); //將第一節點添加到樹根上.
 //給noteA增加兩個子節點.
 noteA.add(new DefaultMutableTreeNode("SecondAA"));
 noteA.add(new DefaultMutableTreeNode("SecondAB"));
 DefaultMutableTreeNode noteB=new DefaultMutableTreeNode("FirstB");//定義及設置第二個子節點.
 root.add(noteB);
 
 DefaultMutableTreeNode noteC=new DefaultMutableTreeNode("FirstC");//定義及設置第三個子節點.
 root.add(noteC);
 noteC.add(new DefaultMutableTreeNode("SecondCA"));//為noteC增加子節點.
 DefaultMutableTreeNode noteSecondCB=new DefaultMutableTreeNode("SecondCB");//為noteC增加子節點.
 noteC.add(noteSecondCB);//將noteSecondCB添加到noteC子節點下.
 //給noteSecondCB增加子節點.
 noteSecondCB.add(new DefaultMutableTreeNode("SecondCBA"));
 noteSecondCB.add(new DefaultMutableTreeNode("SecondCBB"));
 //給noteC增加SecondCC子節點.
 noteC.add(new DefaultMutableTreeNode("SecondCC"));
 
 JTree tree=new JTree(root);//根據根節點創建樹.
 getContentPane().add(tree,BorderLayout.CENTER);
 
 //開始操作節點.
 //定義Panel.
 final JPanel panel=new JPanel();
 panel.setLayout(new GridLayout(0,1)); //設置panel為網格布局.
 getContentPane().add(panel,BorderLayout.EAST); //將panel放置到窗口右邊.
 
 final JButton button_01=new JButton("按前序遍歷節點"); //定義及設置第一個按鈕.
 button_01.addActionListener( new ButtonActionListener("按前序遍歷節點"));
 panel.add(button_01); //將按鈕放置到panel里.
 final JButton button_02=new JButton("按后序遍歷節點"); //定義及設置第二個按鈕.
 button_02.addActionListener( new ButtonActionListener("按后序遍歷節點"));
 panel.add(button_02); //將按鈕放置到panel里.
 final JButton button_03=new JButton("以廣度遍歷節點"); //定義及設置第三個按鈕.
 button_03.addActionListener( new ButtonActionListener("以廣度遍歷節點"));
 panel.add(button_03); //將按鈕放置到panel里.
 final JButton button_04=new JButton("以深度遍歷節點"); //定義及設置第四個按鈕.
 button_04.addActionListener( new ButtonActionListener("以深度遍歷節點"));
 panel.add(button_04); //將按鈕放置到panel里.
 final JButton button_05=new JButton("遍歷直屬子節點"); //定義及設置第五個按鈕.
 button_05.addActionListener( new ButtonActionListener("遍歷直屬子節點"));
 panel.add(button_05); //將按鈕放置到panel里.
 
  }
    //定義按鈕點擊事件.
  private class  ButtonActionListener implements ActionListener{
 private String mode;//定義mode變量.
 //構造方法.
 public ButtonActionListener(String mode){
 this.mode=mode;
 }
 //定義激活方法.
 public void actionPerformed(ActionEvent e){
 //聲明節點枚舉對象.
 Enumeration<?> enumeration;
 if(mode.equals("按前序遍歷節點")){
 //按前序遍歷根節點.
 enumeration=root.preorderEnumeration();
 }else if(mode.equals("按后序遍歷節點")){
 enumeration=root.postorderEnumeration();
 }else if(mode.equals("以廣度遍歷節點")){
 enumeration=root.breadthFirstEnumeration();
 }else if(mode.equals("以深度遍歷節點")){
 enumeration=root.depthFirstEnumeration();
 }else{
 enumeration=root.children();//遍歷該節點的所有子節點.
 }
 //知道了遍歷方式,開始遍歷.
  while(enumeration.hasMoreElements()){ //遍歷枚舉對象.
 //先定義一個節點變量.
  DefaultMutableTreeNode node;
  node=(DefaultMutableTreeNode) enumeration.nextElement();//將節點名稱給node.
 //根據級別輸出占位符.
 for(int l=0;l<node.getLevel();l++){
 System.out.print("---");
 }
 System.out.println(node.getUserObject());//輸入節點標簽.
 }
 System.out.println();
 System.out.println();
 }
  }
  
}
 
運行結果
java遍歷樹節點方法

 

ROOT
---FirstA
------SecondAA
------SecondAB
---FirstB
---FirstC
------SecondCA
------SecondCB
---------SecondCBA
---------SecondCBB
------SecondCC
 
 
------SecondAA
------SecondAB
---FirstA
---FirstB
------SecondCA
---------SecondCBA
---------SecondCBB
------SecondCB
------SecondCC
---FirstC
ROOT
 
 
ROOT
---FirstA
---FirstB
---FirstC
------SecondAA
------SecondAB
------SecondCA
------SecondCB
------SecondCC
---------SecondCBA
---------SecondCBB
 
 
------SecondAA
------SecondAB
---FirstA
---FirstB
------SecondCA
---------SecondCBA
---------SecondCBB
------SecondCB
------SecondCC
---FirstC
ROOT
 
 
---FirstA
---FirstB
---FirstC


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM