Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.39 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Collection;
  3.  
  4. public class MutableNode<T extends Number> implements Node<T> {
  5.  
  6.     T value;
  7.     MutableNode<T> parent;
  8.     Collection<Node<T>> children;
  9.  
  10.     public MutableNode(T value, MutableNode<T> parent, Collection<Node<T>> children) {
  11.         this.value = value;
  12.         this.parent = parent;
  13.         this.children = new ArrayList<>();
  14.     }
  15.  
  16.     void setValue(T value) {
  17.         this.value = value;
  18.     }
  19.  
  20.     void setParent(MutableNode<T> parent) {
  21.         this.parent = parent;
  22.     }
  23.  
  24.     void setChildren(Collection<MutableNode<T>> children) {
  25.         for (int i = 0; i < children.size(); i++) {
  26.             this.addChild((MutableNode<T>) children.toArray()[i]);
  27.         }
  28.     }
  29.  
  30.     void addChild(MutableNode<T> child) {
  31.         this.children.add(child);
  32.     }
  33.  
  34.     void removeChild(MutableNode<T> child) {
  35.         if (parent != null) {
  36.             parent.children.remove(child);
  37.         }
  38.         //else {
  39.             //throw new Exception("Delete all tree");
  40.         //}
  41.     }
  42.  
  43.     @Override
  44.     public Node<T> getParent() {
  45.         return this.parent;
  46.     }
  47.  
  48.     @Override
  49.     public Collection<Node<T>> getChildren() {
  50.         return this.children;
  51.     }
  52.  
  53.     @Override
  54.     public void print(int indents) {
  55.         System.out.println(' ' * indents);
  56.         System.out.println(value.toString());
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement