Пример #1
0
        /// <summary>
        /// Demos all 3 Development versions of the PostOrder Tree Traversal algorithm
        /// in one method.
        ///
        /// Only the algorithm in the TreeLib library is suppossed to be
        /// final - all other version are kept to understand the principal applied to
        /// arrive at the Generic patterns in the TreeLib library.
        /// </summary>
        /// <param name="root"></param>
        private static void TestPostOrder(Node root)
        {
            // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
            // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX PostOrder Traversal (Depth-First)
            Console.WriteLine("(Depth First) PostOrder Tree Traversal V1");
            PostOrderV1.Traverse(root);
            Console.WriteLine();
            Console.WriteLine("Press any key...");
            Console.ReadKey();

            // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX PostOrder Traversal (Depth-First)
            Console.WriteLine("(Depth First) Post-Order Tree Traversal V2");
            var items = PostOrderV2.Traverse(root, i => i.Children);

            foreach (var item in items)
            {
                Console.WriteLine(item.GetPath());
            }
            Console.WriteLine();
            Console.WriteLine("Press any key...");
            Console.ReadKey();

            // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX PostOrder Traversal (Depth-First)
            Console.WriteLine("(Depth First) PostOrder Tree Traversal V3");
            items = TreeLib.Depthfirst.Traverse.PostOrder(root, i => i.Children);

            foreach (var item in items)
            {
                Console.WriteLine(item.GetPath());
            }
            Console.WriteLine();
            Console.WriteLine("Press any key...");
            Console.ReadKey();
        }
