Ejemplo n.º 1
0
        public void GraphNode_CreateTreeManually_ShouldReturnSortedTree()
        {
            var tree = new BinaryNode(3);

            tree.Add(2);
            tree.Add(5);

            var list = tree.InOrderTraverse(new List <int>());

            list.Should().Equal(2, 3, 5);
        }
Ejemplo n.º 2
0
        private BinaryNode CreateTree(IEnumerable <int> arr)
        {
            var root = arr.FirstOrDefault();
            var rest = arr.Skip(1).ToArray();
            var tree = new BinaryNode(root);

            tree.Add(rest);
            return(tree);
        }
        static BinaryNode<int> GetSampleTree()
        {
            int[] content = { 5, 3, 7, 2, 4, 6, 8 };

            BinaryNode<int> root = new BinaryNode<int>(content[0]);

            for (int i = 1; i < content.Length; i++)
            {
                root.Add(content[i]);
            }

            return root;
        }
        /// <summary>
        /// Create a balanced binary search tree with n nodes
        /// </summary>
        static BinaryNode<int> GetBalancedTree(int n)
        {
            List<int> list = new List<int>(n);
            for (int i = 1; i <= n; i++) list.Add(i);

            List<int> balancedList = GetBalancedList(list);

            BinaryNode<int> root = new BinaryNode<int>(balancedList[0]);
            for (int i = 1; i < balancedList.Count; i++)
            {
                root.Add(balancedList[i]);
            }

            return root;
        }
Ejemplo n.º 5
0
        private static void Test_B_Tree_Struct(List <int> randomNumbers, int num_of_iterations)
        {
            Console.WriteLine();
            Console.Write("Starting Struct... ");
            Stopwatch      sw               = new Stopwatch();
            HashSet <long> comparison_set   = new HashSet <long>();
            HashSet <long> milliseconds_set = new HashSet <long>();

            //run XX tests and find average results
            for (int i = 0; i < num_of_iterations; i++)
            {
                sw.Start();

                //now store numbers in B Tree
                BinaryNode b_tree = new BinaryNode();
                foreach (var num in randomNumbers)
                {
                    b_tree.Add(num);
                }

                sw.Stop();

                //gather information for info later
                comparison_set.Add(comparisons);
                milliseconds_set.Add(sw.ElapsedMilliseconds);

                //reset local values
                comparisons = 0;
                sw.Reset();
            }


            Console.WriteLine($"Finished!");
            Console.WriteLine($"Average Adding Nodes in {milliseconds_set.Average()}ms.");
            Console.WriteLine($"Average of {comparison_set.Average()} comparisons.");
            Console.WriteLine();
            //Console.WriteLine($"Max Adding Nodes in {milliseconds_set.Max()}ms.");
            //Console.WriteLine($"Max of {comparison_set.Max()} comparisons.");
            //Console.WriteLine();
        }