java使用递归实现树形结构
比如数据库如下idname640000000000北京市640100000000昌平区640101000000霍营640101001000回龙观东大街这样怎么用递归实现地...
比如数据库如下
id name
640000000000 北京市
640100000000 昌平区
640101000000 霍营
640101001000 回龙观东大街
这样怎么用递归实现地区的树形结构 展开
id name
640000000000 北京市
640100000000 昌平区
640101000000 霍营
640101001000 回龙观东大街
这样怎么用递归实现地区的树形结构 展开
2个回答
展开全部
insert tb_menu(id, name, parent) (640000000000,北京市 ,0);
insert tb_menu(id, name, parent) (640100000000,昌平区 ,1);
insert tb_menu(id, name, parent) (640101000000,霍营 ,2);
insert tb_menu(id, name, parent) (640101001000, 回龙观东大街,3);
添加一个节点属性, 根据数据不同代表的地位不同,0就代表父节点 ,1是0的子节点,2是1的子节点,以此类推。
追问
如果数据量特别大,是全国各地的地区规划代码,怎么办?
追答
Notepad++,你可以用这个工具试试,我平时面对大量插入语句时就用它。这个你要不行的话,就得写存储过程了
展开全部
var zNodes=[
{id:0,pId:-1,name:"Aaaa"},
{id:1,pId:0,name:"A"},
{id:11,pId:1,name:"A1"},
{id:12,pId:1,name:"A2"},
{id:13,pId:1,name:"A3"},
{id:2,pId:0,name:"B"},
{id:21,pId:2,name:"B1"},
{id:22,pId:2,name:"B2"},
{id:23,pId:2,name:"B3"},
{id:3,pId:0,name:"C"},
{id:31,pId:3,name:"C1"},
{id:32,pId:3,name:"C2"},
{id:33,pId:3,name:"C3"},
{id:34,pId:31,name:"x"},
{id:35,pId:31,name:"y"},
{id:36,pId:31,name:"z"},
{id:37,pId:36,name:"z1123"} ,
{id:38,pId:37,name:"z123123123"}
];
function treeMenu(a){
this.tree=a||[];
this.groups={};
};
treeMenu.prototype={
init:function(pid){
this.group();
return this.getDom(this.groups[pid]);
},
group:function(){
for(var i=0;i<this.tree.length;i++){
if(this.groups[this.tree[i].pId]){
this.groups[this.tree[i].pId].push(this.tree[i]);
}else{
this.groups[this.tree[i].pId]=[];
this.groups[this.tree[i].pId].push(this.tree[i]);
}
}
},
getDom:function(a){
if(!a){return ''}
var html='\n<ul >\n';
for(var i=0;i<a.length;i++){
html+='<li><a href="#">'+a[i].name+'</a>';
html+=this.getDom(this.groups[a[i].id]);
html+='</li>\n';
};
html+='</ul>\n';
return html;
}
};
var html=new treeMenu(zNodes).init(0);
alert(html);
{id:0,pId:-1,name:"Aaaa"},
{id:1,pId:0,name:"A"},
{id:11,pId:1,name:"A1"},
{id:12,pId:1,name:"A2"},
{id:13,pId:1,name:"A3"},
{id:2,pId:0,name:"B"},
{id:21,pId:2,name:"B1"},
{id:22,pId:2,name:"B2"},
{id:23,pId:2,name:"B3"},
{id:3,pId:0,name:"C"},
{id:31,pId:3,name:"C1"},
{id:32,pId:3,name:"C2"},
{id:33,pId:3,name:"C3"},
{id:34,pId:31,name:"x"},
{id:35,pId:31,name:"y"},
{id:36,pId:31,name:"z"},
{id:37,pId:36,name:"z1123"} ,
{id:38,pId:37,name:"z123123123"}
];
function treeMenu(a){
this.tree=a||[];
this.groups={};
};
treeMenu.prototype={
init:function(pid){
this.group();
return this.getDom(this.groups[pid]);
},
group:function(){
for(var i=0;i<this.tree.length;i++){
if(this.groups[this.tree[i].pId]){
this.groups[this.tree[i].pId].push(this.tree[i]);
}else{
this.groups[this.tree[i].pId]=[];
this.groups[this.tree[i].pId].push(this.tree[i]);
}
}
},
getDom:function(a){
if(!a){return ''}
var html='\n<ul >\n';
for(var i=0;i<a.length;i++){
html+='<li><a href="#">'+a[i].name+'</a>';
html+=this.getDom(this.groups[a[i].id]);
html+='</li>\n';
};
html+='</ul>\n';
return html;
}
};
var html=new treeMenu(zNodes).init(0);
alert(html);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |