// Random Binary Trees // // Paul E. Dunne 28/11/07 // import java.util.*; public class RandomTrees { static Random S = new Random(); //***************************************************** // Generate Random Binary Tree (n leaves) from Root Down // (as described in lecture notes) //****************************************************** public static BinaryTree RootDown(int n) { BinaryTree T = new BinaryTree(); int k; T.SetRootValue(n); if (n>1) { k = S.nextInt(n-1) + 1; // Choose number of leaves in Left T.SetLeft(RootDown(k)); T.SetRight(RootDown(n-k)); }; return BinaryTree.LabelTree(T); }; //***************************************************** // From Leaf nodes upward // (as described in lecture notes) //***************************************************** public static BinaryTree LeafUp(int n) { BinaryTree[] T = new BinaryTree[n]; BinaryTree Temp; int m = n; int i,j; for (int k=0; k1) { Temp = new BinaryTree(); j = S.nextInt(m); Temp = T[m-1]; T[m-1] = T[j]; T[j] = Temp; i = S.nextInt(m-1); Temp = new BinaryTree(); Temp.SetRootValue(m); Temp.SetLeft(T[m-1]); Temp.SetRight(T[i]); T[i] = Temp; m--; }; return BinaryTree.LabelTree(T[0]); }; //*************************************************************** // Uniform Method (from algorithm given in, // // Generating Binary Trees at Random // M.D. Atkinson and J.-R. Sack, Inf. Proc. Lett., 41, pages 21-23, 1992; // // The variable and method names used in the suite of methods given below // are adopted from the notation used in this paper.) // // Basic approach: generate uniformly at random, a well-formed // sequence of 2(n-1) brackets - '(' and ')' - i.e. in which every // '(' has a matching ')'. Such sequences map 1-1 to the edges in // a binary tree - hence 2n-2 edges = 2n-1 nodes (n leaf nodes + n-1 internal). //**************************************************************** // //*************************************************************************** // Give w (assumed to be well-formed) construct its corresponding binary tree //*************************************************************************** public static BinaryTree ConvertToTree(String w) { BinaryTree T = new BinaryTree(); int k = 0; if (w.length()==0) { T.SetRootValue(1); } else { k = BreakPoint(w); T.SetRootValue(w.length()); T.SetLeft( ConvertToTree(w.substring(1,k-1)) ); T.SetRight( ConvertToTree( w.substring(k,w.length()) ) ); }; return T; }; //*********************************************************************** // Test if the (even length) sequence of '(' and ')' in w is well-formed //*********************************************************************** public static boolean WellFormed(String w) { int defect = 0; int k=0; int[] s = new int[w.length()+1]; s[0]=0; for (k=0; k < w.length(); k++) { if (w.charAt(k)=='(') s[k+1] = s[k]+1; else s[k+1] = s[k]-1; }; for (k=1; k0) // at which u contains equal numbers of ( and ) brackets; u, of course, // is not necessarily well-formed. //***************************************************************** public static int BreakPoint(String w) { int x=1; int k=0; int[] s = new int[w.length()+1]; s[0]=0; for (k=0; k < w.length(); k++) { if (w.charAt(k)=='(') s[k+1] = s[k]+1; else s[k+1] = s[k]-1; }; while ((x