fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. new Ideone();
  13. }
  14.  
  15. Ideone() {
  16. NodeArbre target = new NodeArbre("month", null, null);
  17. NodeArbre head = new NodeArbre("monday",
  18. new NodeArbre("party", new NodeArbre("of", new NodeArbre("friday", null, null), target),
  19. new NodeArbre("at", null, null)),
  20. new NodeArbre("brother", null, null));
  21. fa(head, target);
  22.  
  23.  
  24. }
  25.  
  26. public boolean fa(NodeArbre inicial, NodeArbre fi, List<NodeArbre> visited) {
  27. if (inicial == null)
  28. return false;
  29. visited.add(inicial);
  30. if (inicial == fi) {
  31. System.out.println(visited);
  32. return true;
  33. }
  34. if (fa(inicial._esq, fi, visited))
  35. return true;
  36. else if (fa(inicial._dret, fi, visited))
  37. return true;
  38. visited.remove(inicial);
  39. return false;
  40. }
  41. public void fa(NodeArbre inicial, NodeArbre fi) {
  42. // assign the list to a variable if you want to do something with it...
  43. // fa will return true if it found the path
  44. fa(inicial, fi, new LinkedList());
  45. }
  46. }
  47.  
  48. class NodeArbre {
  49. NodeArbre _esq;
  50. NodeArbre _dret;
  51. String contingut;
  52. NodeArbre(String contingut, NodeArbre _esq, NodeArbre _dret) {
  53. this.contingut = contingut;
  54. this._esq = _esq;
  55. this._dret = _dret;
  56. }
  57.  
  58. @Override
  59. public String toString() {
  60. return contingut;
  61. }
  62. }
Success #stdin #stdout 0.1s 320576KB
stdin
Standard input is empty
stdout
[monday, party, of, month]