首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

最简单的树

2012-01-21 
求一个最简单的树问题是这样的:有数据库如下idname1name2name31aa1a112aa1a123aa2a214bb1b115bb2b216cc1c1

求一个最简单的树
问题是这样的:
有数据库如下
id     name1     name2     name3
  1           a         a1           a11
  2           a         a1           a12  
  3           a         a2           a21    
  4           b         b1           b11    
  5           b         b2           b21    
  6           c         c1           c11
数据是动态的(不一定就是a啊b啊的)。
现在要从数据库读取信息,以树的形式保存,然后得出name1有多少种,各种name1中又有几种name2,又各有多少种name3。



[解决办法]
表述不清楚,如果要在頁面顯示
去下個dtree,只要三層節點就夠了。
[解决办法]
up
[解决办法]
这种表结构做树是不是麻烦了点 最好弄个parentId用DTREE很方便的
[解决办法]
public static void print(HashMap root)
{
for (Iterator iterator = root.keySet().iterator(); iterator.hasNext();)
{
String ss = (String) iterator.next();
HashMap temp = (HashMap) root.get(ss);
System.out.println(ss + "= " + temp.size());
print(temp);
}
}


public static void main(String[] args)
{
String[][] s = { { "1 ", "a ", "a1 ", "a11 " }, { "2 ", "a ", "a1 ", "a12 " },
{ "3 ", "a ", "a2 ", "a21 " }, { "4 ", "b ", "b1 ", "b11 " },
{ "5 ", "b ", "b2 ", "b21 " }, { "6 ", "c ", "c1 ", "c11 " } };

HashMap root = new HashMap();
HashMap temp = null;
//name1
for (int i = 0; i < s.length; i++)
{
if (root.containsKey(s[i][1]))
{
continue;
}
else
{
root.put(s[i][1], new HashMap());
}
}
//name2
for (int i = 0; i < s.length; i++)
{
temp = (HashMap) root.get(s[i][1]);
if (temp.containsKey(s[i][2]))
{
continue;
}
else
{
temp.put(s[i][2], new HashMap());
}
}
//name3
for (int i = 0; i < s.length; i++)
{
temp = (HashMap) root.get(s[i][1]);
temp = (HashMap) temp.get(s[i][2]);
if (temp.containsKey(s[i][3]))
{
continue;
}
else
{
temp.put(s[i][3], new HashMap());
}
}
print(root);


}
看这个能不能满足你要求
------解决方案--------------------


Ajax解决。
http://www.cnblogs.com/ustbwuyi/archive/2007/03/19/679586.html
[解决办法]
再建个临时的表,字段为
Name, PName
Name可以作为key
每从你的表里读出一行的时候,往这个表里插入三条数据,
name1,null
name2,name1
name3,name2
找name1有多少种的时候,就是
select distinct count(Name) where PName is null
找name2有多少种的时候,就是
select distinct count(Name) where PName=name2
...
如果上面设置了key,就不需要distinct
[解决办法]
应用继承啊,在利用复合的形式来调用,比较时看调用的对象是否和要比较的对象为同一类
[解决办法]
我的树,用javascript做的。只要放在网页里就可以,当然还要有图片
<HTML>
<HEAD>
<title> IE-Menu </title>

<script LANGUAGE= "JavaScript ">
<!--
//每个节点有一个数组,包含 4+n个元素
// node[0]为0/1 对应节点的展开/关闭
// node[1]为0/1 对应文件夹的关闭/展开
// node[2]为1 如果节点的子节点是文档
// node[3]是节点的名称
// node[4]...node[4+n]为n个子节点

// 初始化菜单的数据

