前序遍历,后序遍历,广度遍历,深度遍历,遍历一级节点.以及按钮如何响应点击事件。
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();
}
}
}
运行结果:
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
- java 遍历树节点 同时保留所有的从根到叶节点的路径
直接在代码.稍后细说 数据结构定义: /** * */ package Servlet; import java.util.ArrayList; import java.util.List; /** ...
- 【zTree】zTree展开树节点
今天在做zTree树的时候想着将第一级tree展开,于是利用下面方法: /** * 展开树节点的第一层 */ function openFirstTreenode(){ // 获取树对象 var tr ...
- Java 用自带dom解析器遍历叶子节点内容
一.XML文件config.xml,内容如下: <?xml version="1.0" encoding="UTF-8" standalone=" ...
- java遍历树(深度遍历和广度遍历
java遍历树如现有以下一颗树:A B B1 B11 B2 B22 C C ...
- 先序遍历DOM树的5种方法
DOM树由文档中的所有节点(元素节点.文本节点.注释节点等)所构成的一个树结构,DOM树的解析和构建是浏览器要实现的关键功能.既然DOM树是一个树结构,那么我们就可以使用遍历树结构的相关方法来对DOM ...
- Java遍历树(深度优先+广度优先)
在编程生活中,我们总会遇见树性结构,这几天刚好需要对树形结构操作,就记录下自己的操作方式以及过程.现在假设有一颗这样树,(是不是二叉树都没关系,原理都是一样的) 1.深度优先 英文缩写为DFS即Dep ...
- MySQL实现树状所有子节点查询的方法
本文实例讲述了MySQL实现树状所有子节点查询的方法.分享给大家供大家参考,具体如下: 在Oracle 中我们知道有一个 Hierarchical Queries 通过CONNECT BY 我们可以方 ...
- PAT树_层序遍历叶节点、中序建树后序输出、AVL树的根、二叉树路径存在性判定、奇妙的完全二叉搜索树、最小堆路径、文件路由
03-树1. List Leaves (25) Given a tree, you are supposed to list all the leaves in the order of top do ...
- java中遍历map对象的多种方法
在Java中如何遍历Map对象 How to Iterate Over a Map in Java 在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有ma ...
随机推荐
- zabbix-2.4.7环境部署与初始化安装
一.zabbix简介: zabbix的特点: - 安装与配置简单,学习成本低 - 支持多语言(包括中文) - 免费开源 - 自动发现服务器与网络设备 - 分布式监视以及WEB集中管理功能 - 可以无a ...
- 20145325张梓靖 《Java程序设计》第6周学习总结
20145325张梓靖 <Java程序设计>第6周学习总结 教材学习内容总结 串流设计 输入串流(将数据从来源取出),代表对象为java.io.InputStream实例,输出串流(将数据 ...
- Windows10系统远程桌面连接出现卡顿如何解决
最新的windows10系统下,用户只要开启远程桌面连接,就能够轻松地操控其他电脑.但是,最近部分用户在win10中启用远程连接时,发现电脑窗口变得非常缓慢卡顿,这是怎么回事呢?其实,该问题与系统的设 ...
- CentOS7.2 安装zookeeper3.4.9
Zookeeper-3.4.9 下载Zookeeper-3.4.9 在/usr/local下创建hadoop文件夹 将下载的文件迁移到该文件夹下,并解压 tar -zxvf zookeeper-3.4 ...
- 使用 p4-graphs 命令将p4程序依赖关系图形化
位置:/home/wasdns/p4factory/targets/l2_switch/p4src 命令: cd /home/wasdns/p4factory/targets/l2_switch/p4 ...
- POJ 1325 Machine Schedule(最小点覆盖)
http://poj.org/problem?id=1325 题意: 两种机器A和B.机器A具有n种工作模式,称为mode_0,mode_1,...,mode_n-1,同样机器B有m种工作模式mode ...
- php 格式化时间
<?php echo date("Y/m/d") . "<br>"; echo date("Y.m.d") . " ...
- webpack3.0之loader配置及编写(一)
loader 用于对模块的源代码进行转换.loader 可以使你在 import 或"加载"模块时预处理文件.loader 可以将文件从不同的语言(如 TypeScript)转换为 ...
- Object.assign()是浅拷贝
浅拷贝: 复制的值指向同一个内存地址 深拷贝:复制的值指向新的内存地址 var a = { xm: { name: 'xiaoming' } } var b = Object.assign({}, a ...
- UVA-10129 Play on Words (判断欧拉道路的存在性)
题目大意:给出一系列单词,当某个单词的首字母和前一个单词的尾字母相同,则这两个单词能链接起来.给出一系列单词,问是否能够连起来. 题目分析:以单词的首尾字母为点,单词为边建立有向图,便是判断图中是否存 ...