Problem:
Given numbers 1,2,3...N, print all binary search trees that can be constructed with these N numbers.
Solution:
package alg;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class AllBST {
class Node<T extends Comparable<?>> {
Node<T> left, right;
T data;
public Node(T data) {
this(data, null, null);
}
public Node(T data, Node<T> l, Node<T> r){
this.data = data;
this.left = l;
this.right = r;
}
}
class BTreePrinter {
public <T extends Comparable<?>> void printNode(Node<T> root) {
int maxLevel = maxLevel(root);
printNodeInternal(Collections.singletonList(root), 1, maxLevel);
}
public <T extends Comparable<?>> void printNodeInternal(List<Node<T>> nodes, int level, int maxLevel) {
if (nodes.isEmpty() || isAllElementsNull(nodes))
return;
int floor = maxLevel - level;
int endgeLines = (int) Math.pow(2, (Math.max(floor - 1, 0)));
int firstSpaces = (int) Math.pow(2, (floor)) - 1;
int betweenSpaces = (int) Math.pow(2, (floor + 1)) - 1;
printWhitespaces(firstSpaces);
List<Node<T>> newNodes = new ArrayList<Node<T>>();
for (Node<T> node : nodes) {
if (node != null) {
System.out.print(node.data);
newNodes.add(node.left);
newNodes.add(node.right);
} else {
newNodes.add(null);
newNodes.add(null);
System.out.print(" ");
}
printWhitespaces(betweenSpaces);
}
System.out.println("");
for (int i = 1; i <= endgeLines; i++) {
for (int j = 0; j < nodes.size(); j++) {
printWhitespaces(firstSpaces - i);
if (nodes.get(j) == null) {
printWhitespaces(endgeLines + endgeLines + i + 1);
continue;
}
if (nodes.get(j).left != null)
System.out.print("/");
else
printWhitespaces(1);
printWhitespaces(i + i - 1);
if (nodes.get(j).right != null)
System.out.print("\\");
else
printWhitespaces(1);
printWhitespaces(endgeLines + endgeLines - i);
}
System.out.println("");
}
printNodeInternal(newNodes, level + 1, maxLevel);
}
private void printWhitespaces(int count) {
for (int i = 0; i < count; i++)
System.out.print(" ");
}
private <T extends Comparable<?>> int maxLevel(Node<T> node) {
if (node == null)
return 0;
return Math.max(maxLevel(node.left), maxLevel(node.right)) + 1;
}
private <T> boolean isAllElementsNull(List<T> list) {
for (Object object : list) {
if (object != null)
return false;
}
return true;
}
}
public ArrayList<Node<Integer>> buildBST(int i, int j){
ArrayList<Node<Integer>> list = new ArrayList<Node<Integer>>();
if(i > j){
return list;
}else if(i==j){
Node<Integer> n = new Node<Integer>(i);
list.add(n);
return list;
}else {
for(int k=i; k<=j; k++){
ArrayList<Node<Integer>> leftSubTrees = buildBST(i, k-1);
ArrayList<Node<Integer>> rightSubTrees = buildBST(k+1, j);
if(leftSubTrees.isEmpty()){
for(Node<Integer> r: rightSubTrees){
Node<Integer> subRoot = new Node<Integer>(k, null, r);
list.add(subRoot);
}
}else if(rightSubTrees.isEmpty()){
for(Node<Integer> l: leftSubTrees){
Node<Integer> subRoot = new Node<Integer>(k, l, null);
list.add(subRoot);
}
}else {
for(Node<Integer> l: leftSubTrees)
for(Node<Integer> r: rightSubTrees){
Node<Integer> subRoot = new Node<Integer>(k, l, r);
list.add(subRoot);
}
}
}
return list;
}
}
public int calculateAllBstCount(int nodeNum){
int sum = 0;
if(nodeNum <= 1){
return 1;
}
for(int i = 1; i <= nodeNum; i++){
int left = calculateAllBstCount(i-1);
int right = calculateAllBstCount(nodeNum - i);
sum += left * right;
}
return sum;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
AllBST bst = new AllBST();
AllBST.BTreePrinter btp = bst.new BTreePrinter();
ArrayList<Node<Integer>> trees = bst.buildBST(1, 6);
int total = bst.calculateAllBstCount(6);
System.out.println("Total: " + total);
int index = 0;
for(Node<Integer> root : trees){
System.out.println("result " + index++);
btp.printNode(root);
System.out.println("");
}
}
}
Result:
Total: 132
result 0
1
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
2
\
\
\
\
\
\
\
\
3
\
\
\
\
4
\
\
5
\
6
result 1
1
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
2
\
\
\
\
\
\
\
\
3
\
\
\
\
4
\
\
6
/
5
result 2
1
\
\
\
\
\
\
\
\
2
\
\
\
\
3
\
\
5
/ \
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/ \
/ \
2 6
\ /
3 5
result 21
1
\
\
\
\
4
/ \
/ \
3 5
/ \
2 6
result 22
1
\
\
\
\
4
/ \
/ \
3 6
/ /
2 5
result 23
1
\
\
\
\
\
\
\
\
5
/ \
/ \
/ \
/ \
2 6
\
\
3
\
4
result 24
1
\
\
\
\
\
\
\
\
5
/ \
/ \
/ \
/ \
2 6
\
\
4
/
3
result 25
1
\
\
\
\
5
/ \
/ \
3 6
/ \
2 4
result 26
1
\
\
\
\
\
\
\
\
5
/ \
/ \
/ \
/ \
4 6
/
/
2
\
3
result 27
1
\
\
\
\
\
\
\
\
5
/ \
/ \
/ \
/ \
4 6
/
/
3
/
2
result 28
1
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
6
/
/
/
/
/
/
/
/
2
\
\
\
\
3
\
\
4
\
5
result 29
1
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
6
/
/
/
/
/
/
/
/
2
\
\
\
\
3
\
\
5
/
4
result 30
1
\
\
\
\
\
\
\
\
6
/
/
/
/
2
\
\
4
/ \
3 5
result 31
1
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
6
/
/
/
/
/
/
/
/
2
\
\
\
\
5
/
/
3
\
4
result 32
1
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
6
/
/
/
/
/
/
/
/
2
\
\
\
\
5
/
/
4
/
3
result 33
1
\
\
\
\
\
\
\
\
6
/
/
/
/
3
/ \
/ \
2 4
\
5
result 34
1
\
\
\
\
\
\
\
\
6
/
/
/
/
3
/ \
/ \
2 5
/
4
result 35
1
\
\
\
\
\
\
\
\
6
/
/
/
/
4
/ \
/ \
2 5
\
3
result 36
1
\
\
\
\
\
\
\
\
6
/
/
/
/
4
/ \
/ \
3 5
/
2
result 37
1
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
6
/
/
/
/
/
/
/
/
5
/
/
/
/
2
\
\
3
\
4
result 38
1
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
6
/
/
/
/
/
/
/
/
5
/
/
/
/
2
\
\
4
/
3
result 39
1
\
\
\
\
\
\
\
\
6
/
/
/
/
5
/
/
3
/ \
2 4
result 40
1
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
6
/
/
/
/
/
/
/
/
5
/
/
/
/
4
/
/
2
\
3
result 41
1
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
6
/
/
/
/
/
/
/
/
5
/
/
/
/
4
/
/
3
/
2
result 42
2
/ \
/ \
/ \
/ \
/ \
/ \
/ \
/ \
1 3
\
\
\
\
4
\
\
5
\
6
result 43
2
/ \
/ \
/ \
/ \
/ \
/ \
/ \
/ \
1 3
\
\
\
\
4
\
\
6
/
5
result 44
2
/ \
/ \
/ \
/ \
1 3
\
\
5
/ \
4 6
result 45
2
/ \
/ \
/ \
/ \
/ \
/ \
/ \
/ \
1 3
\
\
\
\
6
/
/
4
\
5
result 46
2
/ \
/ \
/ \
/ \
/ \
/ \
/ \
/ \
1 3
\
\
\
\
6
/
/
5
/
4
result 47
2
/ \
/ \
/ \
/ \
1 4
/ \
/ \
3 5
\
6
result 48
2
/ \
/ \
/ \
/ \
1 4
/ \
/ \
3 6
/
5
result 49
2
/ \
/ \
/ \
/ \
1 5
/ \
/ \
3 6
\
4
result 50
2
/ \
/ \
/ \
/ \
1 5
/ \
/ \
4 6
/
3
result 51
2
/ \
/ \
/ \
/ \
/ \
/ \
/ \
/ \
1 6
/
/
/
/
3
\
\
4
\
5
result 52
2
/ \
/ \
/ \
/ \
/ \
/ \
/ \
/ \
1 6
/
/
/
/
3
\
\
5
/
4
result 53
2
/ \
/ \
/ \
/ \
1 6
/
/
4
/ \
3 5
result 54
2
/ \
/ \
/ \
/ \
/ \
/ \
/ \
/ \
1 6
/
/
/
/
5
/
/
3
\
4
result 55
2
/ \
/ \
/ \
/ \
/ \
/ \
/ \
/ \
1 6
/
/
/
/
5
/
/
4
/
3
result 56
3
/ \
/ \
/ \
/ \
1 4
\ \
\ \
2 5
\
6
result 57
3
/ \
/ \
/ \
/ \
1 4
\ \
\ \
2 6
/
5
result 58
3
/ \
/ \
1 5
\ / \
2 4 6
result 59
3
/ \
/ \
/ \
/ \
1 6
\ /
\ /
2 4
\
5
result 60
3
/ \
/ \
/ \
/ \
1 6
\ /
\ /
2 5
/
4
result 61
3
/ \
/ \
/ \
/ \
2 4
/ \
/ \
1 5
\
6
result 62
3
/ \
/ \
/ \
/ \
2 4
/ \
/ \
1 6
/
5
result 63
3
/ \
/ \
2 5
/ / \
1 4 6
result 64
3
/ \
/ \
/ \
/ \
2 6
/ /
/ /
1 4
\
5
result 65
3
/ \
/ \
/ \
/ \
2 6
/ /
/ /
1 5
/
4
result 66
4
/ \
/ \
/ \
/ \
1 5
\ \
\ \
2 6
\
3
result 67
4
/ \
/ \
/ \
/ \
1 6
\ /
\ /
2 5
\
3
result 68
4
/ \
/ \
/ \
/ \
1 5
\ \
\ \
3 6
/
2
result 69
4
/ \
/ \
/ \
/ \
1 6
\ /
\ /
3 5
/
2
result 70
4
/ \
/ \
2 5
/ \ \
1 3 6
result 71
4
/ \
/ \
2 6
/ \ /
1 3 5
result 72
4
/ \
/ \
/ \
/ \
3 5
/ \
/ \
1 6
\
2
result 73
4
/ \
/ \
/ \
/ \
3 6
/ /
/ /
1 5
\
2
result 74
4
/ \
/ \
/ \
/ \
3 5
/ \
/ \
2 6
/
1
result 75
4
/ \
/ \
/ \
/ \
3 6
/ /
/ /
2 5
/
1
result 76
5
/ \
/ \
/ \
/ \
/ \
/ \
/ \
/ \
1 6
\
\
\
\
2
\
\
3
\
4
result 77
5
/ \
/ \
/ \
/ \
/ \
/ \
/ \
/ \
1 6
\
\
\
\
2
\
\
4
/
3
result 78
5
/ \
/ \
/ \
/ \
1 6
\
\
3
/ \
2 4
result 79
5
/ \
/ \
/ \
/ \
/ \
/ \
/ \
/ \
1 6
\
\
\
\
4
/
/
2
\
3
result 80
5
/ \
/ \
/ \
/ \
/ \
/ \
/ \
/ \
1 6
\
\
\
\
4
/
/
3
/
2
result 81
5
/ \
/ \
/ \
/ \
2 6
/ \
/ \
1 3
\
4
result 82
5
/ \
/ \
/ \
/ \
2 6
/ \
/ \
1 4
/
3
result 83
5
/ \
/ \
/ \
/ \
3 6
/ \
/ \
1 4
\
2
result 84
5
/ \
/ \
/ \
/ \
3 6
/ \
/ \
2 4
/
1
result 85
5
/ \
/ \
/ \
/ \
/ \
/ \
/ \
/ \
4 6
/
/
/
/
1
\
\
2
\
3
result 86
5
/ \
/ \
/ \
/ \
/ \
/ \
/ \
/ \
4 6
/
/
/
/
1
\
\
3
/
2
result 87
5
/ \
/ \
/ \
/ \
4 6
/
/
2
/ \
1 3
result 88
5
/ \
/ \
/ \
/ \
/ \
/ \
/ \
/ \
4 6
/
/
/
/
3
/
/
1
\
2
result 89
5
/ \
/ \
/ \
/ \
/ \
/ \
/ \
/ \
4 6
/
/
/
/
3
/
/
2
/
1
result 90
6
/
/
/
/
/
/
/
/
/
/
/
/
/
/
/
/
1
\
\
\
\
\
\
\
\
2
\
\
\
\
3
\
\
4
\
5
result 91
6
/
/
/
/
/
/
/
/
/
/
/
/
/
/
/
/
1
\
\
\
\
\
\
\
\
2
\
\
\
\
3
\
\
5
/
4
result 92
6
/
/
/
/
/
/
/
/
1
\
\
\
\
2
\
\
4
/ \
3 5
result 93
6
/
/
/
/
/
/
/
/
/
/
/
/
/
/
/
/
1
\
\
\
\
\
\
\
\
2
\
\
\
\
5
/
/
3
\
4
result 94
6
/
/
/
/
/
/
/
/
/
/
/
/
/
/
/
/
1
\
\
\
\
\
\
\
\
2
\
\
\
\
5
/
/
4
/
3
result 95
6
/
/
/
/
/
/
/
/
1
\
\
\
\
3
/ \
/ \
2 4
\
5
result 96
6
/
/
/
/
/
/
/
/
1
\
\
\
\
3
/ \
/ \
2 5
/
4
result 97
6
/
/
/
/
/
/
/
/
1
\
\
\
\
4
/ \
/ \
2 5
\
3
result 98
6
/
/
/
/
/
/
/
/
1
\
\
\
\
4
/ \
/ \
3 5
/
2
result 99
6
/
/
/
/
/
/
/
/
/
/
/
/
/
/
/
/
1
\
\
\
\
\
\
\
\
5
/
/
/
/
2
\
\
3
\
4
result 100
6
/
/
/
/
/
/
/
/
/
/
/
/
/
/
/
/
1
\
\
\
\
\
\
\
\
5
/
/
/
/
2
\
\
4
/
3
result 101
6
/
/
/
/
/
/
/
/
1
\
\
\
\
5
/
/
3
/ \
2 4
result 102
6
/
/
/
/
/
/
/
/
/
/
/
/
/
/
/
/
1
\
\
\
\
\
\
\
\
5
/
/
/
/
4
/
/
2
\
3
result 103
6
/
/
/
/
/
/
/
/
/
/
/
/
/
/
/
/
1
\
\
\
\
\
\
\
\
5
/
/
/
/
4
/
/
3
/
2
result 104
6
/
/
/
/
/
/
/
/
2
/ \
/ \
/ \
/ \
1 3
\
\
4
\
5
result 105
6
/
/
/
/
/
/
/
/
2
/ \
/ \
/ \
/ \
1 3
\
\
5
/
4
result 106
6
/
/
/
/
2
/ \
/ \
1 4
/ \
3 5
result 107
6
/
/
/
/
/
/
/
/
2
/ \
/ \
/ \
/ \
1 5
/
/
3
\
4
result 108
6
/
/
/
/
/
/
/
/
2
/ \
/ \
/ \
/ \
1 5
/
/
4
/
3
result 109
6
/
/
/
/
3
/ \
/ \
1 4
\ \
2 5
result 110
6
/
/
/
/
3
/ \
/ \
1 5
\ /
2 4
result 111
6
/
/
/
/
3
/ \
/ \
2 4
/ \
1 5
result 112
6
/
/
/
/
3
/ \
/ \
2 5
/ /
1 4
result 113
6
/
/
/
/
/
/
/
/
4
/ \
/ \
/ \
/ \
1 5
\
\
2
\
3
result 114
6
/
/
/
/
/
/
/
/
4
/ \
/ \
/ \
/ \
1 5
\
\
3
/
2
result 115
6
/
/
/
/
4
/ \
/ \
2 5
/ \
1 3
result 116
6
/
/
/
/
/
/
/
/
4
/ \
/ \
/ \
/ \
3 5
/
/
1
\
2
result 117
6
/
/
/
/
/
/
/
/
4
/ \
/ \
/ \
/ \
3 5
/
/
2
/
1
result 118
6
/
/
/
/
/
/
/
/
/
/
/
/
/
/
/
/
5
/
/
/
/
/
/
/
/
1
\
\
\
\
2
\
\
3
\
4
result 119
6
/
/
/
/
/
/
/
/
/
/
/
/
/
/
/
/
5
/
/
/
/
/
/
/
/
1
\
\
\
\
2
\
\
4
/
3
result 120
6
/
/
/
/
/
/
/
/
5
/
/
/
/
1
\
\
3
/ \
2 4
result 121
6
/
/
/
/
/
/
/
/
/
/
/
/
/
/
/
/
5
/
/
/
/
/
/
/
/
1
\
\
\
\
4
/
/
2
\
3
result 122
6
/
/
/
/
/
/
/
/
/
/
/
/
/
/
/
/
5
/
/
/
/
/
/
/
/
1
\
\
\
\
4
/
/
3
/
2
result 123
6
/
/
/
/
/
/
/
/
5
/
/
/
/
2
/ \
/ \
1 3
\
4
result 124
6
/
/
/
/
/
/
/
/
5
/
/
/
/
2
/ \
/ \
1 4
/
3
result 125
6
/
/
/
/
/
/
/
/
5
/
/
/
/
3
/ \
/ \
1 4
\
2
result 126
6
/
/
/
/
/
/
/
/
5
/
/
/
/
3
/ \
/ \
2 4
/
1
result 127
6
/
/
/
/
/
/
/
/
/
/
/
/
/
/
/
/
5
/
/
/
/
/
/
/
/
4
/
/
/
/
1
\
\
2
\
3
result 128
6
/
/
/
/
/
/
/
/
/
/
/
/
/
/
/
/
5
/
/
/
/
/
/
/
/
4
/
/
/
/
1
\
\
3
/
2
result 129
6
/
/
/
/
/
/
/
/
5
/
/
/
/
4
/
/
2
/ \
1 3
result 130
6
/
/
/
/
/
/
/
/
/
/
/
/
/
/
/
/
5
/
/
/
/
/
/
/
/
4
/
/
/
/
3
/
/
1
\
2
result 131
6
/
/
/
/
/
/
/
/
/
/
/
/
/
/
/
/
5
/
/
/
/
/
/
/
/
4
/
/
/
/
3
/
/
2
/
1