如何实现这样的一个树结构,并进行统计?

xingweiboy 2009-08-19 04:11:16
遇到这样一个问题:
假设将书分为三大类(01类、02类、03类);
01类下又有三个子类(0101类、0102类、0103类);
02类下有两个子类(0201类、0202类);
03类下没有子类。
同时,我们又假设书的作者集合为(A、B、C.....Y、Z),书的语言种类集合为(AA、AB、AC....AZ、....ZA、ZB....ZZ)。
在0101类下定义了(A,AA)(B,AZ)...等等
在0102,0103,0201,0202,03类下同样定义了这么一个集合

现在来了一批书,(A,AA)(A,AB)...(A,AZ)....(A,ZA)...(A,ZZ)...(Z,AA)....(Z,ZZ)
我想要按照书的类别进行统计,有可能统计第一层01、02、03类下各有多少书,有可能统计第二层0101、0102、0103、0201、0202、03下各有多少书?
我该如何使用树实现这样的功能~~
求教中,自己也研究中~~
...全文
176 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
弘石 2009-08-20
  • 打赏
  • 举报
回复
一般都是使用sql从数据库里直接查统计结果
gzchenbing 2009-08-19
  • 打赏
  • 举报
回复
up
baobeihumin521 2009-08-19
  • 打赏
  • 举报
回复
顶下11111111111111111111111111111111
flyinghawl 2009-08-19
  • 打赏
  • 举报
回复
应该是从第一层开始统计吧,直到最后一层依次统计,你可以判断层数来实行遍历
xiaozejun 2009-08-19
  • 打赏
  • 举报
回复
个人觉得首先应该进行数据库的设计
先把数据库设计好了 自然就出来了
xingweiboy 2009-08-19
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 why_java 的回复:]
给你棵JS树吧!!自己看看》。
JScript code<html><head><title>Tree View</title><meta name="author" content=""/><style type="text/css">
body{
font-size: 9pt;
}
.treeview-line-cross{
font-size:0px;
border:solid red 0px;
height:16px;
width:16px;
background-image:url(http://p.blog.csdn.net/images/p_blog_csdn_net/sunxing007/EntryImages/20090611/treeview-default-line.gif); background-repeat:no-repeat;
background-position:0px 0px;float:left;
}

.treeview-line-single{
font-size:0px;
border:solid red 0px;
height:16px;
width:16px;
background-image:url(http://p.blog.csdn.net/images/p_blog_csdn_net/sunxing007/EntryImages/20090611/treeview-default-line.gif); background-repeat:no-repeat;
background-position:0px-32px;float:left;
}
.treeview-line-last{
font-size:0px;
border:solid red 0px;
height:8px;
width:16px;
background:url(http://p.blog.csdn.net/images/p_blog_csdn_net/sunxing007/EntryImages/20090611/treeview-default-line.gif) left bottom no-repeat;float:left;
}
.treeview-line-blank{
font-size:0px;
border:solid red 0px;
height:16px;
width:16px;float:left;
}</style></head><body><div id="dest" style="border: solid red 1px; width: 300px; height: 500px;"></div></body><script>function $(id){return document.getElementById(id);}/**
* js树形菜单
* 作者 sunxing007
* 转载请注明来自:http://blog.csdn.net/sunxing007*/function TreeView(title, isFolder){this.el=null;this.title= title;this.isFolder= isFolder;this.children= [];
}

TreeView.prototype.printout=function(ident){
alert(this.title);if(this.isFolder&&this.children.length>0){for(var i=0; i<this.children.length; i++){this.children[i].printout();
}
}
}

TreeView.prototype.toHTML=function(leftStr, append){var t="<div style='height:16px; overflow:hidden;'>";var hasChild=this.isFolder&&this.children.length>0;if(this.isFolder){
t+= (append+"<img src='http://p.blog.csdn.net/images/p_blog_csdn_net/sunxing007/EntryImages/20090611/folder.gif' onclick='doLeafClick(this,"+ hasChild+")'/>"+this.title);
}else{
t+= (append+"<img src='http://p.blog.csdn.net/images/p_blog_csdn_net/sunxing007/EntryImages/20090611/file.gif' />"+this.title);
}
t+="</div>";if(this.isFolder&&this.children.length>0){
t+="<div>";for(var i=0; i<this.children.length-1; i++){
t+= ( leftStr+this.children[i].toHTML(leftStr+"<div class='treeview-line-single'></div>","<div class='treeview-line-cross'></div>"));
}
t+= (leftStr+this.children[i].toHTML(leftStr+"<div class='treeview-line-blank'></div>","<div class='treeview-line-last'></div>"));
t+="</div>";
}return t;
}var root=new TreeView("C:(SYSTEM)",true);var bea=new TreeView("bea",true);
bea.children.push(new TreeView("logs",true));
bea.children.push(new TreeView("license.bea",false));
root.children.push(bea);var doc=new TreeView("doc",true);var basic=new TreeView("Basic",true);
basic.children.push(new TreeView("test.xml",false))
doc.children.push(basic);
root.children.push(doc);
root.children.push(new TreeView("winnt",true));
root.children.push(new TreeView("sys.info",false));
root.children.push(new TreeView("1.sql",false));//root.printout(); $('dest').innerHTML= root.toHTML("","");function doLeafClick(e, hasChild){if(hasChild){var c= e.parentNode.nextSibling;if(c.style.display==""){
e.src="http://p.blog.csdn.net/images/p_blog_csdn_net/sunxing007/EntryImages/20090611/folder-closed.gif";
c.style.display="none";
}else{
e.src="http://p.blog.csdn.net/images/p_blog_csdn_net/sunxing007/EntryImages/20090611/folder.gif";
c.style.display="";
}
}else{if(e.src.indexOf("folder-closed.gif")==-1){
e.src="http://p.blog.csdn.net/images/p_blog_csdn_net/sunxing007/EntryImages/20090611/folder-closed.gif";
}else{
e.src="http://p.blog.csdn.net/images/p_blog_csdn_net/sunxing007/EntryImages/20090611/folder.gif";
}
}
}</script></html>
[/Quote]
兄弟,你太辛苦了,可是我要的不是一个JS控件的那种树,而是要对数据进行统计,都是在实体Bean中完成的,不是html界面
xingweiboy 2009-08-19
  • 打赏
  • 举报
