From 58e8aa9cd06b07c19f954cefbb78fd338667cd39 Mon Sep 17 00:00:00 2001 From: birkn Date: Sun, 24 Mar 2019 13:15:33 +0100 Subject: [PATCH] Changes for more abstraction --- src/Main.java | 31 ------ src/Node.java | 102 ------------------ .../BreadthFirstSearch.java | 7 +- src/search/breadthfirstsearch/Main.java | 8 ++ src/search/breadthfirstsearch/Node.java | 9 ++ 5 files changed, 21 insertions(+), 136 deletions(-) delete mode 100644 src/Main.java delete mode 100644 src/Node.java rename src/{ => search/breadthfirstsearch}/BreadthFirstSearch.java (76%) create mode 100644 src/search/breadthfirstsearch/Main.java create mode 100644 src/search/breadthfirstsearch/Node.java diff --git a/src/Main.java b/src/Main.java deleted file mode 100644 index 661c941..0000000 --- a/src/Main.java +++ /dev/null @@ -1,31 +0,0 @@ -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 deleted file mode 100644 index 6e09b00..0000000 --- a/src/Node.java +++ /dev/null @@ -1,102 +0,0 @@ -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(); - } -} diff --git a/src/BreadthFirstSearch.java b/src/search/breadthfirstsearch/BreadthFirstSearch.java similarity index 76% rename from src/BreadthFirstSearch.java rename to src/search/breadthfirstsearch/BreadthFirstSearch.java index 072c991..ba2cdf1 100644 --- a/src/BreadthFirstSearch.java +++ b/src/search/breadthfirstsearch/BreadthFirstSearch.java @@ -1,6 +1,7 @@ +package search.breadthfirstsearch; + import java.util.ArrayList; import java.util.List; -import java.util.function.Predicate; public class BreadthFirstSearch { @@ -10,12 +11,12 @@ public class BreadthFirstSearch for (Node node : nodes) { - if (target.equals(node)) + if (node.isTargetReached(target)) { return node; } - newNodes.addAll(node.getChildren()); + newNodes.addAll(node.generateSuccessors()); } if (newNodes.isEmpty()) diff --git a/src/search/breadthfirstsearch/Main.java b/src/search/breadthfirstsearch/Main.java new file mode 100644 index 0000000..f74021c --- /dev/null +++ b/src/search/breadthfirstsearch/Main.java @@ -0,0 +1,8 @@ +package search.breadthfirstsearch; + +public class Main +{ + public static void main(String[] args) + { + } +} diff --git a/src/search/breadthfirstsearch/Node.java b/src/search/breadthfirstsearch/Node.java new file mode 100644 index 0000000..b2bf3e9 --- /dev/null +++ b/src/search/breadthfirstsearch/Node.java @@ -0,0 +1,9 @@ +package search.breadthfirstsearch; + +import java.util.List; + +public interface Node +{ + boolean isTargetReached(Node target); + List generateSuccessors(); +}