java递归写法、江湖告急!各位大侠会的请进!!

漫天雪_昆仑巅
博客专家认证
2014-09-03 08:12:18
江湖告急!!由于各种需要有个需求,要使用Java递归实现;
现有Java List数据集合一个,里面的数据结构如下:
id name partid
-------------------------------
1 A null
2 a1 1
3 a11 2
4 a111 3

5 a2 1
6 a22 5

5 a3 1
6 a33 5
7 a333 6

需要用递归实现查询所有节点下的最小级别的节点有哪些。
比如说 :
A节点的最小级别节点就是a111 、a22、a333
a1节点的最小级别节点就是a111
a11节点的最小级别节点也是a111
a111节点的最小级别节点也是a111,也就是自身了

a3节点的最小级别节点就是a333
a33节点的最小级别节点也是a333
....
反正就是到最小级别的节点去;
得到的数据就装到Map中,如:
Map<id,List<childsId>> map
id是所有的id,List<childs>就是此id下所有最小级别的节点Id;

各位大虾,有知道的知道怎么写的么?灰常感谢了!!!!!高分求救哈!!!
...全文
318 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 3 楼 guozi110001 的回复:
我按照自己的理解写了一个方法,是在主id不重复的情况下适用的,你可以试试,希望对你有帮助

//首先是Childs类,可以自动忽略
public class Childs {
	private Integer id;
	private String name;
	private Integer partid;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getPartid() {
		return partid;
	}
	public void setPartid(Integer partid) {
		this.partid = partid;
	}
	public Childs(Integer id, String name, Integer partid) {
		super();
		this.id = id;
		this.name = name;
		this.partid = partid;
	}
	public Childs() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	
}


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import javax.swing.text.AsyncBoxView.ChildState;


public class TestDemo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		List<Childs> list = getList();
		Map<Integer, List<Childs>> map = new HashMap<Integer,List<Childs>>();
		//支持输入任意id可获得相应的结果(此处没有进行验证,请自觉输入1-9中的任意一位数)
		System.out.print("请输入要查询的id:");
		Scanner sc = new Scanner(System.in);
		int index = sc.nextInt();
		
		map.put(index, getMap(index, list, new ArrayList()));
		//将结果输出
		System.out.println("id\tname\tpartid");
		for(int i = 0;i<map.get(index).size();i++){
			Childs c = map.get(index).get(i);
			System.out.println(c.getId()+"\t"+c.getName()+"\t"+c.getPartid());
		}
	}
	
	/**
	 * 手动设工程一个符合条件的list
	 * @return
	 */
	public static List<Childs> getList(){
		List<Childs> list = new ArrayList<>();
		for(int i = 0;i<4;i++){
			int id = i+1;
			String name = "a";
			String a = "";
			for(int j = 0;j<i;j++){
				a += "1";
			}
			name += a;
			Integer partid;
			if(i==0)
				partid = null;
			else
				partid = i;
			Childs c = new Childs(id,name,partid);
			list.add(c);
		}
		Childs s1 = new Childs(5,"a2",1);
		Childs s2 = new Childs(6,"a22",5);
		Childs s3 = new Childs(7,"a3",1);
		Childs s4 = new Childs(8,"a33",7);
		Childs s5 = new Childs(9,"a333",8);
		list.add(s1);
		list.add(s2);
		list.add(s3);
		list.add(s4);
		list.add(s5);
//		System.out.println("id\tname\tpartid");
//		for(int i=0;i<list.size();i++){
//			Childs c = list.get(i);
//			System.out.println(c.getId()+"\t"+c.getName()+"\t"+c.getPartid());
//		}
		return list;
	}
	
	/**
	 * 传入相应的参数来求得该id下的所有最小级别节点
	 * @param id 要查询的节点id
	 * @param list 已知的所有节点集合
	 * @param result 要返回的所有最小级别节点集合
	 * @return 返回result
	 */
	public static List<Childs> getMap(Integer id,List<Childs> list,List<Childs> result){
		boolean flag = true;
		for(Childs c : list){
			if(c.getPartid()==id){
				flag = false;
				getMap(c.getId(),list,result);
			}
		}
		if(flag){
			for(Childs c : list){
				if(c.getId()==id)
					result.add(c);
			}
		}
		return result;
	}
}


好、我试下
  • 打赏
  • 举报
