便于理解遍历树节点-java实现

本文通过一个Java Swing的例子展示了如何实现树形结构的前序遍历、中序遍历、后序遍历、广度优先遍历和深度优先遍历。代码中创建了一个简单的树结构,并为每个遍历方式提供了一个按钮,点击按钮即可按相应方式打印树的节点。此外,还提供了遍历直属子节点的功能。
摘要由CSDN通过智能技术生成

代码中包含前序排列、后序排列、广度排列、深度排列以及遍历直属的子节点。前、中及后排列中的方位词表达是根的位置,都是左在前右在后。

前序排列(根左右):

中序排列(左根右)

 后序排列(左右根):

 广度优先遍历:

 深度优先遍历:

效果图如下:

以前序遍历为例

代码如下:

package Tree;

import java.awt.*;
import java.awt.event.*;
import java.util.*;

import javax.swing.*;
import javax.swing.tree.*;

public class  ExampleFrame_03 extends JFrame {
   
   /**
    * 
    */
   private static final long serialVersionUID = 1L;
   private DefaultMutableTreeNode root;
   
   public static void main(String args[]) {
      ExampleFrame_03 frame = new ExampleFrame_03();
      frame.setVisible(true);
   }
   
   public ExampleFrame_03() {
      super();
      setTitle("遍历树节点");
      setBounds(100, 100, 290, 260);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      root = new DefaultMutableTreeNode("ROOT");
      DefaultMutableTreeNode nodeFirstA = new DefaultMutableTreeNode(
            "FirstA");
      nodeFirstA.add(new DefaultMutableTreeNode("SecondAA"));
      nodeFirstA.add(new DefaultMutableTreeNode("SecondAB"));
      root.add(nodeFirstA);
      DefaultMutableTreeNode nodeFirstB = new DefaultMutableTreeNode(
            "FirstB");
      root.add(nodeFirstB);
      DefaultMutableTreeNode nodeFirstC = new DefaultMutableTreeNode(
            "FirstC");
      nodeFirstC.add(new DefaultMutableTreeNode("SecondCA"));
      DefaultMutableTreeNode nedeSecondCB = new DefaultMutableTreeNode(
            "SecondCB");
      nedeSecondCB.add(new DefaultMutableTreeNode("ThirdCBA"));
      nedeSecondCB.add(new DefaultMutableTreeNode("ThirdCBB"));
      nodeFirstC.add(nedeSecondCB);
      nodeFirstC.add(new DefaultMutableTreeNode("SecondCC"));
      root.add(nodeFirstC);
      JTree tree = new JTree(root);
      getContentPane().add(tree, BorderLayout.CENTER);
      final JPanel panel = new JPanel();
      panel.setLayout(new GridLayout(0, 1));
      getContentPane().add(panel, BorderLayout.EAST);
      final JButton button = new JButton("按前序遍历节点");
      button.addActionListener(new ButtonActionListener("按前序遍历"));
      panel.add(button);
      final JButton button_1 = new JButton("按后序遍历节点");
      button_1.addActionListener(new ButtonActionListener("按后序遍历"));
      panel.add(button_1);
      final JButton button_2 = new JButton("以广度优先遍历");
      button_2.addActionListener(new ButtonActionListener("以广度优先遍历"));
      panel.add(button_2);
      final JButton button_3 = new JButton("以深度优先遍历");
      button_3.addActionListener(new ButtonActionListener("以深度优先遍历"));
      panel.add(button_3);
      final JButton button_4 = new JButton("遍历直属子节点");
      button_4.addActionListener(new ButtonActionListener("遍历子节点"));
      panel.add(button_4);
   }
   
   private class ButtonActionListener implements ActionListener {
      
      private String 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();
            // 根据节点级别输出占位符
            for (int l = 0; l < node.getLevel(); l++) {
               System.out.print("----");
            }
            System.out.println(node.getUserObject());// 输出节点标签
         }
         System.out.println();
         System.out.println();
      }
   }
   
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值