Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- /**
- * Created by Bakytzhan_Manaspayev on 2/20/2017.
- */
- public class Node {
- String id;
- ArrayList<Node> childs = new ArrayList<Node>();
- public Node(String id) {
- this.id = id;
- }
- Node addChild(Node child) {
- childs.add(child);
- return this;
- }
- Node find(String id) {
- if (this.id.equals(id)) {
- return this;
- }
- for (int i = 0; i < childs.size(); i++) {
- Node node = childs.get(i).find(id);
- if (node != null) {
- return node;
- }
- }
- return null;
- }
- }
- public class Main {
- public static void main(String[] args) {
- /*
- 1
- / \
- 1_1 1_2
- / \
- 1_1_1 1_1_2
- */
- Node root = new Node("1");
- Node child_1_1 = new Node("1_1");
- Node child_1_2 = new Node("1_2");
- root.addChild(child_1_1).addChild(child_1_2);
- Node child_1_1_1 = new Node("1_1_1");
- Node child_1_1_2 = new Node("1_1_2");
- child_1_1.addChild(child_1_1_1).addChild(child_1_1_2);
- //finding node, via visitor pattern
- System.out.println(root.find("1_1_2").id);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement