BinaryDecompositionTree.java 2.47 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
package mvd.jester.utils;

public class BinaryDecompositionTree<N> {

    private Node<N> root;

    public BinaryDecompositionTree() {
    }

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

    public boolean contains(N object) {
        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
         */
        public void setLeftNode(Node<T> leftNode) {
            this.leftNode = leftNode;
        }

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

        /**
         * @param rightNode the rightNode to set
         */
        public void setRightNode(Node<T> rightNode) {
            this.rightNode = rightNode;
        }

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

    public enum NodeType {
        SEQ, PAR, LEAF
    }
}