1

How can I implement a "binary tree structure"?

The result should be:

enter image description here

If the field contains "true" both lists should add this element. If the field contains "false" only the right list should add this element. I think without a recursive function this method is not implementable.

public class Test {

    static List<String> fields = new ArrayList<>();
    static List<Fieldmatrix> list = new ArrayList<>();

    public static void main(String[] args) {

        fields.add("1 : true");
        fields.add("2 : false");
        fields.add("3 : false");    

        permute(new ArrayList<>(),new ArrayList<>(), 0 , fields); 
    }
}

Method:

public static List<List<String>> permute(List<String> fieldsRight, List<String> fieldsLeft, int start, List<String> fields){

    List<List<String>> combinations = new ArrayList<>();

    if (start >= fields.size()) {
        combinations.add(fieldsRight);
        combinations.add(fieldsLeft);
        return combinations;
    }
    List<String> tmpRight = new ArrayList<>();
    List<String> tmpLeft = new ArrayList<>();

    if (fields.get(start).contains("false")){


        fieldsRight.add(fields.get(start));
        tmpRight = new ArrayList<>(fieldsRight);
        tmpLeft = new ArrayList<>(fieldsLeft);

        combinations.addAll(permute(fieldsRight, fieldsLeft, start+1, fields));


    }
    else{

        fieldsRight.add(fields.get(start));
        fieldsLeft.add(fields.get(start));
        tmpRight = new ArrayList<>(fieldsRight);
        tmpLeft = new ArrayList<>(fieldsLeft);

        combinations.addAll(permute(fieldsRight, fieldsLeft, start+1, fields));
    }

    return combinations;
}

Where is my mistake? How can I solve this problem?

1
  • The classic way to implement a binary try is with a class that represents a node has some data fields, and a left and a right pointer (of that same class). Commented May 12, 2017 at 6:28

1 Answer 1

1

The way I understand your diagram, each node would have a data field that is a list of boolean, and a left and a right reference to some other node:

class Node {
    Map<Integer,Boolean> data;
    Node left;
    Node right;
};

UPDATE

public class Tree {
    private List<Boolean> list;
    private Node left;
    private Node right;

    private static class Node {
        List<Integer> list;
        private Node left;
        private Node right;

        Node(List<Boolean> treeList, List<Integer> list, int start) {
            this.list = list;
            if (start < treeList.size()) {
                List<Integer> leftList = new ArrayList<>(list);
                List<Integer> rightList = new ArrayList<>(list);
                rightList.add(start+1);
                if (treeList.get(start)) {
                    leftList.add(start+1);
                }
                left = new Node(treeList, leftList, start+1);
                right = new Node(treeList, rightList, start+1);
            }
        }

        void print(int indent) {
            for (int i = 0; i < indent; ++i) {
                System.out.print("    ");
            }
            System.out.println(list);
            if (left != null) {
                left.print(indent+1);
            }
            if (right != null) {
                right.print(indent+1);
            }
        }
    }

    public Tree(List<Boolean> list) {
        this.list = new ArrayList<>(list);
        if (!list.isEmpty()) {
            List<Integer> leftList = new ArrayList<>();
            List<Integer> rightList = new ArrayList<>();
            rightList.add(1);
            if (list.get(0)) {
                leftList.add(1);
            }
            left = new Node(list, leftList, 1);
            right = new Node(list, rightList, 1);
        }
    }

    public void print() {
        System.out.println(list);
        if (left != null) {
            left.print(1);
        }
        if (right != null) {
            right.print(1);
        }
    }
}

To build the tree:

    Tree tree = new Tree(Arrays.asList(true, false, false));
    tree.print();

UPDATE 2: Output

[true, false, false]
    [1]
        [1]
            [1]
            [1, 3]
        [1, 2]
            [1, 2]
            [1, 2, 3]
    [1]
        [1]
            [1]
            [1, 3]
        [1, 2]
            [1, 2]
            [1, 2, 3]
Sign up to request clarification or add additional context in comments.

5 Comments

How did you come to List<Boolean> ?
@mrfsl you're right: it's not a List<Boolean>, rather a Map<Integer,Boolean>.
And how can I make the references to the lists recursively?
You build the tree
@mrfsl like this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.