java递归获取父节点(java根据子节点获取它对应的所有父节点)

http://www.itjxue.com  2023-02-11 22:24  来源:未知  点击次数: 

java 找到一节点的所有子节点 是不是得递归实现?

2L谁说必须用递归的,只是递归写起来简单罢了,迭代的方法一样OK;

深度优先或者广度优先都可以

LZ的问题用迭代的方法可以这样解决:

1。把当前节点(需要查找字节点的节点)压入一个堆栈,这步是初始化;

2。从堆栈中弹出一个节点,如果该节点是叶子节点,则这条路已经走不通了,如果是非叶子节点,那就把这个节点的所有子节点压入堆栈

3。重复第二步直到堆栈为空

上面三步就能遍历当前节点的所有字节点

递归的话:

f(node){

for(遍历node的所有子节点){

child=当前子节点

if(child为叶子节点){

..return

;}

else{

f(child)

}

}

}

java1.8使用mybaitis连接mysql递归查询所有父节点用到:和@报错Cause: java.util.NoSuchElementException

Java mysql mybatis批量更新数据库,采用以下写法即可执行,但是数据库连接必须配置:allowMultiQueries=true

例如:jdbc:mysql://192.168.1.236:3306/test?useUnicode=trueamp;characterEncoding=UTF-8allowMultiQueries=true

update?id="batchUpdate"??parameterType="java.util.List"

?????foreach?collection="list"?item="item"?index="index"?open=""?close=""?separator=";"

update?test?

set

?test=${item.test}+1

/set

where?id?=?${item.id}

/foreach

???/update

MyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。MyBatis使用简单的XML或注解用于配置和原始映射,将接口和Java的POJOs(Plan Old Java Objects,普通的Java对象)映射成数据库中的记录.

java Tree 怎么获得父节点的值。?SWT做的tree

TreeItem parentItem=item.getParentItem();得到父节点

Tree tree= item.getParent();得到树

parentItem.getText();节点显示的值

parentItem.getData();item上保存的数据

java的递归查询怎么写

原文在这里,写得不错,楼主可参考下,具体链接如下,我只是搬运工!

/**?

????*?说明方法描述:将list转为树tree结构?

????*??

????*?@param?allRrecords?

????*?@return?

????*?@time?2016年5月10日?下午6:00:35?

????*?@author?yangdong?

????*/??

???public?ListRecord?useListRecordToTree(ListRecord?allRrecords)?{??

??

???????ListRecord?listParentRecord?=?new?ArrayListRecord();??

???????ListRecord?listNotParentRecord?=?new?ArrayListRecord();??

???????//?第一步:遍历allRrecords保存所有数据的uuid用于判断是不是根节点??

???????MapString,?String?mapAllUuid?=?new?HashMapString,?String();??

???????MapString,?Record?allRecordMap?=?new?HashMapString,?Record();??

???????for?(Record?record?:?allRrecords)?{??

???????????mapAllUuid.put(record.getStr("uuid"),?record.getStr("uuid"));??

???????????allRecordMap.put(record.getStr("uuid"),?record);??

???????}??

???????//?第二步:遍历allRrecords找出所有的根节点和非根节点??

???????if?(allRrecords?!=?null??allRrecords.size()??0)?{??

???????????for?(Record?record?:?allRrecords)?{??

???????????????if?(StringUtil.isBlank(record.getStr("parent_uuid"))??

???????????????????||?!mapAllUuid.containsKey(record.getStr("parent_uuid")))?{??

???????????????????listParentRecord.add(record);??

???????????????}?else?{??

???????????????????listNotParentRecord.add(record);??

???????????????}??

???????????}??

???????}??

??

???????//?第三步:?递归获取所有子节点??

???????if?(listParentRecord.size()??0)?{??

???????????for?(Record?record?:?listParentRecord)?{??

???????????????//?添加所有子级??

???????????????record.set("childs",?this.getTreeChildRecord(listNotParentRecord,?record.getStr("uuid")));??

???????????}??

???????}??

???????return?listParentRecord;??

???}??

??

???/**?

????*?说明方法描述:使list转换为树并根据关键字和节点名称过滤?

????*??

????*?@param?allRecords?所有节点?

????*?@param?keywords?要过滤的关键字?

????*?@param?filterFields?要过滤的字段?

????*?@return?

????*?@time?2016年5月19日?下午3:27:32?

????*?@author?yangdong?

????*/??

