public static List <UBSTNode> generateTrees(int n) { List <UBSTNode> ls = new List <UBSTNode>(); ls.Add(null); _dict[0] = ls; ls = new List <UBSTNode>(); ls.Add(new UBSTNode(1, null, null)); _dict[1] = ls; for (int i = 2; i <= n; i++) { ls = new List <UBSTNode>(); for (int j = 1; j <= i; j++) { foreach (UBSTNode left in _dict[j - 1]) { foreach (UBSTNode right in _dict[i - j]) { UBSTNode nRoot = new UBSTNode(j, null, null); nRoot.left = left; nRoot.right = DeepCopyIncrease(right, j); ls.Add(nRoot); } } } _dict[i] = ls; } return(_dict[n]); }
public static UBSTNode DeepCopyIncrease(UBSTNode n, int d) { if (n == null) { return(null); } UBSTNode newRoot = new UBSTNode(n._d + d, null, null); newRoot.left = DeepCopyIncrease(n.left, d); newRoot.right = DeepCopyIncrease(n.right, d); return(newRoot); }
public UBSTNode(int d, UBSTNode l, UBSTNode r) { _d = d; left = l; right = r; }