commit 600cc73df61b7c32de398b11739733746f6999da Author: Niklas Birk Date: Sun Mar 24 11:58:30 2019 +0100 Initial Setup diff --git a/src/BreadthFirstSearch.java b/src/BreadthFirstSearch.java new file mode 100644 index 0000000..072c991 --- /dev/null +++ b/src/BreadthFirstSearch.java @@ -0,0 +1,28 @@ +import java.util.ArrayList; +import java.util.List; +import java.util.function.Predicate; + +public class BreadthFirstSearch +{ + public Node breadthFirstSearch(List nodes, Node target) + { + var newNodes = new ArrayList(); + + for (Node node : nodes) + { + if (target.equals(node)) + { + return node; + } + + newNodes.addAll(node.getChildren()); + } + + if (newNodes.isEmpty()) + { + return breadthFirstSearch(newNodes, target); + } + + return null; + } +} diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..661c941 --- /dev/null +++ b/src/Main.java @@ -0,0 +1,31 @@ +import java.util.List; + +public class Main +{ + public static void main(String[] args) + { + var root = new Node(0); + + var childOne = new Node(1); + var childTwo = new Node(2); + var childThree = new Node(3); + + var childrenLayer1 = List.of(childOne, childTwo, childThree); + root.addChildren(childrenLayer1); + + var childFour = new Node(4); + var childFive = new Node(5); + + var children2 = List.of(childFour, childFive); + childOne.addChildren(children2); + + var childSix = new Node(6); + var children3 = List.of(childSix); + childTwo.addChildren(children3); + + System.out.println(root); + + + System.out.println(new BreadthFirstSearch().breadthFirstSearch(List.of(root), new Node(2))); + } +} diff --git a/src/Node.java b/src/Node.java new file mode 100644 index 0000000..6e09b00 --- /dev/null +++ b/src/Node.java @@ -0,0 +1,102 @@ +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +public class Node +{ + private Object value; + + private Node parent; + + private List children; + + public Node(Object value) + { + this(value, new ArrayList<>()); + } + + public Node(Object value, List children) + { + this.value = value; + this.children = children; + + for (Node child : children) + { + child.setParent(this); + } + } + + public void addChildren(List children) + { + this.children.addAll(children); + } + + public void addChild(Node child) + { + this.children.add(child); + } + + public void removeChild(Node child) + { + this.children.remove(child); + } + + public Object getValue() + { + return value; + } + + public void setParent(Node parent) + { + this.parent = parent; + } + + public Node getParent() + { + return parent; + } + + public List getChildren() + { + return children; + } + + @Override + public boolean equals(Object o) + { + if (this == o) + { + return true; + } + if (o == null || getClass() != o.getClass()) + { + return false; + } + Node node = (Node) o; + return value.equals(node.value); + } + + @Override + public int hashCode() + { + return Objects.hash(value); + } + + @Override + public String toString() + { + StringBuilder builder = new StringBuilder(); + + for (Node child : children) + { + builder.append(this.value).append(" -> ").append(child.getValue()).append("\n"); + } + + for (Node child : children) + { + builder.append(child); + } + + return builder.toString(); + } +}