展开全部
我自己用递归写了下,不知道能不能给你帮助:
测试类:
package tree;
import java.util.*;
public class Test {
public static void main(String[] args){
List<Tree> trees=new ArrayList<Tree>();
int id=1;
Tree tree1=new Tree(0,id++,"张三丰");
Tree tree2=new Tree(tree1.getId(),id++,"武当宋大侠宋远桥");
Tree tree3=new Tree(tree1.getId(),id++,"武当俞二侠俞莲舟");
Tree tree4=new Tree(tree1.getId(),id++,"武当俞三侠俞岱岩");
Tree tree5=new Tree(tree1.getId(),id++,"武当张四侠张松溪");
Tree tree6=new Tree(tree1.getId(),id++,"武当张五侠张翠山");
Tree tree7=new Tree(tree1.getId(),id++,"武当殷六侠殷梨亭");
Tree tree8=new Tree(tree1.getId(),id++,"武当莫七侠莫声谷");
Tree tree9=new Tree(tree6.getId(),id++,"明教张无忌");
Tree tree13=new Tree(tree2.getId(),id++,"叛徒宋青书");
Tree tree10=new Tree(0,id++,"任我行");
Tree tree11=new Tree(tree10.getId(),id++,"令狐冲");
Tree tree12=new Tree(tree10.getId(),id++,"任盈盈");
trees.add(tree1);
trees.add(tree2);
trees.add(tree3);
trees.add(tree4);
trees.add(tree5);
trees.add(tree6);
trees.add(tree7);
trees.add(tree8);
trees.add(tree9);
trees.add(tree10);
trees.add(tree11);
trees.add(tree12);
trees.add(tree13);
for(int i=0;i<trees.size();i++){
Tree tree=trees.get(i);
if(tree.getParentId()==0){
tree.showChildTree(trees);
}
}
}
}
树类:
package tree;
import java.util.List;
public class Tree {
private int parentId;
private int id;
private String showStr;
private String Spaces="";
public Tree() {
// TODO Auto-generated constructor stub
}
public Tree(int parentId,int id,String showStr){
this.parentId=parentId;
this.id=id;
this.showStr=showStr;
}
public void showChildTree(List<Tree> trees){
if(parentId!=0){
trees.get(id-1).setSpaces(trees.get(parentId-1).getSpaces()+" ");
}
System.out.println(trees.get(id-1).getSpaces()+showStr);
for(int i=0;i<trees.size();i++){
Tree tree=trees.get(i);
if(tree.getParentId()==id){
tree.showChildTree(trees);
}
}
}
public int getParentId() {
return parentId;
}
public void setParentId(int parentId) {
this.parentId = parentId;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getShowStr() {
return showStr;
}
public void setShowStr(String showStr) {
this.showStr = showStr;
}
public String getSpaces() {
return Spaces;
}
public void setSpaces(String spaces) {
Spaces = spaces;
}
}
效果图:
张三丰
武当宋大侠宋远桥
叛徒宋青书
武当俞二侠俞莲舟
武当俞三侠俞岱岩
武当张四侠张松溪
武当张五侠张翠山
明教张无忌
武当殷六侠殷梨亭
武当莫七侠莫声谷
任我行
令狐冲
任盈盈
测试类:
package tree;
import java.util.*;
public class Test {
public static void main(String[] args){
List<Tree> trees=new ArrayList<Tree>();
int id=1;
Tree tree1=new Tree(0,id++,"张三丰");
Tree tree2=new Tree(tree1.getId(),id++,"武当宋大侠宋远桥");
Tree tree3=new Tree(tree1.getId(),id++,"武当俞二侠俞莲舟");
Tree tree4=new Tree(tree1.getId(),id++,"武当俞三侠俞岱岩");
Tree tree5=new Tree(tree1.getId(),id++,"武当张四侠张松溪");
Tree tree6=new Tree(tree1.getId(),id++,"武当张五侠张翠山");
Tree tree7=new Tree(tree1.getId(),id++,"武当殷六侠殷梨亭");
Tree tree8=new Tree(tree1.getId(),id++,"武当莫七侠莫声谷");
Tree tree9=new Tree(tree6.getId(),id++,"明教张无忌");
Tree tree13=new Tree(tree2.getId(),id++,"叛徒宋青书");
Tree tree10=new Tree(0,id++,"任我行");
Tree tree11=new Tree(tree10.getId(),id++,"令狐冲");
Tree tree12=new Tree(tree10.getId(),id++,"任盈盈");
trees.add(tree1);
trees.add(tree2);
trees.add(tree3);
trees.add(tree4);
trees.add(tree5);
trees.add(tree6);
trees.add(tree7);
trees.add(tree8);
trees.add(tree9);
trees.add(tree10);
trees.add(tree11);
trees.add(tree12);
trees.add(tree13);
for(int i=0;i<trees.size();i++){
Tree tree=trees.get(i);
if(tree.getParentId()==0){
tree.showChildTree(trees);
}
}
}
}
树类:
package tree;
import java.util.List;
public class Tree {
private int parentId;
private int id;
private String showStr;
private String Spaces="";
public Tree() {
// TODO Auto-generated constructor stub
}
public Tree(int parentId,int id,String showStr){
this.parentId=parentId;
this.id=id;
this.showStr=showStr;
}
public void showChildTree(List<Tree> trees){
if(parentId!=0){
trees.get(id-1).setSpaces(trees.get(parentId-1).getSpaces()+" ");
}
System.out.println(trees.get(id-1).getSpaces()+showStr);
for(int i=0;i<trees.size();i++){
Tree tree=trees.get(i);
if(tree.getParentId()==id){
tree.showChildTree(trees);
}
}
}
public int getParentId() {
return parentId;
}
public void setParentId(int parentId) {
this.parentId = parentId;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getShowStr() {
return showStr;
}
public void setShowStr(String showStr) {
this.showStr = showStr;
}
public String getSpaces() {
return Spaces;
}
public void setSpaces(String spaces) {
Spaces = spaces;
}
}
效果图:
张三丰
武当宋大侠宋远桥
叛徒宋青书
武当俞二侠俞莲舟
武当俞三侠俞岱岩
武当张四侠张松溪
武当张五侠张翠山
明教张无忌
武当殷六侠殷梨亭
武当莫七侠莫声谷
任我行
令狐冲
任盈盈
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询