???public?ListRecord?useListRecordToTreeByKeywords(ListRecord?allRecords,?String?keywords,?String...?filterFields)?{??

???????ListRecord?listRecord?=?new?ArrayListRecord();??

???????MapString,?Record?allRecordMap?=?new?HashMapString,?Record();??

???????for?(Record?record?:?allRecords)?{??

???????????allRecordMap.put(record.getStr("uuid"),?record);??

???????}??

???????//?遍历allRrecords找出所有的nodeName和关键字keywords相关的数据??

???????if?(allRecords?!=?null??allRecords.size()??0)?{??

???????????if?(filterFields.length??1)?{??

???????????????for?(Record?record?:?allRecords)?{??

???????????????????for?(String?field?:?filterFields)?{??

???????????????????????//?比较??

???????????????????????if?(record.getStr(field).toLowerCase().indexOf(keywords.toLowerCase())?!=?-1)?{??

???????????????????????????listRecord.add(record);??

???????????????????????}??

???????????????????}??

???????????????}??

???????????}?else?{??

???????????????for?(Record?record?:?allRecords)?{??

???????????????????//?比较??

???????????????????if?(record.getStr(filterFields[0]).toLowerCase().indexOf(keywords.toLowerCase())?!=?-1)?{??

???????????????????????listRecord.add(record);??

???????????????????}??

???????????????}??

???????????}??

???????}??

???????//?查找过滤出来的节点和他们的父节点??

???????listRecord?=?this.getSelfAndTheirParentRecord(listRecord,?new?ArrayListRecord(),??

?????????????????????????????????????????????????????new?HashMapString,?Record(),?allRecordMap);??

???????//?将过滤出来的数据变成树tree结构??

???????listRecord?=?this.useListRecordToTree(listRecord);??

???????return?listRecord;??

???}??

??

???/**?

????*?说明方法描述:递归查询子节点?

????*??

????*?@param?childList?子节点?

????*?@param?parentUuid?父节点id?

????*?@return?

????*?@time?2016年5月10日?下午3:29:35?

????*?@author?yangdong?

????*/??

???private?ListRecord?getTreeChildRecord(ListRecord?childList,?String?parentUuid)?{??

???????ListRecord?listParentRecord?=?new?ArrayListRecord();??

???????ListRecord?listNotParentRecord?=?new?ArrayListRecord();??

???????//?遍历tmpList,找出所有的根节点和非根节点??

???????if?(childList?!=?null??childList.size()??0)?{??

???????????for?(Record?record?:?childList)?{??

???????????????//?对比找出父节点??

???????????????if?(StringUtil.equals(record.getStr("parent_uuid"),?parentUuid))?{??

???????????????????listParentRecord.add(record);??

???????????????}?else?{??

???????????????????listNotParentRecord.add(record);??

???????????????}??

??

???????????}??

???????}??

???????//?查询子节点??

???????if?(listParentRecord.size()??0)?{??

???????????for?(Record?record?:?listParentRecord)?{??

???????????????//?递归查询子节点??

???????????????record.set("childs",?getTreeChildRecord(listNotParentRecord,?record.getStr("uuid")));??

???????????}??

???????}??

???????return?listParentRecord;??

???}??

??

???/**?

????*?说明方法描述:递归找出本节点和他们的父节点?

????*??

????*?@param?parentList?根据关键字过滤出来的相关节点的父节点?

????*?@param?resultList?返回的过滤出来的节点?

????*?@param?filterRecordMap?已经过滤出来的节点?

????*?@param?allRecordMap?所有节点?

????*?@return?

????*?@time?2016年5月19日?上午9:53:56?

????*?@author?yangdong?

????*/??

???private?ListRecord?getSelfAndTheirParentRecord(ListRecord?parentList,?ListRecord?resultList,??

????????????????????????????????????????????????????MapString,?Record?filterRecordMap,??

????????????????????????????????????????????????????MapString,?Record?allRecordMap)?{??

???????//?当父节点为null或者节点数量为0时返回结果,退出递归??

???????if?(parentList?==?null?||?parentList.size()?==?0)?{??

???????????return?resultList;??

???????}??

???????//?重新创建父节点集合??

???????ListRecord?listParentRecord?=?new?ArrayListRecord();??

???????//?遍历已经过滤出来的节点??

???????for?(Record?record?:?parentList)?{??

??

???????????String?uuid?=?record.getStr("uuid");??

???????????String?parent_uuid?=?record.getStr("parent_uuid");??

??

???????????//?如果已经过滤出来的节点不存在则添加到list中??

???????????if?(!filterRecordMap.containsKey(uuid))?{??

???????????????listParentRecord.add(record);//?添加到父节点中??

???????????????filterRecordMap.put(uuid,?record);//?添加到已过滤的map中??

???????????????allRecordMap.remove(uuid);//?移除集合中相应的元素??

???????????????resultList.add(record);//?添加到结果集中??

???????????}??

??

???????????//?找出本节点的父节点并添加到listParentRecord父节点集合中,并移除集合中相应的元素??

???????????if?(StringUtil.isNotBlank(parent_uuid))?{??

???????????????Record?parentRecord?=?allRecordMap.get(parent_uuid);??

???????????????if?(parentRecord?!=?null)?{??

???????????????????listParentRecord.add(parentRecord);??

???????????????????allRecordMap.remove(parent_uuid);??

???????????????}??

???????????}??

??

???????}??

???????//?递归调用??

???????getSelfAndTheirParentRecord(listParentRecord,?resultList,?filterRecordMap,?allRecordMap);??

??

???????return?resultList;??

???}??

[java]?view?plain?copy

//示例??