function generateTree()
{
var aux1, aux2, aux3, aux4

foldersTree = folderNode( "我的网站 ")
aux1 = appendChild(foldersTree, folderNode( "娱乐天地 "))
aux2 = appendChild(aux1, leafNode( "literature "))
appendChild(aux2, generateDocEntry(0, "文学巨著 ", "basefolder.htm ", " "))
appendChild(aux2, generateDocEntry(0, "杂家杂谈 ", "basefolder.htm ", " "))
aux2 = appendChild(aux1, leafNode( "Music "))
appendChild(aux2, generateDocEntry(0, "世纪流行风 ", "basefolder.htm ", " "))
appendChild(aux2, generateDocEntry(0, "民乐赏析 ", "basefolder.htm ", " "))
appendChild(aux2, generateDocEntry(0, "器乐演奏 ", "basefolder.htm ", " "))
aux1 = appendChild(foldersTree, folderNode( "资料仓库 "))
aux2 = appendChild(aux1, leafNode( "SoftWare "))
appendChild(aux2,generateDocEntry(1, "系统软件 ", "basefolder.htm ", " "))
appendChild(aux2,generateDocEntry(1, "工具软件 ", "basefolder.htm ", " "))
appendChild(aux2,generateDocEntry(1, "
游戏
", "basefolder.htm ", " "))
aux2 = appendChild(aux1, folderNode( "SourcePrograme "))
aux3 = appendChild(aux2, leafNode( "JavaScript "))
appendChild(aux3, generateDocEntry(1, "第一期 ", "basefolder.htm ", " "))
appendChild(aux3, generateDocEntry(1, "第二期 ", "basefolder.htm ", " "))
appendChild(aux3, generateDocEntry(1, "第三期 ", "basefolder.htm ", " "))
aux3 = appendChild(aux2, leafNode( "ASP "))
appendChild(aux3, generateDocEntry(1, "第一期 ", "basefolder.htm ", " "))
appendChild(aux3, generateDocEntry(1, "第二期 ", "basefolder.htm ", " "))
aux3 = appendChild(aux2, leafNode( "PHP "))
appendChild(aux3, generateDocEntry(1, "第一期 ", "basefolder.htm ", " "))
appendChild(aux3, generateDocEntry(1, "第二期 ", "basefolder.htm ", " "))
appendChild(aux3, generateDocEntry(1, "第三期 ", "basefolder.htm ", " "))


appendChild(aux3, generateDocEntry(1, "第四期 ", "basefolder.htm ", " "))
aux3 = appendChild(aux2, leafNode( "CGI "))
appendChild(aux3,generateDocEntry(1, "第一期 ", "basefolder.htm ", " "))
appendChild(aux3,generateDocEntry(1, "第二期 ", "basefolder.htm ", " "))
appendChild(aux3,generateDocEntry(1, "第三期 ", "basefolder.htm ", " "))
appendChild(aux3,generateDocEntry(1, "第四期 ", "basefolder.htm ", " "))
appendChild(aux3,generateDocEntry(1, "第五期 ", "basefolder.htm ", " "))
appendChild(aux3,generateDocEntry(1, "第六期 ", "basefolder.htm ", " "))
aux3 = appendChild(aux2, leafNode( "另类 "))
appendChild(aux3, generateDocEntry(1, "Applet效果 ", "basefolder.htm ", " "))
appendChild(aux3, generateDocEntry(1, "Flash动画 ", "basefolder.htm ", " "))
aux2 = appendChild(aux1, folderNode( "DownLoad Area "))
aux3 = appendChild(aux2, leafNode( "免费软件 "))
appendChild(aux3, generateDocEntry(1, "升级程序 ", "basefolder.htm ", " "))
appendChild(aux3, generateDocEntry(1, "补丁系列 ", "basefolder.htm ", " "))
}

// 创建节点的辅助函数
function folderNode(name)
{
var arrayAux
arrayAux = new Array
arrayAux[0] = 0
arrayAux[1] = 0
arrayAux[2] = 0
arrayAux[3] = name

return arrayAux
}
function leafNode(name)
{
var arrayAux
arrayAux = new Array
arrayAux[0] = 0
arrayAux[1] = 0
arrayAux[2] = 1
arrayAux[3] = name

return arrayAux
}

function appendChild(parent, child)
{
parent[parent.length] = child
return child
}

function generateDocEntry(icon, docDescription, link)
{
var retString = " "

if (icon==0)
retString = " <A href= ' "+link+ " ' target=folderFrame> <img src= 'doc.gif ' alt= '在右边框架中打开 ' "
else
retString = " <A href= ' "+link+ " ' target=_blank> <img src= 'link.gif ' alt= '在新窗口中打开 ' "
retString = retString + " border=0> </a> <td nowrap> <font style= 'font-size:9pt;font-family:宋体 '> " + docDescription + " </font> "

return retString

}


//刷新树状菜单
function redrawTree()
{
var doc = top.treeFrame.window.document

doc.clear()
doc.write( " <body bgcolor= 'white '> ")
redrawNode(foldersTree, doc, 0, 1, " ")
doc.close()
}

function redrawNode(foldersNode, doc, level, lastNode, leftSide)
{
var j=0
var i=0

doc.write( " <table border=0 cellspacing=0 cellpadding=0> ")
doc.write( " <tr> <td valign = middle nowrap> ")



doc.write(leftSide)

if (level> 0)
if (lastNode) // 'brother '子节点数组中有否兄弟节点
{
doc.write( " <img src= 'lastnode.gif ' width=16 height=22> ")
leftSide = leftSide + " <img src= 'blank.gif ' width=16 height=22> "
}
else
{
doc.write( " <img src= 'node.gif ' width=16 height=22> ")
leftSide = leftSide + " <img src= 'vertline.gif ' width=16 height=22> "
}

displayIconAndLabel(foldersNode, doc)
doc.write( " </table> ")

未完

[解决办法]
哇噻

热点排行