使用递归会有栈溢出的问题,这个时候需要把递归改为循环。以下代码演示了递归改循环的方法。并且最后还附加了复制树结构的案例(将Person的树结构,复制为Human树结构)。
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
class Main {
public static void main(String[] args) {
// 构建 原始数据
Person rootPerson = parsePerson();
// 1. 常用递归
System.out.println("------------------------- 1. 常用递归");
printPerson(rootPerson);
// 2. 递归改循环(广度优先)(避免爆栈)
System.out.println("------------------------- 2. 递归改循环(广度优先)(避免爆栈)");
printPerson2(rootPerson);
// 3. 递归改循环(深度优先)(避免爆栈)
System.out.println("------------------------- 3. 递归改循环(深度优先)(避免爆栈)");
printPerson3(rootPerson);
// 4. 去除属性生成新的树状图对象(递归)
System.out.println("------------------------- 4. 去除属性生成新的树状图对象(递归)");
Human human = printPerson4(rootPerson);
// print human string ok
// 5. 去除属性生成新的树状图对象(循环)
System.out.println("------------------------- 5. 去除属性生成新的树状图对象(循环)");
createHumanTree(rootPerson);
}
private static void printPerson(Person person) {
System.out.println(person.getName());
if (person.getChild() != null && person.getChild().size() != 0) {
for (int i = 0; i < person.getChild().size(); i++) {
printPerson(person.getChild().get(i));
}
}
}
private static void printPerson2(Person rooPerson) {
List<Person> personList = new ArrayList<>();
personList.add(rooPerson);
while (personList.size() != 0) {
Person person = personList.remove(0);
if (person == null) continue;
System.out.println(person.getName());
if (person.getChild() != null && person.getChild().size() != 0) {
personList.addAll(person.getChild());
}
}
}
/**
* 去除属性生成新的树状图对象(循环)
*
* @param rooPerson
* @return
*/
private static Human createHumanTree(Person rooPerson) {
List<Person> personList = new ArrayList<>();
List<Human> humanList = new ArrayList<>();
Human rootHuman = new Human();
personList.add(rooPerson);
humanList.add(rootHuman);
while (personList.size() != 0) {
Person person = personList.remove(0);
Human human = humanList.remove(0);
if (person == null) continue;
human.setName(person.getName());
//System.out.println(person.getName());
if (person.getChild() != null && person.getChild().size() != 0) {
personList.addAll(person.getChild());
List list = new ArrayList();
for (int i = 0; i < person.getChild().size(); i++) {
Human h = new Human();
list.add(h);
}
humanList.addAll(list);
human.setChild(list);
}
}
return rootHuman;
}
private static void printPerson3(Person rooPerson) {
Stack<Person> stack = new Stack<>();
stack.push(rooPerson);
while (!stack.isEmpty()) {
Person person = stack.pop();
System.out.println(person.getName());
if (person.getChild() != null && person.getChild().size() != 0) {
for (int i = person.getChild().size() - 1; i >= 0; i--) {
stack.push(person.getChild().get(i));
}
}
}
}
private static Human printPerson4(Person rooPerson) {
Human rootHuman = new Human();
rootHuman.setName(rooPerson.getName());
if (rooPerson.getChild() != null && rooPerson.getChild().size() != 0) {
List<Human> humanList = new ArrayList<>();
for (int i = 0; i < rooPerson.getChild().size(); i++) {
humanList.add(printPerson4(rooPerson.getChild().get(i)));
}
rootHuman.setChild(humanList);
}
return rootHuman;
}
// ---------------------------------------------------------------------------------------------
//
// 构建原始对象
//
// ---------------------------------------------------------------------------------------------
private static Person parsePerson() {
// 参考 json
String textJson = "{\"index\":1,\"name\":\"1\",\"child\":[{\"index\":2,\"name\":\"2\",\"child\":[{\"index\":4,\"name\":\"4\",\"child\":[]},{\"index\":5,\"name\":\"5\",\"child\":[]}]},{\"index\":3,\"name\":\"3\",\"child\":[{\"index\":6,\"name\":\"6\",\"child\":[]},{\"index\":7,\"name\":\"7\",\"child\":[]}]}]}";
Person person7 = new Person();
person7.setIndex(7);
person7.setName("7");
person7.setChild(new ArrayList<Person>());
Person person6 = new Person();
person6.setIndex(6);
person6.setName("6");
person6.setChild(new ArrayList<Person>());
Person person5 = new Person();
person5.setIndex(5);
person5.setName("5");
person5.setChild(new ArrayList<Person>());
Person person4 = new Person();
person4.setIndex(4);
person4.setName("4");
person4.setChild(new ArrayList<Person>());
List<Person> childList3 = new ArrayList<>();
childList3.add(person6);
childList3.add(person7);
Person person3 = new Person();
person3.setIndex(3);
person3.setName("3");
person3.setChild(childList3);
List<Person> childList2 = new ArrayList<>();
childList2.add(person4);
childList2.add(person5);
Person person2 = new Person();
person2.setIndex(2);
person2.setName("2");
person2.setChild(childList2);
List<Person> childList1 = new ArrayList<>();
childList1.add(person2);
childList1.add(person3);
Person person1 = new Person();
person1.setIndex(1);
person1.setName("1");
person1.setChild(childList1);
return person1;
}
// ---------------------------------------------------------------------------------------------
//
// Person 、 Human 两个实体类
//
// ---------------------------------------------------------------------------------------------
public static class Person {
private int index;
private String name;
private List<Person> child;
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Person> getChild() {
return child;
}
public void setChild(List<Person> child) {
this.child = child;
}
}
public static class Human {
private String name;
private List<Human> child;
public Human() {
}
public Human(String name, List<Human> child) {
this.name = name;
this.child = child;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Human> getChild() {
return child;
}
public void setChild(List<Human> child) {
this.child = child;
}
}
}