[java]?view?plain?copy

/**?

?????*?说明方法描述:递归查询所有权限?

?????*??

?????*?@param?keyword?

?????*?@param?is_deleted?

?????*?@return?

?????*?@time?2016年5月10日?下午3:47:50?

?????*?@author?yangdong?

?????*/??

????public?ListRecord?getRecordByKeywordRecursive(String?keyword,?String?is_deleted)?{??

????????//?第一步:查询所有的数据??

????????StringBuffer?sql?=?new?StringBuffer(??

????????????????????????????????????????????"?select?pa.uuid,pa.parent_uuid,pa.author_code,pa.author_name,pa.is_menu,pa.sort_number,pa.is_enable,pa.menu_icon?");??

????????sql.append("??from?s_author?pa");??

????????ListObject?params?=?new?ArrayListObject();??

????????sql.append("?where??pa.is_deleted=??");??

????????params.add(is_deleted);??

????????sql.append("?order?by?pa.sort_number?asc?");??

??

??

????????ListRecord?allRrecords?=?Db.use(AppConst.DB_DATASOURCE_MAIN).find(sql.toString(),?ParamUtil.listToArray(params));??

[java]?view?plain?copy

????????????//第二步:将list变为树tree结构??

????????if?(StringUtil.isNotBlank(keyword))?{??

????????????return?super.useListRecordToTreeByKeywords(allRrecords,?keyword,?"author_name");??

????????}?else?{??

????????????return?super.useListRecordToTree(allRrecords);??

????????}??

????}

java 递归数据库生成 树形结构问题

1、准备表结构及对应的表数据

a、表结构:

create table TB_TREE

(

CID NUMBER not null,

CNAME VARCHAR2(50),

PID NUMBER //父节点

)

b、表数据:

insert into tb_tree (CID, CNAME, PID) values (1, '中国', 0);

insert into tb_tree (CID, CNAME, PID) values (2, '北京市', 1);

insert into tb_tree (CID, CNAME, PID) values (3, '广东省', 1);

insert into tb_tree (CID, CNAME, PID) values (4, '上海市', 1);

insert into tb_tree (CID, CNAME, PID) values (5, '广州市', 3);

insert into tb_tree (CID, CNAME, PID) values (6, '深圳市', 3);

insert into tb_tree (CID, CNAME, PID) values (7, '海珠区', 5);

insert into tb_tree (CID, CNAME, PID) values (8, '天河区', 5);

insert into tb_tree (CID, CNAME, PID) values (9, '福田区', 6);

insert into tb_tree (CID, CNAME, PID) values (10, '南山区', 6);

insert into tb_tree (CID, CNAME, PID) values (11, '密云县', 2);

insert into tb_tree (CID, CNAME, PID) values (12, '浦东', 4);

2、TreeNode对象,对应tb_tree

public class TreeNode implements Serializable {

private Integer cid;

private String cname;

private Integer pid;

private List nodes = new ArrayList();

public TreeNode() {

}

//getter、setter省略

}

3、测试数据

public class TreeNodeTest {

@Test

public void loadTree() throws Exception{

System.out.println(JsonUtils.javaToJson(recursiveTree(1)));

}

/**

* 递归算法解析成树形结构

*

* @param cid

* @return

* @author jiqinlin

*/

public TreeNode recursiveTree(int cid) {

//根据cid获取节点对象(SELECT * FROM tb_tree t WHERE t.cid=?)

TreeNode node = personService.getreeNode(cid);

//查询cid下的所有子节点(SELECT * FROM tb_tree t WHERE t.pid=?)

List childTreeNodes = personService.queryTreeNode(cid);

//遍历子节点

for(TreeNode child : childTreeNodes){

TreeNode n = recursiveTree(child.getCid()); //递归

node.getNodes().add(n);

}

return node;

}

}

输出的json格式如下:

{

"cid": 1,

"nodes": [

{

"cid": 2,

"nodes": [

{

"cid": 11,

"nodes": [

],

"cname": "密云县",

"pid": 2

}

],

"cname": "北京市",

"pid": 1

},

{

"cid": 3,

"nodes": [

{

"cid": 5,

"nodes": [

{

"cid": 7,

"nodes": [

],

"cname": "海珠区",

"pid": 5

},

{

"cid": 8,

"nodes": [

],

"cname": "天河区",

"pid": 5

}

],

"cname": "广州市",

"pid": 3

},

{

"cid": 6,

"nodes": [

{

"cid": 9,

"nodes": [

],

"cname": "福田区",

"pid": 6

},

{

"cid": 10,

"nodes": [

],

"cname": "南山区",

"pid": 6

}

],

"cname": "深圳市",

"pid": 3

}

],

"cname": "广东省",

"pid": 1

},

{

"cid": 4,

"nodes": [

{

"cid": 12,

"nodes": [

],

"cname": "浦东",

"pid": 4

}

],

"cname": "上海市",

"pid": 1

}

],

"cname": "中国",

"pid": 0

}

(责任编辑:IT教学网)

更多

推荐JSP教程文章