diff --git a/src/lab16_composite/ex3/TestTraversalAlgorithms.java b/src/lab16_composite/ex3/TestTraversalAlgorithms.java new file mode 100644 index 0000000..4bb8e8c --- /dev/null +++ b/src/lab16_composite/ex3/TestTraversalAlgorithms.java @@ -0,0 +1,74 @@ +package lab16_composite.ex3; + +import lab16_composite.ex3.algorithm.BFSSearch; +import lab16_composite.ex3.algorithm.DFSSearch; +import lab16_composite.ex3.algorithm.TraversalAlgorithm; +import lab16_composite.ex3.component.Component; +import lab16_composite.ex3.composite.Composite; +import lab16_composite.ex3.leaf.Leaf; + +public class TestTraversalAlgorithms { + public static void main(String[] args) { + TestTraversalAlgorithms launcher = new TestTraversalAlgorithms(); + launcher.launch(); + } + + public void launch() { + + Component treeRoot = this.createTreeStruct(); + + //Instanciating one searchAlgoritm of each type. + TraversalAlgorithm dfs = new DFSSearch(); + TraversalAlgorithm bfs = new BFSSearch(); + + //Launching DFS search for one existing element. + Component result = dfs.search(treeRoot, "Leaf 5"); + System.out.println("1 element found by DFS: " + result); + + //Launching BFS search for one existing element. + result = bfs.search(treeRoot, "Leaf 5"); + System.out.println("1 element found by BFS : " + result); + } + + //1. We create the tree. + // node1 + // / \ + // node2 node3 + // / \ / \ + // l1 n4 n5 n6 + // /\ \ / \ + // l2 l3 l4 l5 l6 + // + //Creating the leafs. + private Component createTreeStruct() { + Leaf l1 = new Leaf("Leaf 1"); + Leaf l2 = new Leaf("Leaf 2"); + Leaf l3 = new Leaf("Leaf 3"); + Leaf l4 = new Leaf("Leaf 4"); + Leaf l5 = new Leaf("Leaf 5"); + Leaf l6 = new Leaf("Leaf 6"); + + //Creating the nodes + Composite n1 = new Composite("Node 1"); + Composite n2 = new Composite("Node 2"); + Composite n3 = new Composite("Node 3"); + Composite n4 = new Composite("Node 4"); + Composite n5 = new Composite("Node 5"); + Composite n6 = new Composite("Node 6"); + + //Assigning leaf and nodes to parents (nodes). + n6.add(l5); + n6.add(l6); + n5.add(l4); + n4.add(l2); + n4.add(l3); + n3.add(n5); + n3.add(n6); + n2.add(l1); + n2.add(n4); + n1.add(n2); + n1.add(n3); + + return n1; + } +} diff --git a/src/lab16_composite/ex3/algorithm/BFSSearch.java b/src/lab16_composite/ex3/algorithm/BFSSearch.java new file mode 100644 index 0000000..4073cab --- /dev/null +++ b/src/lab16_composite/ex3/algorithm/BFSSearch.java @@ -0,0 +1,13 @@ +package lab16_composite.ex3.algorithm; + +import lab16_composite.ex3.component.Component; + +import java.util.ArrayList; + +public class BFSSearch extends TraversalAlgorithm { + + @Override + protected void addSuccessor(ArrayList newSuccessors) { + successors.addAll(newSuccessors); + } +} diff --git a/src/lab16_composite/ex3/algorithm/DFSSearch.java b/src/lab16_composite/ex3/algorithm/DFSSearch.java new file mode 100644 index 0000000..38d12eb --- /dev/null +++ b/src/lab16_composite/ex3/algorithm/DFSSearch.java @@ -0,0 +1,12 @@ +package lab16_composite.ex3.algorithm; + +import lab16_composite.ex3.component.Component; + +import java.util.ArrayList; + +public class DFSSearch extends TraversalAlgorithm { + @Override + protected void addSuccessor(ArrayList newSuccessors) { + successors.addAll(0, newSuccessors); + } +} diff --git a/src/lab16_composite/ex3/algorithm/TraversalAlgorithm.java b/src/lab16_composite/ex3/algorithm/TraversalAlgorithm.java new file mode 100644 index 0000000..7f0d923 --- /dev/null +++ b/src/lab16_composite/ex3/algorithm/TraversalAlgorithm.java @@ -0,0 +1,27 @@ +package lab16_composite.ex3.algorithm; + +import lab16_composite.ex3.component.Component; + +import java.util.ArrayList; +import java.util.List; + +public abstract class TraversalAlgorithm { + protected List successors = new ArrayList<>(); + + public Component search(Component root, String target) { + successors = new ArrayList<>(); + successors.add(root); + while (true) { + if (successors.isEmpty()) { + return null; + } + Component component = successors.removeFirst(); + if (component.getName().equals(target)) { + return component; + } + addSuccessor(component.getChildren()); + } + } + + protected abstract void addSuccessor(ArrayList newSuccessors); +} diff --git a/src/lab16_composite/ex3/component/Component.java b/src/lab16_composite/ex3/component/Component.java new file mode 100644 index 0000000..4a1a8c9 --- /dev/null +++ b/src/lab16_composite/ex3/component/Component.java @@ -0,0 +1,23 @@ +package lab16_composite.ex3.component; + +import java.util.ArrayList; + +public abstract class Component { + public abstract String getName(); + + public void add(Component c) { + throw new UnsupportedOperationException(); + } + + public void remove(Component c) { + throw new UnsupportedOperationException(); + } + + public Component getChild(int i) { + throw new UnsupportedOperationException(); + } + + public ArrayList getChildren() { + throw new UnsupportedOperationException(); + } +} diff --git a/src/lab16_composite/ex3/composite/Composite.java b/src/lab16_composite/ex3/composite/Composite.java new file mode 100644 index 0000000..4c1da92 --- /dev/null +++ b/src/lab16_composite/ex3/composite/Composite.java @@ -0,0 +1,52 @@ +package lab16_composite.ex3.composite; + +import lab16_composite.ex3.component.Component; + +import java.util.ArrayList; + +public class Composite extends Component { + private String name; + private final ArrayList children; + + public Composite(String n) { + name = n; + children = new ArrayList(); + } + + public void setName(String n) { + name = n; + } + + public String getName() { + return name; + } + + @Override + public void add(Component c) { + children.add(c); + } + + @Override + public void remove(Component c) { + children.remove(c); + } + + @Override + public Component getChild(int i) { + return children.get(i); + } + + @Override + public ArrayList getChildren() { + return children; + } + + @Override + public String toString() { + String temp = "Composite " + name; + for (Component n : this.children) { + temp = temp + n.toString(); + } + return temp; + } +} diff --git a/src/lab16_composite/ex3/leaf/Leaf.java b/src/lab16_composite/ex3/leaf/Leaf.java new file mode 100644 index 0000000..f08d3be --- /dev/null +++ b/src/lab16_composite/ex3/leaf/Leaf.java @@ -0,0 +1,30 @@ +package lab16_composite.ex3.leaf; + +import lab16_composite.ex3.component.Component; + +import java.util.ArrayList; + +public class Leaf extends Component { + //Attributes + + private final String name; + + //Constructor + public Leaf(String n) { + name = n; + } + + public String getName() { + return name; + } + + @Override + public String toString() { + return "Leaf " + name; + } + + @Override + public ArrayList getChildren() { + return new ArrayList(); + } +}