BinaryDecompositionTree.java 2.6 KB
Newer Older
1 2
package mvd.jester.utils;

3

4 5 6 7 8 9 10 11 12 13 14 15
public class BinaryDecompositionTree<N> {

    private Node<N> root;

    public BinaryDecompositionTree() {
    }

    public Node<N> getRootNode() {
        return root;
    }

    public boolean contains(N object) {
16 17 18
        if (root == null) {
            return false;
        }
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
        return root.contains(object);
    }

    public boolean isEmpty() {
        return root == null;
    }

    public void setRoot(Node<N> root) {
        this.root = root;
    }

    public static class Node<T> {
        private final Node<T> parentNode;
        private Node<T> leftNode;
        private Node<T> rightNode;
        private NodeType nodeType;
        private T object;

        public Node(Node<T> parentNode, NodeType nodeType) {
            this.parentNode = parentNode;
            this.nodeType = nodeType;
        }

        public boolean contains(T object) {
            if (nodeType.equals(NodeType.LEAF)) {
                return this.object == object;
            } else {
                boolean leftContains = false;
                boolean rightContains = false;
                if (leftNode != null) {
                    leftContains = leftNode.contains(object);
                }
                if (rightNode != null) {
                    rightContains = rightNode.contains(object);
                }
                return leftContains || rightContains;
            }
        }

        public Node(Node<T> parentNode, T object) {
            this.parentNode = parentNode;
            this.nodeType = NodeType.LEAF;
            this.object = object;
        }

        /**
         * @return the parentNode
         */
        public Node<T> getParentNode() {
            return parentNode;
        }

        public NodeType getNodeType() {
            return nodeType;
        }

        public T getObject() {
            return object;
        }

        /**
         * @param leftNode the leftNode to set
         */
82
        public Node<T> setLeftNode(Node<T> leftNode) {
83
            this.leftNode = leftNode;
84
            return this.leftNode;
85 86 87 88 89 90 91 92 93 94 95 96
        }

        /**
         * @return the leftNode
         */
        public Node<T> getLeftNode() {
            return leftNode;
        }

        /**
         * @param rightNode the rightNode to set
         */
97
        public Node<T> setRightNode(Node<T> rightNode) {
98
            this.rightNode = rightNode;
99
            return this.rightNode;
100 101 102 103 104 105 106 107 108 109 110 111 112 113
        }

        /**
         * @return the rightNode
         */
        public Node<T> getRightNode() {
            return rightNode;
        }
    }

    public enum NodeType {
        SEQ, PAR, LEAF
    }
}