Пример #2
0
        private static void TestTreeTraversal(Node root)
        {
            // Generate version 1 traversals
            var levelOrderItems1 = LevelOrderV1.Traverse(root);
            var preOrderItems1   = PreOrderV1.Traverse(root);
            var postOrderItems1  = PostOrderV1.Traverse(root);

            // Generate version 2 traversals
            var levelOrderItems2 = LevelOrderV2.Traverse(root, i => i.Children).ToList();
            var preOrderItems2   = PreOrderV2.Traverse(root, i => i.Children).ToList();
            var postOrderItems2  = PostOrderV2.Traverse(root, i => i.Children).ToList();

            // Generate version 3 traversals
            var levelOrderItems3 = TreeLib.BreadthFirst.Traverse.LevelOrder(root, i => i.Children).ToList();
            var preOrderItems3   = TreeLib.Depthfirst.Traverse.Preorder(root, i => i.Children).ToList();

            List <Node> postOrderItems3 = null;

            try
            {
                postOrderItems3 = TreeLib.Depthfirst.Traverse.PostOrder(root, i => i.Children).ToList();
            }
            catch (Exception exc)
            {
                Console.WriteLine(exc.Message);
                Console.WriteLine(exc.StackTrace);
            }

            // Order of traversal may be different,
            // in the end, all elements are there (count)
            Assert.AreEqual(levelOrderItems1.Count, preOrderItems1.Count);
            Assert.AreEqual(levelOrderItems1.Count, postOrderItems1.Count);

            Assert.AreEqual(levelOrderItems1.Count, levelOrderItems2.Count);
            Assert.AreEqual(preOrderItems1.Count, preOrderItems2.Count);
            Assert.AreEqual(postOrderItems1.Count, postOrderItems2.Count);

            Assert.AreEqual(levelOrderItems1.Count, levelOrderItems3.Count);
            Assert.AreEqual(preOrderItems1.Count, preOrderItems3.Count);
            Assert.AreEqual(postOrderItems1.Count, postOrderItems3.Count);

            // Ensure same sequence on each version 1 and 2 returned
            for (int i = 0; i < levelOrderItems1.Count; i++)
            {
                Assert.AreEqual(levelOrderItems1[i], levelOrderItems2[i].Item2);
            }

            for (int i = 0; i < preOrderItems1.Count; i++)
            {
                Assert.AreEqual(preOrderItems1[i], preOrderItems2[i]);
            }

            for (int i = 0; i < postOrderItems1.Count; i++)
            {
                Assert.AreEqual(postOrderItems1[i], postOrderItems2[i]);
            }

            // Ensure same sequence on each version 1 and 3 returned
            for (int i = 0; i < levelOrderItems1.Count; i++)
            {
                Assert.AreEqual(levelOrderItems1[i], levelOrderItems3[i].Node);
            }

            for (int i = 0; i < preOrderItems1.Count; i++)
            {
                Assert.AreEqual(preOrderItems1[i], preOrderItems3[i]);
            }

            for (int i = 0; i < postOrderItems1.Count; i++)
            {
                Assert.AreEqual(postOrderItems1[i], postOrderItems3[i]);
            }

            // Sort Version 1 by Id and make sure all items are there
            levelOrderItems1.Sort((i1, i2) => i1.Id.CompareTo(i2.Id));
            preOrderItems1.Sort((i1, i2) => i1.Id.CompareTo(i2.Id));
            postOrderItems1.Sort((i1, i2) => i1.Id.CompareTo(i2.Id));

            // Sort Version 2 by Id and make sure all items are there
            levelOrderItems2.Sort((i1, i2) => i1.Item2.Id.CompareTo(i2.Item2.Id));
            preOrderItems2.Sort((i1, i2) => i1.Id.CompareTo(i2.Id));
            postOrderItems2.Sort((i1, i2) => i1.Id.CompareTo(i2.Id));

            // Sort Version 3 by Id and make sure all items are there
            levelOrderItems3.Sort((i1, i2) => i1.Node.Id.CompareTo(i2.Node.Id));
            preOrderItems3.Sort((i1, i2) => i1.Id.CompareTo(i2.Id));
            postOrderItems3.Sort((i1, i2) => i1.Id.CompareTo(i2.Id));

            for (int i = 0; i < levelOrderItems1.Count; i++)
            {
                Assert.AreEqual(levelOrderItems1[i], preOrderItems1[i]);
                Assert.AreEqual(levelOrderItems1[i], postOrderItems1[i]);

                if (levelOrderItems1[i].Equals(levelOrderItems2[i].Item2) == false)
                {
                    Console.WriteLine("1.1) Comparing levelOrderItems1[i] '{0}' with levelOrderItems2[i] '{1}' failed."
                                      , levelOrderItems1[i], levelOrderItems2[i].Item2);
                }

                if (preOrderItems1[i].Equals(preOrderItems2[i]) == false)
                {
                    Console.WriteLine("2.1) Comparing preOrderItems1[i] '{0}' with preOrderItems2[i] '{1}' failed."
                                      , preOrderItems1[i], preOrderItems2[i]);
                }

                if (postOrderItems1[i].Equals(postOrderItems2[i]) == false)
                {
                    Console.WriteLine("3.1) Comparing postOrderItems1[i] '{0}' with postOrderItems2[i] '{1}' failed."
                                      , postOrderItems1[i], postOrderItems2[i]);
                }

                Assert.AreEqual(levelOrderItems1[i], levelOrderItems2[i].Item2);
                Assert.AreEqual(preOrderItems1[i], preOrderItems2[i]);
                Assert.AreEqual(postOrderItems1[i], postOrderItems2[i]);

                if (levelOrderItems1[i].Equals(levelOrderItems3[i].Node) == false)
                {
                    Console.WriteLine("1.2) Comparing levelOrderItems1[i] '{0}' with levelOrderItems3[i] '{1}' failed."
                                      , levelOrderItems1[i], levelOrderItems3[i].Node);
                }

                if (preOrderItems1[i].Equals(preOrderItems3[i]) == false)
                {
                    Console.WriteLine("2.2) Comparing preOrderItems1[i] '{0}' with preOrderItems3[i] '{1}' failed."
                                      , preOrderItems1[i], preOrderItems3[i]);
                }

                if (postOrderItems1[i].Equals(postOrderItems3[i]) == false)
                {
                    Console.WriteLine("3.3) Comparing postOrderItems1[i] '{0}' with postOrderItems3[i] '{1}' failed."
                                      , postOrderItems1[i], postOrderItems3[i]);
                }

                Assert.AreEqual(levelOrderItems1[i], levelOrderItems3[i].Node);
                Assert.AreEqual(preOrderItems1[i], preOrderItems3[i]);

                postOrderItems3.Sort((i1, i2) => i1.Id.CompareTo(i2.Id));
                Assert.AreEqual(postOrderItems1[i], postOrderItems3[i]);
            }
        }