Developer, Former MVP, now at Microsoft - Best of 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
static void Main() { Tree tr = Tree.CreateSomeTree(9, 1); Stopwatch sw = Stopwatch.StartNew(); WalkTree(tr); Console.WriteLine("Elapsed= " + sw.ElapsedMilliseconds.ToString()); Console.ReadLine(); } static int ProcessItem(int treeData) { // for demo purposes Thread.SpinWait(4000000); //TODO something real with data return treeData; // not being used } static void WalkTree(Tree tree) { if (tree == null) return; WalkTree(tree.Left); WalkTree(tree.Righ); ProcessItem(tree.Data); }
static void WalkTree(Tree tree) { if (tree == null) return; Thread left = new Thread((o) => WalkTree(tree.Left)); left.Start(); Thread righ = new Thread((o) => WalkTree(tree.Righ)); righ.Start(); left.Join(); righ.Join(); ProcessItem(tree.Data); }