//public Tree _tree { get; set; } static int Main(string[] args) { System.IO.StreamWriter sw = new System.IO.StreamWriter(@"C:\Users\kylan_000\Desktop\tree.txt"); sw.Flush(); int depth = 0; List<Tree> _tree = new Tree(Guid.NewGuid(), depth); Random r = new Random(); int i = 10000; //this is the number of children you wand to add to the root node. sw.WriteLine("i: " + i); int j = 0; while (j < i) { _tree.AddChild(new Tree(Guid.NewGuid(), depth) { _parents = _tree }); j++; } Action<Tree, int> buildAction = (N,I) => N.AddChild(new Tree(Guid.NewGuid(), I) { _parents = N }); //foreach (Tree t in _tree._children) //{ // i = 0; // Build(ref i, ref j, ref action, t, depth++); //} //Func<Random, bool> addCondition = R => R.NextDouble() > 0.1; //this is the condition under which to add a child to the current node. Func<Random, Tree, bool> addCondition = (R, T) => R.NextDouble() * ( Math.Pow( T._depth + 1, 0.5) ) > 0.5; foreach (Tree t in _tree._children) { i = 0; Build(buildAction, t, ref r, addCondition, addCondition(r, t)); } int nodes = 0; foreach (Tree t in _tree._children) { sw.WriteLine("Parent: " + t._parents._id.ToString() +" | Node: " + t._id.ToString() + " | depth: " + t._depth); nodes++; Print(t, ref nodes, ref sw); } sw.WriteLine(nodes + " total nodes."); Console.WriteLine(nodes + " total nodes."); sw.Close(); for (; ; ) { } return 0; }
public void AddChild( Tree t ) { _children.Add(t); }
public static void Build(ref int i, ref int j, ref Action<Tree> buildAction, Tree t, int depth) { if (i < j) { i++; buildAction(t); Build(ref i,ref j,ref buildAction,t, depth); } }
public static void Build(Action<Tree, int> buildAction, Tree t, ref Random r, Func<Random, bool> addCondition, bool Add) { if (addCondition(r)) { buildAction(t, t._depth + 1); foreach (Tree T in t._children) Build(buildAction, T, ref r, addCondition, addCondition(r)); } }
private static void Print(Tree t, ref int count, ref System.IO.StreamWriter sw) { foreach (Tree T in t._children) { count++; sw.WriteLine( GetTabDepth(T._depth, " ") + "Parent: " + (T._parents != null ? T._parents._id.ToString() : "Has no parents.") + " | Node: " + T._id.ToString() + " | depth: " + T._depth); Print(T, ref count, ref sw); } }