Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.02 KB | None | 0 0
  1. package model;
  2.  
  3. import org.apache.commons.logging.Log;
  4. import org.apache.commons.logging.LogFactory;
  5.  
  6. import java.util.ArrayList;
  7. import java.util.List;
  8.  
  9. //
  10. // Autor:
  11. // Mateusz Lukaszenko, 96249
  12. //
  13. public class Node {
  14.  
  15.     private static Log log = LogFactory.getLog(Node.class);
  16.  
  17.     private String value; //zawiera operator lub staΕ‚Δ… lub zmienna
  18.     private List<Node> children = new ArrayList<>();
  19.     private Node parent;
  20.  
  21.     public Node() {
  22.     }
  23.  
  24.     public Node(String value, Node parent) {
  25.         this.value = value;
  26.         this.parent = parent;
  27.     }
  28.  
  29.     public void addChild(int index, Node node) {
  30.         this.getChildren().add(index, node);
  31.         node.setParent(this);
  32.     }
  33.  
  34.     public boolean hasParent() {
  35.         return (this.getParent() != null);
  36.     }
  37.  
  38.     public boolean hasChildren() {
  39.         return this.getChildren().size() > 0;
  40.     }
  41.  
  42.     public String getValue() {
  43.         return value;
  44.     }
  45.  
  46.     public void setValue(String value) {
  47.         this.value = value;
  48.     }
  49.  
  50.     public List<Node> getChildren() {
  51.         return children;
  52.     }
  53.  
  54.     public void setChildren(List<Node> children) {
  55.         this.children = children;
  56.     }
  57.  
  58.     public List<Node> getNodeWithAllOffspring() { //todo sprawdzic czy dizala
  59.         List<Node> nodesList = new ArrayList<>();
  60.         nodesList.add(this);
  61.         this.addChildrenToList(nodesList);
  62.         return nodesList;
  63.     }
  64.  
  65.     private List<Node> addChildrenToList(List<Node> nodes) {
  66.         if (this.hasChildren()) {
  67.             Node child0 = this.getChildren().get(0);
  68.             Node child1 = this.getChildren().get(1);
  69.             nodes.add(child0);
  70.             nodes.add(child1);
  71.             if (child0.hasChildren()) child0.addChildrenToList(nodes);
  72.             if (child1.hasChildren()) child1.addChildrenToList(nodes);
  73.         }
  74.         return nodes;
  75.     }
  76.  
  77.     public Node getParent() {
  78.         return parent;
  79.     }
  80.  
  81.     public void setParent(Node parent) {
  82.         this.parent = parent;
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement