Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package model;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import java.util.ArrayList;
- import java.util.List;
- //
- // Autor:
- // Mateusz Lukaszenko, 96249
- //
- public class Node {
- private static Log log = LogFactory.getLog(Node.class);
- private String value; //zawiera operator lub staΕΔ lub zmienna
- private List<Node> children = new ArrayList<>();
- private Node parent;
- public Node() {
- }
- public Node(String value, Node parent) {
- this.value = value;
- this.parent = parent;
- }
- public void addChild(int index, Node node) {
- this.getChildren().add(index, node);
- node.setParent(this);
- }
- public boolean hasParent() {
- return (this.getParent() != null);
- }
- public boolean hasChildren() {
- return this.getChildren().size() > 0;
- }
- public String getValue() {
- return value;
- }
- public void setValue(String value) {
- this.value = value;
- }
- public List<Node> getChildren() {
- return children;
- }
- public void setChildren(List<Node> children) {
- this.children = children;
- }
- public List<Node> getNodeWithAllOffspring() { //todo sprawdzic czy dizala
- List<Node> nodesList = new ArrayList<>();
- nodesList.add(this);
- this.addChildrenToList(nodesList);
- return nodesList;
- }
- private List<Node> addChildrenToList(List<Node> nodes) {
- if (this.hasChildren()) {
- Node child0 = this.getChildren().get(0);
- Node child1 = this.getChildren().get(1);
- nodes.add(child0);
- nodes.add(child1);
- if (child0.hasChildren()) child0.addChildrenToList(nodes);
- if (child1.hasChildren()) child1.addChildrenToList(nodes);
- }
- return nodes;
- }
- public Node getParent() {
- return parent;
- }
- public void setParent(Node parent) {
- this.parent = parent;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement