Java代码树形图 java树形数据结构
请问这个树状图在Java中应该怎么做?
打个比方
专注于为中小企业提供成都网站设计、做网站、成都外贸网站建设公司服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业伍家岗免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上千余家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
可以写一个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());
如何用Java实现树形结构啊?
package tree;
import java.util.LinkedList;
import java.util.List;
/**
* 功能:把一个数组的值存入二叉树中,然后进行3种方式的遍历
*
* 参考资料0:数据结构(C语言版)严蔚敏
*
* 参考资料1:
*
* 参考资料2:
*
* @author ocaicai@yeah点虐 @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点虐 @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);
}
}
哪位大侠可以帮我写一段java代码!是关于根据数据库里的数据来形成一个动态树形图
这是代码,你可以自己调试一下。
数据结构如下:
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
怎样利用java在页面实现树形结构图
你针对这个表写个实体类,写出它的dao,然后取出所有数据放到List,把list放到request.setAttribute("all");在请求到的页面中用getAttribute取出,然后用js写DOM模型表示出来,js中的引用可以直接使用java的数据,例如:%
List l = (List)request.getAttribute("all");
for(int i==0;il.size();i++){
实体类 a = ( 实体类)l.get(i);
%
var url = %=a.get属性()%
%
}
%
DOM模型可以使js更好的控制我们想要实现的效果
标题名称:Java代码树形图 java树形数据结构
新闻来源:http://cdiso.cn/article/ddgspgp.html