added lab16 ex3

This commit is contained in:
Louis Heredero 2024-11-18 12:46:03 +01:00
parent 5220b27146
commit 4af29e8f0f
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
7 changed files with 231 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -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<Component> newSuccessors) {
successors.addAll(newSuccessors);
}
}

View File

@ -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<Component> newSuccessors) {
successors.addAll(0, newSuccessors);
}
}

View File

@ -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<Component> 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<Component> newSuccessors);
}

View File

@ -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<Component> getChildren() {
throw new UnsupportedOperationException();
}
}

View File

@ -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<Component> children;
public Composite(String n) {
name = n;
children = new ArrayList<Component>();
}
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<Component> getChildren() {
return children;
}
@Override
public String toString() {
String temp = "Composite " + name;
for (Component n : this.children) {
temp = temp + n.toString();
}
return temp;
}
}

View File

@ -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<Component> getChildren() {
return new ArrayList<Component>();
}
}