java叶节点是什么意思,java中jtree双击叶节点如何获取该文件所在的路径
老铁们,大家好,相信还有很多朋友对于java叶节点是什么意思和java中jtree双击叶节点如何获取该文件所在的路径的相关问题不太懂,没关系,今天就由我来为大家分享分享java叶节点是什么意思以及java中jtree双击叶节点如何获取该文件所在的路径的问题,文章篇幅可能偏长,希望可以帮助到大家,下面一起来看看吧!
Java生成树中的从根到叶子节点的所有路径
方法是在dfs的过程中维护dfs的路径,到达叶子结点时将路径加入到答案中,具体见代码的注释。
输入格式为:
7A
AB
CA
DB
EB
CF
CG
其中,第一行表示结点数量n和根结点编号,第[2..n+ 1]行每行两个字符u和v,表示树边。
importjava.io.File;
importjava.io.FileNotFoundException;
importjava.util.ArrayList;
importjava.util.List;
importjava.util.Scanner;
publicclassMain{
staticfinalintmaxn=1000;
intn;//树的结点个数
charroot;//树的根
List<List<Character>>ans=newArrayList<List<Character>>();//结果,存放所有路径
boolean[][]G=newboolean[maxn][maxn];//邻接矩阵
/**
*dfs
*@paramu当前到达的结点
*@paramfa当前结点的父节点
*@parampath根到fa的路径
*/
voiddfs(charu,charfa,List<Character>path){
path.add(u);
booleanleaf=true;//u为叶子结点
for(charv='A';v<='Z';++v){
if(G[u][v]&&v!=fa){//邻接矩阵中有别的边
leaf=false;//u不是叶子结点
dfs(v,u,path);
}
}
if(leaf){//u为叶子结点,将路径加入到答案
List<Character>pt=newArrayList<Character>();
for(Characterc:path)pt.add(c);
ans.add(pt);
}
path.remove(path.size()-1);
}
publicvoidgo()throwsFileNotFoundException
{
Scannerin=newScanner(newFile("data.in"));
Strings;//进行输入的处理时的临时变量
n=in.nextInt();//获取结点个数
s=in.next();root=s.charAt(0);//获取根
for(inti=0;i<n-1;++i){
charu,v;
s=in.next();u=s.charAt(0);//获取边,并在邻接矩阵中做标记
s=in.next();v=s.charAt(0);
G[u][v]=G[v][u]=true;
}
List<Character>path=newArrayList<Character>();
dfs(root,'0',path);
for(inti=0;i<ans.size();++i){//输出答案
for(Characterc:ans.get(i))
System.out.print(c);
System.out.println();
}
in.close();
}
publicstaticvoidmain(String[]args)throwsFileNotFoundException{
newMain().go();
}
}
用java求最短路径问题,求源程序
import java.util.Vector;
public class Link{
private Vector link= new Vector();
// private Link next= null;
public Link(){
}
public boolean addNode(Node setNode){//增加一个节点
setNode= checkNode(setNode);
if(setNode!= null){
this.link.addElement((Node)setNode);
return true;
}
return false;
}
public void delNode(Node setNode){//删除一个节点
if(!this.link.isEmpty()){
for(int i=0;i< this.link.size(); i++)
{
if(setNode.getPos()==((Node)this.link.elementAt(i)).getPos()){
this.link.remove(i);
//System.out.println("asdfasdfas:"+this.link.size());
break;
}
}
}
}
public Node checkNode(Node setNode){//判断节点是否在链表里面并取得两者的最佳值
if(!this.link.isEmpty()&& setNode!=null){
for(int i=0;i< this.link.size(); i++)
{
if(setNode.getPos()==((Node)this.link.elementAt(i)).getPos()){
if(setNode.getStep()<((Node)this.link.elementAt(i)).getStep()){
setNode=(Node)this.link.elementAt(i);
this.link.remove(i);
}
else
return null;
break;
}
}
}
return setNode;
}
public boolean isEmpty(){
return this.link.isEmpty();
}
public Node getBestNode(){//得到最好的节点
Node tmpNode= null;
if(!this.link.isEmpty()){
tmpNode=(Node)this.link.elementAt(0);
//System.out.println("tmpNodeStep:"+tmpNode.getStep());
//System.out.print("OpenNode(pos,step):");
for(int i=1;i< this.link.size(); i++)
{
//System.out.print("("+((Node)this.link.elementAt(i)).getPos()+","+((Node)this.link.elementAt(i)).getStep()+")");
if(tmpNode.getJudgeNum()>=((Node)this.link.elementAt(i)).getJudgeNum()){
tmpNode=(Node)this.link.elementAt(i);
}
}
}
return tmpNode;
}
}
public class FindBestPath{
private char[][] map= null;//地图
private int maxX,maxY;//最大的地图边界大小
Node startNode= null;//入口
Node endNode= null;//出口
private int endX,endY;
/*初始化
*@param setMap地图
*@param setX,setY边界值
//////////*@param startNode入口
//////////*param endNode出口
*@param sX,sY:开始点
*@param eX,eY:结束点
*/
public FindBestPath(char[][] setMap,int setX,int setY,int sX,int sY,int eX,int eY){
this.map= setMap;
this.maxY= setX- 1;//x,y互换
this.maxX= setY- 1;//x,y互换
//this.startNode= sNode;
//this.endNode= eNode;
Node sNode= new Node();
Node eNode= new Node();
sNode.setFarther(null);
sNode.setPos(posToNum(sX,sY));
sNode.setStep(0);
eNode.setPos(posToNum(eX,eY));
this.startNode= sNode;
this.endNode= eNode;
this.endX= eX;//numToX(eNode.getPos());
this.endY= eY;//numToY(eNode.getPos());
}
public int posToNum(int x,int y){//从xy坐标获得编号
return(x+y*(this.maxY+1));
}
public int numToX(int num){//从编号获得x坐标
return(num%(this.maxY+1));
}
public int numToY(int num){//从编号获得y坐标
return(int)(num/(this.maxY+1));
}
public boolean checkVal(int x,int y){//判断是否为障碍
//System.out.println("map["+x+"]["+y+"]="+map[x][y]);
if(this.map[x][y]=='N')
return false;
else
return true;
}
public int judge(Node nowNode){//一定要比实际距离小
//System.out.println("nowNodePos:"+nowNode.getPos());
int nowX= numToX(nowNode.getPos());
int nowY= numToY(nowNode.getPos());
int distance= Math.abs((nowX-this.endX))+Math.abs((nowY-this.endY));
// System.out.println("distance:"+distance);
return distance;
}
public Node getLeft(Node nowNode){//取得左节点
int nowX= numToX(nowNode.getPos());
int nowY= numToY(nowNode.getPos());
Node tmpNode= new Node();
if(nowY> 0){//判断节点是否到最左
if(checkVal(nowX,nowY-1)){
tmpNode.setFarther(nowNode);
tmpNode.setPos(posToNum(nowX,nowY-1));
tmpNode.setStep(nowNode.getStep()+1);
tmpNode.setJudgeNum(tmpNode.getStep()+judge(tmpNode));
return tmpNode;
}
}
return null;
}
public Node getRight(Node nowNode){//取得右节点
int nowX= numToX(nowNode.getPos());
int nowY= numToY(nowNode.getPos());
Node tmpNode= new Node();
if(nowY< this.maxX){//判断节点是否到最左
if(checkVal(nowX,nowY+1)){
tmpNode.setFarther(nowNode);
tmpNode.setPos(posToNum(nowX,nowY+1));
tmpNode.setStep(nowNode.getStep()+1);
tmpNode.setJudgeNum(tmpNode.getStep()+judge(tmpNode));
return tmpNode;
}
}
return null;
}
public Node getTop(Node nowNode){//取得上节点
int nowX= numToX(nowNode.getPos());
int nowY= numToY(nowNode.getPos());
Node tmpNode= new Node();
if(nowX> 0){//判断节点是否到最左
if(checkVal(nowX-1,nowY)){
tmpNode.setFarther(nowNode);
tmpNode.setPos(posToNum(nowX-1,nowY));
tmpNode.setStep(nowNode.getStep()+1);
tmpNode.setJudgeNum(tmpNode.getStep()+judge(tmpNode));
return tmpNode;
}
}
return null;
}
public Node getBottom(Node nowNode){//取得下节点
int nowX= numToX(nowNode.getPos());
int nowY= numToY(nowNode.getPos());
Node tmpNode= new Node();
if(nowX< this.maxY){//判断节点是否到最左
if(checkVal(nowX+1,nowY)){
tmpNode.setFarther(nowNode);
tmpNode.setPos(posToNum(nowX+1,nowY));
tmpNode.setStep(nowNode.getStep()+1);
tmpNode.setJudgeNum(tmpNode.getStep()+judge(tmpNode));
return tmpNode;
}
}
return null;
}
public Link getBestPath(){//寻找路径
Link openLink= new Link();//没有访问的路径
Link closeLink= new Link();//访问过的路径
Link path= null;//最短路径
Node bestNode= null;
Node tmpNode= null;
openLink.addNode(this.startNode);
while(!openLink.isEmpty())//openLink is not null
{
bestNode= openLink.getBestNode();//取得最好的节点
//System.out.println("bestNode:("+numToX(bestNode.getPos())+","+numToY(bestNode.getPos())+")step:"+bestNode.getJudgeNum());
if(bestNode.getPos()==this.endNode.getPos())
{
/*this.endNode.setStep(bestNode.getStep()+1);
this.endNode.setFarther(bestNode);
this.endNode.setJudgeNum(bestNode.getStep()+1);*/
path= makePath(bestNode);
break;
}
else
{
tmpNode= closeLink.checkNode(getLeft(bestNode));
if(tmpNode!= null)
//System.out.println("("+numToY(tmpNode.getPos())+","+numToX(tmpNode.getPos())+")");
openLink.addNode(tmpNode);
tmpNode= closeLink.checkNode(getRight(bestNode));
if(tmpNode!= null)
// System.out.println("("+numToY(tmpNode.getPos())+","+numToX(tmpNode.getPos())+")");
openLink.addNode(tmpNode);
tmpNode= closeLink.checkNode(getTop(bestNode));
if(tmpNode!= null)
// System.out.println("("+numToY(tmpNode.getPos())+","+numToX(tmpNode.getPos())+")");
openLink.addNode(tmpNode);
tmpNode= closeLink.checkNode(getBottom(bestNode));
if(tmpNode!= null)
// System.out.println("("+numToY(tmpNode.getPos())+","+numToX(tmpNode.getPos())+")");
openLink.addNode(tmpNode);
openLink.delNode(bestNode);
closeLink.addNode(bestNode);
}
}
return path;
}
public Link makePath(Node lastNode){//制造路径
Link tmpLink= new Link();
Node tmpNode= new Node();
int x,y;
tmpNode= lastNode;
if(tmpNode!= null){
do{
x=numToX(tmpNode.getPos());
y=numToY(tmpNode.getPos());
System.out.println("map["+x+"]["+y+"]="+map[x][y]);
tmpLink.addNode(tmpNode);
tmpNode= tmpNode.getFarther();
}while(tmpNode!= null);
}else
{
System.out.println("Couldn't find the path!");
}
return tmpLink;
}
/**
*@param args the command line arguments
*/
public static void main(String[] args){
char[][] map={
{'Y','N','z','y','x','w','v','N','N','N'},
{'Y','N','1','N','N','N','u','t','N','N'},
{'N','1','2','1','1','1','N','s','N','N'},
{'N','N','1','N','9','N','q','r','N','N'},
{'N','N','1','N','n','o','p','N','N','N'},
{'N','4','5','6','m','N','N','N','N','N'},
{'N','3','N','5','l','k','j','N','N','N'},
{'N','N','3','4','N','d','i','d','N','N'},
{'N','1','N','N','1','N','h','N','N','N'},
{'N','1','N','N','1','N','g','N','N','N'},
{'N','a','b','c','d','e','f','N','N','N'}
};
/*map[x][y]
*如上所示:maxY=10 maxX=11横的代表maxY,竖的代表maxX可以自己替换
*地图的读取是
*for(i=1;i<行的最大值;i++)
* for(j=1;j<列的最大值;j++)
* map[i][j]=地图[i][j]
*/
Link bestPath= new Link();
/*startNode.setFarther(null);
startNode.setPos(21);
startNode.setStep(0);
//endNode.setFarther(startNode);
endNode.setPos(79);
//endNode.setStep(0);*/
FindBestPath path= new FindBestPath(map, 11, 10, 10, 1, 0, 2);
//FindBestPath path= new FindBestPath(map, 11, 10, startNode, endNode);
bestPath= path.getBestPath();
//bestPath.printLink();
}
}
public class Node{
private int step;//从入口到该节点经历的步数
private int pos;//位置
private Node farther;//上一个结点
private int judgeNum;
public Node(){
}
public void setStep(int setStep){
this.step= setStep;
}
public int getStep(){
return this.step;
}
public void setPos(int setPos){
this.pos= setPos;
}
public int getPos(){
return this.pos;
}
public void setFarther(Node setNode){
this.farther= setNode;;
}
public Node getFarther(){
return this.farther;
}
public void setJudgeNum(int setInt){
this.judgeNum= setInt;;
}
public int getJudgeNum(){
return this.judgeNum;
}
}
java中jtree双击叶节点如何获取该文件所在的路径
一个读写XML文件的类
首先必须获得XML Parser的包,可以从下面的地址获得: http://xml.apache.org/xerces2-j/index.html。
然后设计一个XMLTree的类,继承自JTree类的定义和成员变量,函数定义如下:
public class XMLTree extends JTree{ private DefaultMutableTreeNode treeNode;//JTree的根节点 private DocumentBuilderFactory dbf;//这三个成员变量是xml parser需要的 private DocumentBuilder db; private Document doc; XMLTree(String fileName);//构造函数,做初始化工作 public DefaultMutableTreeNode LoadFile(Node root);//从某个XML文件生成该树 public void SaveToFile(DefaultMutableTreeNode root,FileWriter fw);//将该树存盘成XML文件 private Node parseXml( String text)}
其中构造函数所做的初始化工作如下:
XMLTree(String fileName){ dbf= DocumentBuilderFactory.newInstance();//生成dbf的实例 db= dbf.newDocumentBuilder();//生成db的实例 treeNode= LoadFile( getXMLRoot( text));//解析该xml文件,返回JTree的根节点 setModel( new DefaultTreeModel( treeNode));//根据该根节点生成JTree}
其中,parseXml是一个返回XML文件根元素的程序,如下:
private Node getXMLRoot( String text){ ByteArrayInputStream byteStream; byteStream= new ByteArrayInputStream( text.getBytes());//将XML文件读到Stream里去 try{ doc= db.parse( byteStream);//解析该xml文件。} catch( Exception e){ e.printStackTrace();} return( Node)doc.getDocumentElement();//返回该XML文件的DOM树的根元素}
核心部分的LoadFile是一个递归过程,如下:
private DefaultMutableTreeNode createTreeNode( Node root){ DefaultMutableTreeNode treeNode= null;//定义要返回的根节点 String name= root.getNodeName();//获得该节点的NodeName String value= root.getNodeValue();//获得该节点的NodeValue treeNode= new DefaultMutableTreeNode( root. getNodeType()== Node.TEXT_NODE? value: name);//如果为值节点,那么取得该节点的值,否则取得该节点的Tag的名字 if( root.hasChildNodes())//如果该节点有孩子节点,那么递归处理该节点的孩子节点{ NodeList children= root.getChildNodes();//取得该节点的子节点列表 if( children!= null){//判断子节点是否为空 int numChildren= children.getLength();//取得字节数目 for(int i=0; i< numChildren; i++){ Node node= children.item(i);//循环处理每个子节点 if( node!= null){ if( node.getNodeType()== Node.ELEMENT_NODE){ treeNode.add( createTreeNode(node));//如果该子节点还有孩子节点使用递归的方法处理该子节点} else{ String data= node.getNodeValue(); if( data!= null){ data= data.trim(); if(!data.equals(“\n”)&&!data.equals(“\r\n”)&& data.length()> 0){ treeNode.add(new DefaultMutableTreeNode(node.getNodeValue()));//如果该节点没有孩子节点,那么直接加到节点下}}}}}}} return treeNode;//返回节点}
使用Java的Swing包里的方法能够很容易地在JTree上做改动,可以使用弹出对话框的方法,也可以直接在JTree上改动。总之,JTree改动后,需要重新写回文件中去将一棵JTree写成XML文件是一个递归的过程,方法如下:
public void SaveToFile(DefaultMutableTreeNode, FileWriter fw){try{ if(root.isLeaf()) fw.write(root.toString()+“\r\n”);//如果是叶子节点则直接将该节点输出到文件中else{//不是叶子节点的话递归输出该节点 fw.write(“<”+root.toString()+“>\r\n”); for(int i=0; i< root.getChildCount(); i++){ DefaultMutableTreeNode childNode=(DefaultMutableTreeNode) root.getChildAt(i); saveFile(childNode, fw);//递归输出该节点的所有子节点} fw.write(“</”+root.toString()+“>\r\n”);}} catch(Exception e){ e.printStackTrace();}}
必须注意的是,如果XML文件中包含中文,那么需要在调用上面的函数之前,先在文件中输入该XML文件的编码方式,方法如下:
fw.write(“<?xml version=“1.0” encoding=“GB2312”?>\r\n”);
在调用该函数结束后,还应该关闭该文件,方法是:
fw.close()
好了,文章到此结束,希望可以帮助到大家。