Node

Home   »   Node

public class Node{

    private E element;
    private Node parent;
    private List> children;

    public Node(E element, Node parent) {
        this.element = element;
        this.parent = parent;
        children = new LinkedList<>();
    }

    public E getElement() {
        return element;
    }

    public Node getParent() {
        return parent;
    }


    public void setElement(E element) {
        this.element = element;
    }

    public void setParent(Node parent) {
        this.parent = parent;
    }

    public void setChildren(List> children) {
        this.children = children;
    }

    public List> getChildren() {
        return this.children;
    }

    public void removeChild(Node child) {
        this.children.remove(child);
    }

    public void addChild(Node child) {
        // make sure not adding self
        if (child != this) {
            this.children.add(child);
        }
    }

    public boolean hasChildren() {
        return this.children != null && !this.children.isEmpty();
    }

}

Leave a Reply

Your email address will not be published. Required fields are marked *