Code example for HashSet

Methods: add

     * Get a set of all the leaves in the tree. 
     * @return Set of all the leaves. 
    public HashSet<ConditionsTreeNode> getLeafSet() {
        HashSet<ConditionsTreeNode> leafSet = new HashSet<ConditionsTreeNode>();
        return getLeafSet(leafSet);
     * Returns a list of all the nodes in the subtree of which this node 
     * is the root. The list contains the nodes in a pre traversal order. 
     * @return List of all nodes in subtree in preorder. 
    public List<ConditionsTreeNode> preorder() {
        ArrayList<ConditionsTreeNode> result = new ArrayList<ConditionsTreeNode>();
        Stack<ConditionsTreeNode> stack = new Stack<ConditionsTreeNode>();
        while (!stack.isEmpty()) {
            ConditionsTreeNode current = stack.pop();
            if (current.mLeft != null) {
            if (current.mRight != null) {
        return result;
    // Private class logic 
     * Adds two new ConditionTreeNodes, one for the operator and one for the 
     * new condition. The current node will end up on the same level as the 
     * one provided in the arguments, they will be siblings. Their common 
     * parent node will be one containing the operator provided in the arguments. 
     * The method will update all the required references so the tree ends up in 
     * a valid state. 
     * This method only supports node arguments with a null parent node. 
     * @param Node to add. 
     * @param Operator that will connect the new node with this one. 
     * @return New parent node, containing the operator. 
     * @throws Exception Throws when the provided new node does not have a null parent. 
    private ConditionsTreeNode add(ConditionsTreeNode node, Operator op) throws Exception {
        if (node.mParent != null) {
            throw new Exception("Can only add new expressions from root node down.");
        ConditionsTreeNode tmpNode = new ConditionsTreeNode(mParent, op);
        tmpNode.mLeft = this;
        tmpNode.mRight = node;
        if (mParent != null) {
            mParent.updateChild(this, tmpNode);
        this.mParent = tmpNode;
        node.mParent = tmpNode;
        return tmpNode;
     * Helper method that replaces a child of the current node with a new node. 
     * If the provided old child node was the left one, left will be replaced with 
     * the new one. Same goes for the right one. 
     * @param oldChild Old child node to be replaced. 
     * @param newChild New child node. 
    private void updateChild(ConditionsTreeNode oldChild, ConditionsTreeNode newChild) {
        // we can compare objects id's because this is the desired behaviour in this case 
        if (mLeft == oldChild) {
            mLeft = newChild;
        } else if (mRight == oldChild) {
            mRight = newChild;
     * Recursive function to gather all the leaves in the subtree of which 
     * this node is the root. 
     * @param leafSet Leafset that's being built. 
     * @return Set of leaves being completed. 
    private HashSet<ConditionsTreeNode> getLeafSet(HashSet<ConditionsTreeNode> leafSet) {
        if (mLeft == null && mRight == null) {
            // if we ended up in a leaf, add ourself and return 
            return leafSet;
        // we didn't end up in a leaf 
        if (mLeft != null) {