How to make a random Expression Binary Tree
I need to code a program that buildl a Binary Tree with two types of Node: ConstNode: represent random numbers OperaNode; represent operators [+, -, *, /] I realized that: +Each node has zero, one or two children +Only OperaNode can have children +All leaf node must be ConstNode (Check the attached image for more details) I can only build a Binary Tree with all random numeric elements. I don't know how to generate a random expression tree based on the code I had. 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; } } I appreciate your help in solving this problem.