Created
August 3, 2010 10:48
-
-
Save mklabs/506176 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import java.util.ArrayList; | |
import java.util.Iterator; | |
import java.util.List; | |
public class Test { | |
public static void main(final String[] args) { | |
Test t = new Test(); | |
A a = t.init(); | |
t.recFunction(a, 0); | |
} | |
public boolean recFunction(final A element, final int level) { | |
boolean result = false; | |
System.out.println("PARSING: " + element.getId()); | |
if (level == 2) { | |
result = true; | |
} | |
if (element.getChildren() != null) { | |
for (Iterator<A> iterator = element.getChildren().iterator(); iterator.hasNext();) { | |
A child = iterator.next(); | |
System.out.println("CHILD:" + child.getId()); | |
boolean tmp = recFunction(child, level + 1); | |
System.out.println("Result for :" + child.getId() + " is " + tmp); | |
//result = result || recFunction(child, level + 1); | |
} | |
} | |
return result; | |
} | |
public A init() { | |
/* | |
* a |_ 1a |_ 2a-a [_ 2a-b |_ 1b |_ 2b-a |_ 2b-b | |
*/ | |
A a = new A("a"); | |
List<A> list = new ArrayList<A>(); | |
A son1a = new A("1a"); | |
List<A> subList = new ArrayList<A>(); | |
subList.add(new A("2a-a")); | |
subList.add(new A("2a-b")); | |
son1a.setChildren(subList); | |
A son1b = new A("1b"); | |
List<A> subList2 = new ArrayList<A>(); | |
subList2.add(new A("2b-a")); | |
subList2.add(new A("2b-b")); | |
son1b.setChildren(subList2); | |
list.add(son1a); | |
list.add(son1b); | |
a.setChildren(list); | |
return a; | |
} | |
public class A { | |
private List<A> children; | |
private String id; | |
public A(final String id) { | |
setId(id); | |
} | |
public String getId() { | |
return id; | |
} | |
public void setId(final String id) { | |
this.id = id; | |
} | |
public List<A> getChildren() { | |
return children; | |
} | |
public void setChildren(final List<A> children) { | |
this.children = children; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sur l'instruction result = result || recFunction(child, level + 1); arrive un moment ou la variable result vaut true, donc cassant la récursivité au premier élement identifié de level 2.