回复
2楼兄弟,我要的不是设计这样的一个表结构,而是已经有了这样的树结构的数据表和需要统计的数据表,需要在JAVA程序中进行统计
xingweiboy 2009-08-19
  • 打赏
  • 举报
回复
可能我说的不是很清晰,我要的不是从数据库表中进行查询,得到统计的结果。数据库的表已经是设计好的。
乍一看好像我要的树结构和统计确实没有很大关联,主要是因为我想要设计一个公共的方法,因为要做很多报表,对不同的数据进行统计,可是这些数据共有的特点就是有这种树形结构,比如部门档案了,按照部门统计效益;物资档案了,根据物资分类的不同统计物资消耗等等。所以想要做这么一个公共的方法,参入的参数为树结构和数据对象,返回结果是分类统计好的数据对象。
弘石 2009-08-19
  • 打赏
  • 举报
回复
你所提到的统计和树好像没有关系
  • 打赏
  • 举报
回复
书的分类,书的作者,书的语言作为master表,书作为一个数据表。每一本书属于什么类,属于哪个作者,属于什么语言,都跟master关联,这样很容易就用sql查出你所需要条件的统计数字来了。
radeonxhl 2009-08-19
  • 打赏
  • 举报
回复
晕倒了,楼上的树具备统计功能吗?
bigbug9002 2009-08-19
  • 打赏
  • 举报
回复
关键是书的结构是什么?
why_java 2009-08-19
  • 打赏
  • 举报
回复
给你棵JS树吧!!自己看看》。

