思路:
成都创新互联公司于2013年成立,是专业互联网技术服务公司,拥有项目网站制作、网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元平遥做网站,已为上家服务,为平遥各地企业和个人服务,联系电话:13518219792
首先你要把词条的数据源整理好
然后把数据源和你的记事本读取的数据进行比较
最后统计每个词条出现的次数,并做成树状图
这是代码,你可以自己调试一下。
数据结构如下:
CREATE TABLE dtree (
id int,
pid int,
name varchar(200),
url varchar(200),
title varchar(200),
target varchar(200),
icon varchar(200),
iconopen varchar(200),
opened bit);
为了实现获取数据库变量功能,需要建立一个DTree类,并编译生成CLASS文件,放入\WEB-INF\classes文件夹下。
DTree类代码如下:
package work3;
public class DTree {
private int id;
private int pid;
private String name;
private String url;
private String title;
private String target;
private String icon;
private String iconOpen;
private int opened;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getTarget() {
return target;
}
public void setTarget(String target) {
this.target = target;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public String getIconOpen() {
return iconOpen;
}
public void setIconOpen(String iconOpen) {
this.iconOpen = iconOpen;
}
public int getOpened() {
return opened;
}
public void setOpened(int opened) {
this.opened = opened;
}
}
work3.jsp代码如下:
%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%
%@ page import="java.sql.*"%
jsp:useBean id='settree' scope="application" class="work3.DTree" /
!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
""
html
head
meta http-equiv="Content-Type" content="text/html; charset=GB18030"
link rel="StyleSheet" href="dtree.css" type="text/css" /
script type="text/javascript" src="dtree.js"/script
titledTree in MySQL/title
/head
body
h2
Example
/h2
div class="dtree"
p
a href="javascript: d.openAll();"open all/a |
a href="javascript: d.closeAll();"close all/a
/p
script type="text/javascript"
!--
d = new dTree('d');
%
//驱动程序名
String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
//数据库用户名
String userName = "sa";
//密码
String userPwd = "1";
//数据库名
String dbName = "master";
//表名
String tableName = "dtree";
//连接字符串
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName="+dbName;
//加载驱动
Class.forName(driverName).newInstance();
//连接数据库
java.sql.Connection conn = DriverManager.getConnection(url,userName,userPwd);
//得到Statement实例
java.sql.Statement statement = conn.createStatement();
//查询数据
String sql = "select * from " + tableName;
//返回结果
java.sql.ResultSet rs = statement.executeQuery(sql);
//获取变量
while (rs.next()) {
settree.setId(rs.getInt(1));
settree.setPid(rs.getInt(2));
settree.setName(rs.getString(3));
settree.setUrl(rs.getString(4));
settree.setTitle(rs.getString(5));
settree.setTarget(rs.getString(6));
settree.setIcon(rs.getString(7));
settree.setIconOpen(rs.getString(8));
settree.setOpened(rs.getInt(9));
if(settree.getPid()==0)
settree.setOpened(1);
%
d.add(%=settree.getId()%,%=settree.getPid()%,'%=settree.getName()%','%=settree.getUrl()%','%=settree.getTitle()%','%=settree.getTarget()%','','',%=settree.getOpened()%);
%
}
%
document.write(d);
//--
/script
/div
/body
/html
打个比方
可以写一个Tree的类
public class Tree extends BaseDomain {
private String id;
private String key;
private String icon;
private String title;
private String value;
private String text;
private String code;
private String mtype;
private String type;
/**
* 部门的特殊个别字段
* 1.部门 / 2.岗位 的编码
*/
private String number;
private Double order;
private Double sort;
private String href;
private String component;
private ListTreeT children;
private String parentId;
private boolean hasParent = false;
private boolean hasChildren = false;
private Date createTime;
private Date modifyTime;
public void initChildren(){
this.children = new ArrayList();
}
}
在写一个工具类
public class TreeUtil {
protected TreeUtil() {
}
private final static String TOP_NODE_ID = "0";
/**
* 用于构建菜单
*
* @param nodes nodes
* @param T T
* @return T TreeT
*/
public static T TreeT build(ListTreeT nodes) {
if (nodes == null) {
return null;
}
ListTreeT topNodes = new ArrayList();
nodes.forEach(node - {
String pid = node.getParentId();
if (pid == null || TOP_NODE_ID.equals(pid)) {
topNodes.add(node);
return;
}
for (TreeT n : nodes) {
String id = n.getId();
if (id != null id.equals(pid)) {
if (n.getChildren() == null)
n.initChildren();
n.getChildren().add(node);
node.setHasParent(true);
n.setHasChildren(true);
n.setHasParent(true);
return;
}
}
if (topNodes.isEmpty())
topNodes.add(node);
});
TreeT root = new Tree();
root.setId("0");
root.setParentId("");
root.setHasParent(false);
root.setHasChildren(true);
root.setChildren(topNodes);
root.setText("root");
return root;
}
}
写完了这两个在写业务层
一个构建书的方法
private void buildTrees(List trees, List menus, List ids) {
menus.forEach(menu - {
ids.add(menu.getId().toString());
Tree tree = new Tree();
tree.setId(menu.getId().toString());
tree.setKey(tree.getId());
tree.setParentId(menu.getParentId().toString());
tree.setText(menu.getName());
tree.setTitle(tree.getText());
tree.setIcon(menu.getIcon());
tree.setComponent(menu.getComponent());
tree.setCreateTime(menu.getCreateTime());
tree.setCreateTime(menu.getCreateTime());
tree.setHref(menu.getHref());
tree.setSort(menu.getSort());
tree.setCode(menu.getCode());
tree.setMtype(menu.getMtype());
trees.add(tree);
});
}
MapString, Object result = new HashMap();
ListDept depts = findDepts(dept, request);
ListTreeDept trees = new ArrayList();
buildTrees(trees, depts);
TreeDept deptTree = TreeUtil.build(trees);
result.put("rows", deptTree);
result.put("total", depts.size());
package tree;
import java.util.LinkedList;
import java.util.List;
/**
* 功能:把一个数组的值存入二叉树中,然后进行3种方式的遍历
*
* 参考资料0:数据结构(C语言版)严蔚敏
*
* 参考资料1:
*
* 参考资料2:
*
* @author ocaicai@yeah.net @date: 2011-5-17
*
*/
public class BinTreeTraverse2 {
private int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
private static ListNode nodeList = null;
/**
* 内部类:节点
*
* @author ocaicai@yeah.net @date: 2011-5-17
*
*/
private static class Node {
Node leftChild;
Node rightChild;
int data;
Node(int newData) {
leftChild = null;
rightChild = null;
data = newData;
}
}
public void createBinTree() {
nodeList = new LinkedListNode();
// 将一个数组的值依次转换为Node节点
for (int nodeIndex = 0; nodeIndex array.length; nodeIndex++) {
nodeList.add(new Node(array[nodeIndex]));
}
// 对前lastParentIndex-1个父节点按照父节点与孩子节点的数字关系建立二叉树
for (int parentIndex = 0; parentIndex array.length / 2 - 1; parentIndex++) {
// 左孩子
nodeList.get(parentIndex).leftChild = nodeList
.get(parentIndex * 2 + 1);
// 右孩子
nodeList.get(parentIndex).rightChild = nodeList
.get(parentIndex * 2 + 2);
}
// 最后一个父节点:因为最后一个父节点可能没有右孩子,所以单独拿出来处理
int lastParentIndex = array.length / 2 - 1;
// 左孩子
nodeList.get(lastParentIndex).leftChild = nodeList
.get(lastParentIndex * 2 + 1);
// 右孩子,如果数组的长度为奇数才建立右孩子
if (array.length % 2 == 1) {
nodeList.get(lastParentIndex).rightChild = nodeList
.get(lastParentIndex * 2 + 2);
}
}
/**
* 先序遍历
*
* 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已
*
* @param node
* 遍历的节点
*/
public static void preOrderTraverse(Node node) {
if (node == null)
return;
System.out.print(node.data + " ");
preOrderTraverse(node.leftChild);
preOrderTraverse(node.rightChild);
}
/**
* 中序遍历
*
* 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已
*
* @param node
* 遍历的节点
*/
public static void inOrderTraverse(Node node) {
if (node == null)
return;
inOrderTraverse(node.leftChild);
System.out.print(node.data + " ");
inOrderTraverse(node.rightChild);
}
/**
* 后序遍历
*
* 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已
*
* @param node
* 遍历的节点
*/
public static void postOrderTraverse(Node node) {
if (node == null)
return;
postOrderTraverse(node.leftChild);
postOrderTraverse(node.rightChild);
System.out.print(node.data + " ");
}
public static void main(String[] args) {
BinTreeTraverse2 binTree = new BinTreeTraverse2();
binTree.createBinTree();
// nodeList中第0个索引处的值即为根节点
Node root = nodeList.get(0);
System.out.println("先序遍历:");
preOrderTraverse(root);
System.out.println();
System.out.println("中序遍历:");
inOrderTraverse(root);
System.out.println();
System.out.println("后序遍历:");
postOrderTraverse(root);
}
}
你是要那种的。。。就是选择一个目录。
然后把下面的所有都列出来,
就可以了吗
import java.io.File;
public class Test {
public static void main(String[] args) {
File file=new File("C:/");
viewTree(file,index);
}
static int index=0;
private static void viewTree(File file,int index) {
String space="";
for(int j=0;jindex;j++){
space += " ";
}
File [] files= file.listFiles();
for(int i=0;ifiles.length;i++){
File f=files[i];
if(f.isDirectory()){
index++;
System.out.println(space+"+"+f.getName());
viewTree(f,index);
}else{
System.out.println(space+"-"+f.getName());
}
}
}
}
网页题目:java代码写出树状图 java 树状图
地址分享:https://www.cdcxhl.com/article26/hhghjg.html
成都网站建设公司_创新互联,为您提供网站导航、、响应式网站、静态网站、手机网站建设、网站收录
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联