//Definition for a binary tree node.
public class TreeNode {
int key;
TreeNode left;
TreeNode right;
TreeNode(int x) { key = x; }
}
给定TreeNode的总数int
n
,如何生成一棵随机分布的二叉树(我指的是二叉树的random形状,不是随机键值。您可以将 TreeNodes 的所有键值设置为 1) 并返回 TreeNode root
。
这就是如何实现以下API:
public class RandomBinaryTree{
public TreeNode binaryTreeGenerator(int n){
}
}
PS:比如n = 3
,我希望算法能够每次随机生成以下5
二叉树之一:
1 1 1 1 1
/ / / \ \ \
1 1 1 1 1 1
/ \ / \
1 1 1 1
是否有任何算法可以等概率地生成具有固定节点数n
的二叉树?
最佳答案
偏分布,算法简单
从根开始,随机选择每个子树的节点数,然后递归:
public class RandomBinaryTree {
private Random random = new Random();
public TreeNode binaryTreeGenerator(int n, int key){
if (n == 0)
return null;
TreeNode root = new TreeNode(key);
// Number of nodes in the left subtree (in [0, n-1])
int leftN = random.nextInt(n);
// Recursively build each subtree
root.setLeft(binaryTreeGenerator(leftN, key));
root.setRight(binaryTreeGenerator(n - leftN - 1, key));
return root;
}
}
该算法不强制结果的均匀分布,并且非常有利于平衡树。对于一个简单的证明,请考虑 n = 3
的情况,并计算 5 种可能的二叉树中每一种的出现概率(有关相关组合,请参阅 Catalan numbers)。
均匀分布,更具挑战性
已经有一些关于这个主题的研究和this可能是最简单和最快的方法之一(O(n))。这个想法是生成一个包含相等数量的左右括号的随机单词,然后使用保持均匀分布的转换将其映射到二叉树。
第 1 步:生成一个随机平衡词:
private static Random random = new Random();
// true means '(', false means ')'
private static boolean[] buildRandomBalancedWord(int n) {
boolean[] word = new boolean[n * 2];
List<Integer> positions = IntStream.range(0, 2 * n).boxed()
.collect(Collectors.toList());
for (int i = n; i > 0; i--) {
int index = random.nextInt(n + i);
word[positions.remove(index)] = true;
}
return word;
}
第 2 步: 生成的单词可能有 k
个“缺陷”,基本上是不匹配的右括号。该论文表明,有一种方法可以重新排列生成的词,使得生成的映射是从具有 k
缺陷的词集到具有 0
的词集的双射缺陷(格式正确的词)。这是程序:
private static void rearrange(boolean[] word, int start, int end) {
int sum = 0;
int defectIndex = -1;
for (int i = start; i < end; i++) {
sum = sum + (word[i] ? 1 : -1);
if (defectIndex < 0 && sum < 0) {
defectIndex = i;
} else if (defectIndex >= 0 && sum == 0) {
// We now have irreducible u = rtl spanning [defectIndex, i]
int uLength = i - defectIndex + 1;
boolean[] flipped = new boolean[uLength - 2];
for (int j = 0; j < flipped.length; j++)
flipped[j] = !word[defectIndex + j + 1];
// Shift the remaining word
if (i + 1 < end)
System.arraycopy(word, i + 1, word, defectIndex + 1, end - (i + 1));
// Rewrite uw as lwrt*, t* being the flipped array
word[defectIndex] = true;
System.arraycopy(flipped, 0, word, end - flipped.length, flipped.length);
word[end - uLength + 1] = false;
// Now recurse on w, worst case we go (word.length/2)-deep
rearrange(word, defectIndex + 1, end - uLength + 1);
break;
}
}
}
第 3 步: 从格式正确的括号单词到二叉树存在一对一的映射:每一对匹配的括号都是一个节点,里面的所有内容都是左子树,所有内容之后是右子树:
// There is probably a smarter way to do this
public static TreeNode buildTree(boolean[] word, int key) {
Deque<TreeNode> stack = new ArrayDeque<>();
boolean insertRight = false;
TreeNode root = null;
TreeNode currentNode = null;
for (int i = 0; i < word.length; i++) {
if (word[i]) {
TreeNode previousNode = currentNode;
currentNode = new TreeNode(key);
if (root == null) {
root = currentNode;
} else if (insertRight) {
previousNode.setRight(currentNode);
insertRight = false;
} else {
previousNode.setLeft(currentNode);
}
stack.push(currentNode);
} else {
currentNode = stack.pop();
insertRight = true;
}
}
return root;
}
一些实用函数:
public static boolean[] buildRandomWellFormedWord(int n) {
boolean[] word = buildRandomBalancedWord(n);
rearrange(word, 0, word.length);
return word;
}
public static String toString(boolean[] word) {
StringBuilder str = new StringBuilder();
for (boolean b : word)
str.append(b ? "(" : ")");
return str.toString();
}
测试:让我们打印超过 1000 万次大小为 3 的实际分布:
public static void main(String[] args) throws Exception {
Map<String, Integer> counts = new HashMap<String, Integer>();
int N = 10000000, n = 3;
for (int i = 0; i < N; i++) {
boolean[] word = buildRandomWellFormedWord(n);
String str = toString(word);
Integer count = counts.get(str);
if (count == null)
count = 0;
counts.put(str, count + 1);
}
counts.entrySet().stream().forEach(e ->
System.out.println("P[" + e.getKey() + "] = " + e.getValue().doubleValue() / N));
}
输出应该类似于:
P[()()()] = 0.200166
P[(()())] = 0.200451
P[()(())] = 0.199894
P[((()))] = 0.199006
P[(())()] = 0.200483
因此 buildTree(buildRandomWellFormedWord(n), key)
将生成一棵大小为 n
的二叉树,遵循对所有可能树的均匀分布。
关于java - 如何根据节点编号随机生成二叉树?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56873764/