回复
引用 1 楼 guozi110001 的回复:
你的id为什么会有重复的呢?a2和a3、a22和a33的id是重复的,partid应该是该节点的父节点吧?如果是这样的话貌似不好判断
后面的id不重复的、当时复制的时候弄错了而已、 按理应该是1、2、3、、、5、6、7、8、9、10、、、的。
  • 打赏
  • 举报
回复
引用 2 楼 whos2002110 的回复:
[quote=引用 1 楼 guozi110001 的回复:] 你的id为什么会有重复的呢?a2和a3、a22和a33的id是重复的,partid应该是该节点的父节点吧?如果是这样的话貌似不好判断
就是[/quote] 试了下,就是需要这个效果,不错!
whos2002110 2014-09-05
  • 打赏
  • 举报
回复
引用 1 楼 guozi110001 的回复:
你的id为什么会有重复的呢?a2和a3、a22和a33的id是重复的,partid应该是该节点的父节点吧?如果是这样的话貌似不好判断
就是
guozi110001 2014-09-05
  • 打赏
  • 举报
回复
你的id为什么会有重复的呢?a2和a3、a22和a33的id是重复的,partid应该是该节点的父节点吧?如果是这样的话貌似不好判断
guozi110001 2014-09-05
  • 打赏
  • 举报
回复
我按照自己的理解写了一个方法,是在主id不重复的情况下适用的,你可以试试,希望对你有帮助

//首先是Childs类,可以自动忽略
public class Childs {
	private Integer id;
	private String name;
	private Integer partid;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getPartid() {
		return partid;
	}
	public void setPartid(Integer partid) {
		this.partid = partid;
	}
	public Childs(Integer id, String name, Integer partid) {
		super();
		this.id = id;
		this.name = name;
		this.partid = partid;
	}
	public Childs() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	
}


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import javax.swing.text.AsyncBoxView.ChildState;


public class TestDemo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		List<Childs> list = getList();
		Map<Integer, List<Childs>> map = new HashMap<Integer,List<Childs>>();
		//支持输入任意id可获得相应的结果(此处没有进行验证,请自觉输入1-9中的任意一位数)
		System.out.print("请输入要查询的id:");
		Scanner sc = new Scanner(System.in);
		int index = sc.nextInt();
		
		map.put(index, getMap(index, list, new ArrayList()));
		//将结果输出
		System.out.println("id\tname\tpartid");
		for(int i = 0;i<map.get(index).size();i++){
			Childs c = map.get(index).get(i);
			System.out.println(c.getId()+"\t"+c.getName()+"\t"+c.getPartid());
		}
	}
	
	/**
	 * 手动设工程一个符合条件的list
	 * @return
	 */
	public static List<Childs> getList(){
		List<Childs> list = new ArrayList<>();
		for(int i = 0;i<4;i++){
			int id = i+1;
			String name = "a";
			String a = "";
			for(int j = 0;j<i;j++){
				a += "1";
			}
			name += a;
			Integer partid;
			if(i==0)
				partid = null;
			else
				partid = i;
			Childs c = new Childs(id,name,partid);
			list.add(c);
		}
		Childs s1 = new Childs(5,"a2",1);
		Childs s2 = new Childs(6,"a22",5);
		Childs s3 = new Childs(7,"a3",1);
		Childs s4 = new Childs(8,"a33",7);
		Childs s5 = new Childs(9,"a333",8);
		list.add(s1);
		list.add(s2);
		list.add(s3);
		list.add(s4);
		list.add(s5);
//		System.out.println("id\tname\tpartid");
//		for(int i=0;i<list.size();i++){
//			Childs c = list.get(i);
//			System.out.println(c.getId()+"\t"+c.getName()+"\t"+c.getPartid());
//		}
		return list;
	}
	
	/**
	 * 传入相应的参数来求得该id下的所有最小级别节点
	 * @param id 要查询的节点id
	 * @param list 已知的所有节点集合
	 * @param result 要返回的所有最小级别节点集合
	 * @return 返回result
	 */
	public static List<Childs> getMap(Integer id,List<Childs> list,List<Childs> result){
		boolean flag = true;
		for(Childs c : list){
			if(c.getPartid()==id){
				flag = false;
				getMap(c.getId(),list,result);
			}
		}
		if(flag){
			for(Childs c : list){
				if(c.getId()==id)
					result.add(c);
			}
		}
		return result;
	}
}


67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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