Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Collection;
- public class MutableNode<T extends Number> implements Node<T> {
- T value;
- MutableNode<T> parent;
- Collection<Node<T>> children;
- public MutableNode(T value, MutableNode<T> parent, Collection<Node<T>> children) {
- this.value = value;
- this.parent = parent;
- this.children = new ArrayList<>();
- }
- void setValue(T value) {
- this.value = value;
- }
- void setParent(MutableNode<T> parent) {
- this.parent = parent;
- }
- void setChildren(Collection<MutableNode<T>> children) {
- for (int i = 0; i < children.size(); i++) {
- this.addChild((MutableNode<T>) children.toArray()[i]);
- }
- }
- void addChild(MutableNode<T> child) {
- this.children.add(child);
- }
- void removeChild(MutableNode<T> child) {
- if (parent != null) {
- parent.children.remove(child);
- }
- //else {
- //throw new Exception("Delete all tree");
- //}
- }
- @Override
- public Node<T> getParent() {
- return this.parent;
- }
- @Override
- public Collection<Node<T>> getChildren() {
- return this.children;
- }
- @Override
- public void print(int indents) {
- System.out.println(' ' * indents);
- System.out.println(value.toString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement