Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. /**
  4. * Created by Bakytzhan_Manaspayev on 2/20/2017.
  5. */
  6. public class Node {
  7. String id;
  8. ArrayList<Node> childs = new ArrayList<Node>();
  9.  
  10. public Node(String id) {
  11. this.id = id;
  12. }
  13.  
  14. Node addChild(Node child) {
  15. childs.add(child);
  16. return this;
  17. }
  18.  
  19. Node find(String id) {
  20. if (this.id.equals(id)) {
  21. return this;
  22. }
  23.  
  24. for (int i = 0; i < childs.size(); i++) {
  25. Node node = childs.get(i).find(id);
  26. if (node != null) {
  27. return node;
  28. }
  29. }
  30. return null;
  31. }
  32.  
  33. }
  34.  
  35.  
  36.  
  37. public class Main {
  38. public static void main(String[] args) {
  39.  
  40. /*
  41.  
  42. 1
  43. / \
  44. 1_1 1_2
  45. / \
  46. 1_1_1 1_1_2
  47.  
  48.  
  49. */
  50.  
  51. Node root = new Node("1");
  52. Node child_1_1 = new Node("1_1");
  53. Node child_1_2 = new Node("1_2");
  54. root.addChild(child_1_1).addChild(child_1_2);
  55.  
  56. Node child_1_1_1 = new Node("1_1_1");
  57. Node child_1_1_2 = new Node("1_1_2");
  58. child_1_1.addChild(child_1_1_1).addChild(child_1_1_2);
  59.  
  60. //finding node, via visitor pattern
  61. System.out.println(root.find("1_1_2").id);
  62.  
  63.  
  64.  
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement