public void testCalculateAmortizedComplexityOfRandomTreesInLoop()
        {
            int minNumberOfNodes = 50, maxNumberOfNodes = 100, numberOfTrees = 50;
            bool writeResultsToFile = true;
            FileStream fs = new FileStream("./amortizedAnalysisResults-" +
                DateTime.Now.Year + "-" +
                DateTime.Now.Month + "-" +
                DateTime.Now.Day + "_" +
                DateTime.Now.Hour + "." +
                DateTime.Now.Minute + "." +
                DateTime.Now.Second +
                ".txt", FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            Random rnd = new Random();

            if (minNumberOfNodes > maxNumberOfNodes || minNumberOfNodes < 0 || maxNumberOfNodes < 1 || numberOfTrees < 1)
                Assert.Fail("The configuration is incorrect");

            for (int i = 0; i < numberOfTrees; i++)
            {
                Debug.WriteLine("-----------------------------------------------------------------");
                var numberOfNodes = rnd.Next(minNumberOfNodes, maxNumberOfNodes);
                Debug.WriteLine("Generating " + numberOfTrees + " random trees with " + numberOfNodes + " Nodes");
                BinaryTree tree = new BinaryTree(numberOfNodes);

                Debug.WriteLine("Tree was generated from the following values: { " + string.Join(",", tree.nodeValues) + "} (" + tree.nodeValues.Length + " nodes)");

                int amortizedComplexity = tree.calculateAmortizedComplexity();
                Assert.AreEqual(2 * (tree.nodeValues.Length - 1), amortizedComplexity); // the complexity is always 2*(n-1)

                Debug.WriteLine("Amortized complexity: " + amortizedComplexity);
                Debug.WriteLine("Is amortized complexity right: " + (2 * (tree.nodeValues.Length - 1) == amortizedComplexity).ToString());

                if (writeResultsToFile)
                    sw.WriteLine(tree.nodeValues.Length + "," + amortizedComplexity);
            }
            sw.Close();
            fs.Close();
        }
 public void Initialize()
 {
     this.exampleTree = new BinaryTree(new int[] { 3, 2, 7, 1, 5, 4, 6, 8 });
 }
 public void testCalculateAmortizedComplexityOfRandomTreeOfHundredNodes()
 {
     BinaryTree tree = new BinaryTree(100); // will generate Nodes with values [1, 100] in random order
     Assert.AreEqual(2 * (tree.nodeValues.Length - 1), tree.calculateAmortizedComplexity()); // the complexity is always 2*(n-1)
 }
        public void testTransverseProblematicTreeOfHundredNodes()
        {
            BinaryTree tree = new BinaryTree(new int[] { 61, 33, 9, 81, 22, 75, 45, 8, 16, 54, 4, 66, 63, 53, 62, 48, 14, 90, 1, 38, 80, 28, 44, 68, 76, 82, 69, 93, 96, 71, 98, 31, 100, 41, 84, 35, 24, 51, 73, 47, 6, 20, 23, 7, 37, 94, 97, 70, 58, 91, 17, 57, 78, 13, 32, 50, 18, 34, 87, 79, 85, 27, 46, 39, 83, 30, 3, 36, 42, 67, 26, 40, 5, 25, 12, 10, 74, 19, 15, 2, 29, 60, 65, 86, 64, 11, 52, 77, 59, 95, 99, 72, 89, 88, 92, 49, 43, 56, 55, 21 });

            tree.inorderFirst();
            for (int i = 1; i < tree.nodeValues.Length + 1; i++)
            {
                Assert.AreEqual(i, tree.DistinguishedNode.value);
                tree.inorderNext();
            }
        }
        public void testTransverseRandomTreeOfHundredNodes()
        {
            BinaryTree tree = new BinaryTree(100); // will generate Nodes with values [1, 100] in random order

            tree.inorderFirst();
            for (int i = 1; i < tree.nodeValues.Length + 1; i++)
            {
                Assert.AreEqual(i, tree.DistinguishedNode.value, "Assertion failed, tree node values: " + string.Join(" ", tree.nodeValues));
                tree.inorderNext();
            }
        }
        public void testTransverseComplexTree()
        {
            BinaryTree tree = new BinaryTree(new int[] { 14, 8, 6, 4, 2, 7, 9, 11, 13, 25, 15, 18, 17, 29, 28, 30, 31 });
            int[] transverseOrder = new int[] { 2, 4, 6, 7, 8, 9, 11, 13, 14, 15, 17, 18, 25, 28, 29, 30, 31 };

            Assert.AreEqual(14, tree.RootNode.value);

            tree.inorderFirst();
            for (int i = 0; i < transverseOrder.Length; i++)
            {
                Assert.AreEqual(transverseOrder[i], tree.DistinguishedNode.value);
                tree.inorderNext();
            }
        }
        public void testInitializingWithTenRandomNodes()
        {
            BinaryTree treeWithTenNodes = new BinaryTree(10);

            Assert.AreEqual(treeWithTenNodes.nodeValues.Length, 10);
            Assert.AreEqual(treeWithTenNodes.nodeValues.Min(), 1);
            Assert.AreEqual(treeWithTenNodes.nodeValues.Max(), 10);
        }
        public void testInitializingWithNotDistinctNotOrderedArray()
        {
            BinaryTree tree = new BinaryTree(new int[] { 3, 4, 3, 3, 2, 4, 5, 1, 1, 1 });

            CollectionAssert.AreEqual(new int[] { 3, 4, 2, 5, 1 }, tree.nodeValues);
            Assert.AreEqual(3, tree.RootNode.value);
            Assert.AreEqual(3, tree.DistinguishedNode.value);
        }
        public void testInitializingWithDistinctOrderedArray()
        {
            BinaryTree tree = new BinaryTree(new int[] { 1, 2, 3, 4, 5 });

            CollectionAssert.AreEqual(new int[] { 1, 2, 3, 4, 5 }, tree.nodeValues);
            Assert.AreEqual(1, tree.RootNode.value);
            Assert.AreEqual(1, tree.DistinguishedNode.value);
        }
        public void testIfRandomNodeInitializerGeneratesNonSequentialArray()
        {
            BinaryTree treeWithHundredNodes = new BinaryTree(100);

            for (int i = 0; i < treeWithHundredNodes.nodeValues.Length - 1; i++)
            {
                if (treeWithHundredNodes.nodeValues[i] > treeWithHundredNodes.nodeValues[i + 1])
                    return;
            }

            Assert.Fail("The binary tree of 100 random Nodes was sequential");
        }