<html>
<head>
<title>Tree View</title>
<meta name="author" content="" />
<style type="text/css">
body{
font-size: 9pt;
}
.treeview-line-cross{
font-size:0px;
border:solid red 0px;
height:16px;
width:16px;
background-image:url(http://p.blog.csdn.net/images/p_blog_csdn_net/sunxing007/EntryImages/20090611/treeview-default-line.gif);
background-repeat:no-repeat;
background-position:0px 0px;
float:left;
}

.treeview-line-single{
font-size:0px;
border:solid red 0px;
height:16px;
width:16px;
background-image:url(http://p.blog.csdn.net/images/p_blog_csdn_net/sunxing007/EntryImages/20090611/treeview-default-line.gif);
background-repeat:no-repeat;
background-position:0px -32px;
float:left;
}
.treeview-line-last{
font-size:0px;
border:solid red 0px;
height:8px;
width:16px;
background:url(http://p.blog.csdn.net/images/p_blog_csdn_net/sunxing007/EntryImages/20090611/treeview-default-line.gif) left bottom no-repeat;
float:left;
}
.treeview-line-blank{
font-size:0px;
border:solid red 0px;
height:16px;
width:16px;
float:left;
}

</style>
</head>
<body>
<div id="dest" style="border: solid red 1px; width: 300px; height: 500px;"></div>
</body>
<script>
function $(id){return document.getElementById(id);}
/**
* js树形菜单
* 作者 sunxing007
* 转载请注明来自:http://blog.csdn.net/sunxing007
*/
function TreeView(title, isFolder){
this.el = null;
this.title = title;
this.isFolder = isFolder;
this.children = [];
}

TreeView.prototype.printout = function(ident){
alert(this.title);
if(this.isFolder&&this.children.length>0){
for(var i=0; i<this.children.length; i++){
this.children[i].printout();
}
}
}

TreeView.prototype.toHTML = function(leftStr, append){
var t = "<div style='height:16px; overflow:hidden;'>";
var hasChild = this.isFolder&&this.children.length>0;
if(this.isFolder){
t += (append + "<img src='http://p.blog.csdn.net/images/p_blog_csdn_net/sunxing007/EntryImages/20090611/folder.gif' onclick='doLeafClick(this," + hasChild + ")'/>" + this.title);
}
else{
t += (append + "<img src='http://p.blog.csdn.net/images/p_blog_csdn_net/sunxing007/EntryImages/20090611/file.gif' />" + this.title);
}
t += "</div>";
if(this.isFolder&&this.children.length>0){
t += "<div>";
for(var i=0; i<this.children.length-1; i++){
t += ( leftStr + this.children[i].toHTML(leftStr+"<div class='treeview-line-single'></div>", "<div class='treeview-line-cross'></div>"));
}
t += (leftStr + this.children[i].toHTML(leftStr+"<div class='treeview-line-blank'></div>", "<div class='treeview-line-last'></div>"));
t += "</div>";
}
return t;
}
var root = new TreeView("C:(SYSTEM)", true);
var bea = new TreeView("bea", true);
bea.children.push(new TreeView("logs", true));
bea.children.push(new TreeView("license.bea", false));
root.children.push(bea);
var doc = new TreeView("doc", true);
var basic = new TreeView("Basic", true);
basic.children.push(new TreeView("test.xml", false))
doc.children.push(basic);
root.children.push(doc);
root.children.push(new TreeView("winnt", true));
root.children.push(new TreeView("sys.info", false));
root.children.push(new TreeView("1.sql", false));
//root.printout();
$('dest').innerHTML = root.toHTML("","");

function doLeafClick(e, hasChild){
if(hasChild){
var c = e.parentNode.nextSibling;
if(c.style.display==""){
e.src="http://p.blog.csdn.net/images/p_blog_csdn_net/sunxing007/EntryImages/20090611/folder-closed.gif";
c.style.display="none";
}
else{
e.src="http://p.blog.csdn.net/images/p_blog_csdn_net/sunxing007/EntryImages/20090611/folder.gif";
c.style.display="";
}
}
else{
if(e.src.indexOf("folder-closed.gif")==-1){
e.src="http://p.blog.csdn.net/images/p_blog_csdn_net/sunxing007/EntryImages/20090611/folder-closed.gif";
}
else{
e.src="http://p.blog.csdn.net/images/p_blog_csdn_net/sunxing007/EntryImages/20090611/folder.gif";
}
}
}
</script>
</html>
Billy.Wang 2009-08-19
  • 打赏
  • 举报
回复
问题太笼统了吧,你要先设计数据库表吧。

关于书的分类 可以设计一个递归表 ,
然后 书的作者一个表,
书的语言一个表,
再设置相应的关联条件,
应该能解决你的问题了吧。

xingweiboy 2009-08-19
  • 打赏
  • 举报
回复
为啥没理我呢~~~

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