示例#1
0
        public void Delete_Unsorted_Check()
        {
            int[] data = { 5, 2, 6, 3, 1, 4 };
            int[] expected_sorted = { 1, 2, 3, 4, 5, 6 };
            int[] expected_delete3 = { 1, 2, 4, 5, 6 };
            int[] expected_delete2 = { 1, 4, 5, 6 };
            int[] expected_delete6 = { 1, 4, 5 };
            int[] expected_delete1 = { 4, 5 };

            IBinarySearchTree<int> tree = new BinarySearchTree<int>();

            foreach (int value in data)
                tree.Insert(value);

            int[] actualArray = tree.ToArray();

            CollectionAssert.AreEqual(expected_sorted, actualArray);

            Assert.IsTrue(tree.Delete(3));
            CollectionAssert.AreEqual(expected_delete3, tree.ToArray());

            Assert.IsTrue(tree.Delete(2));
            CollectionAssert.AreEqual(expected_delete2, tree.ToArray());

            Assert.IsTrue(tree.Delete(6));
            CollectionAssert.AreEqual(expected_delete6, tree.ToArray());

            Assert.IsTrue(tree.Delete(1));
            CollectionAssert.AreEqual(expected_delete1, tree.ToArray());
        }
示例#2
0
        public void Delete_EntireArray()
        {
            int[] data = { 5, 2, 6, 3, 1, 4 };
            int[] expected = { 1, 2, 3, 4, 5, 6 };
            IBinarySearchTree<int> tree = new BinarySearchTree<int>();

            foreach (int value in data)
                tree.Insert(value);

            int[] actualArray = tree.ToArray();
            CollectionAssert.AreEqual(expected, actualArray);
            Assert.AreEqual(expected.Length, tree.Size);
            Assert.IsTrue(tree.Delete(1));
            Assert.IsFalse(tree.Find(1));
            Assert.IsTrue(tree.Delete(2));
            Assert.IsFalse(tree.Find(2));
            Assert.IsTrue(tree.Delete(3));
            Assert.IsFalse(tree.Find(3));
            Assert.IsTrue(tree.Delete(4));
            Assert.IsFalse(tree.Find(4));
            Assert.IsTrue(tree.Delete(5));
            Assert.IsFalse(tree.Find(5));
            Assert.IsTrue(tree.Delete(6));
            Assert.IsFalse(tree.Find(6));
            Assert.AreEqual(0, tree.Size);
        }
        public void ClearAllItemsInList()
        {
            var tree = new BinarySearchTree<int> {5, 3, 7};

            tree.Clear();

            Assert.That(tree, Is.EqualTo(new int[0]));
        }
示例#4
0
        public void Delete_Single()
        {
            const int insertValue = 1;

            IBinarySearchTree<int> tree = new BinarySearchTree<int>();
            tree.Insert(insertValue);

            Assert.IsTrue(tree.Find(insertValue));
            Assert.IsTrue(tree.Delete(insertValue));
            Assert.IsFalse(tree.Find(insertValue));
        }
示例#5
0
文件: bst_test.cs 项目: hsn6/training
        public static void AddRandomNumbers(BinarySearchTree bst, int minVal, int maxVal, int n)
        {
            int[] a = Utils.GenerateRandomDistinctArray(minVal, maxVal, n);
            //int[] a = new int[] {  41, 16, 76, 66, 20, 83, 12, 38, 86, 97, 53, 55, 60, 92, 1, 25, 31, 74, 45, 5 };
            Utils.PrintArray(a, "Input distinct arr: ");

            if (a != null)
            {
                foreach (int val in a)
                {
                    bst.Insert(val);
                }
            }

            bst.PrintInOrderRecursive();
            bst.PrintInOrderNonRecursive();
        }
示例#6
0
文件: bst_test.cs 项目: hsn6/training
        public static void Main(string[] args)
        {
            BinarySearchTree bst = new BinarySearchTree();

            BinarySearchTreeTest.AddRandomNumbers(bst, 0, 100, 20);
        }
        public void MaximumCalledOnEmptyTreeCausesException()
        {
            var tree = new BinarySearchTree<int>();

            Assert.Throws<InvalidOperationException>(() => tree.Maximum());
        }
        public void CreateTreeWithOtherComparableTypes()
        {
            var tree = new BinarySearchTree<string> {"Sam", "Joe", "Victoria", "Rufus", "Gill", "Travis", "Zelda"};

            Assert.That(tree, Is.EqualTo(new[] { "Gill", "Joe", "Rufus", "Sam", "Travis", "Victoria", "Zelda" }));
        }
        public void RemoveRootNode()
        {
            var tree = new BinarySearchTree<int> {10};
            tree.Remove(10);

            Assert.That(tree, Is.EqualTo(new int[0]));
        }
        public void SearchForValueThatDoesNotExist()
        {
            var tree = new BinarySearchTree<int> {15, 6, 18, 3, 17, 20};

            Assert.That(tree.Contains(21), Is.EqualTo(false));
        }
        public void RemoveValueFromEmptyTree()
        {
            var tree = new BinarySearchTree<int>();

            Assert.That(tree.Remove(10), Is.False);
            Assert.That(tree, Is.EqualTo(new int[0]));
        }
示例#12
0
        public void Insert_OneValue()
        {
            IBinarySearchTree<int> tree = new BinarySearchTree<int>();
            tree.Insert(1);

            Assert.IsTrue(tree.Find(1));
        }
        public void AddSmallerValueAfterRootNode()
        {
            var tree = new BinarySearchTree<int> {7, 3};

            Assert.That(tree, Is.EqualTo(new[] {3, 7}));
        }
        public void AddSingleValueIntoTree()
        {
            var tree = new BinarySearchTree<int> {7};

            Assert.That(tree, Is.EqualTo(new[] {7}));
        }
 public void AddSameValueTwice()
 {
     var tree = new BinarySearchTree<int> {5, 5};
     Assert.That(tree, Is.EqualTo(new[] { 5, 5}));
 }
        public void AddMoreThanOneLevelOfChildNodes()
        {
            var tree = new BinarySearchTree<int> {15, 6, 18, 3, 17, 20};

            Assert.That(tree, Is.EqualTo(new[] { 3, 6, 15, 17, 18, 20 }));
        }
        public void SearchForValueThatExistsInRightOfTree()
        {
            var tree = new BinarySearchTree<int> {15, 18, 17, 20};

            Assert.That(tree.Contains(17), Is.EqualTo(true));
        }
        public void SearchForValueThatExistsInLeftOfTree()
        {
            var tree = new BinarySearchTree<int> {15, 6, 3, 7};

            Assert.That(tree.Contains(7), Is.EqualTo(true));
        }
示例#19
0
        public void Insert_SortedArray()
        {
            int[] data = {1, 2, 3, 4, 5, 6};
            int[] expected = {1, 2, 3, 4, 5, 6};
            IBinarySearchTree<int> tree = new BinarySearchTree<int>();

            foreach (int value in data)
            {
                tree.Insert(value);
            }

            int[] actual = tree.ToArray();
            CollectionAssert.AreEqual(expected, actual);
        }
示例#20
0
        static void Main(string[] args)
        {
            const int seed = 0;

            int[] sizes =
            {
                1000,
                10000,
                100000,
                1000000
            };

            foreach (var count in sizes)
            {
                Random randNum = new Random(seed);
                int[] random = Enumerable
                    .Repeat(0, count)
                    .Select(i => randNum.Next(0, int.MaxValue))
                    .ToArray();

                int[] randomCopy = new int[random.Length];
                random.CopyTo(randomCopy, 0);
                int[] randomHeapCopy = new int[random.Length];
                random.CopyTo(randomHeapCopy, 0);

                int[] sorted = random.OrderBy(s => s).ToArray();
                TimeSpan orderByTime = MeasureSort(s => s.OrderBy(t => t).ToArray(), random, sorted);
                TimeSpan sortTime = MeasureSort(s =>
                                                {
                                                    Array.Sort(s);
                                                    return s;
                                                }, randomCopy, sorted);
                TimeSpan heapTime = MeasureSort(Heap.HeapSort, random, sorted.Reverse().ToArray());
                TimeSpan inplaceHeapTime = MeasureSort(Heap.InplaceHeapSort, randomHeapCopy, sorted.Reverse().ToArray());
                TimeSpan bstTime = MeasureSort(s =>
                {
                    BinarySearchTree<int> tree = new BinarySearchTree<int>();

                    foreach (int val in s)
                    {
                        tree.Insert(val);
                    }

                    return tree.ToArray();
                }, random, sorted);
                TimeSpan avlTime = MeasureSort(s =>
                {
                    AvlTree<int> tree = new AvlTree<int>();

                    foreach (int val in s)
                        tree.Insert(val);

                    return tree.ToArray();
                }, random, sorted);

                Console.WriteLine("----- {0} -----", count);
                Console.WriteLine("Heap:               {0}", heapTime.TotalSeconds);
                Console.WriteLine("Inplace Heap:       {0}", inplaceHeapTime.TotalSeconds);
                Console.WriteLine("Binary search tree: {0}", bstTime.TotalSeconds);
                Console.WriteLine("AVL tree:           {0}", avlTime.TotalSeconds);
                Console.WriteLine("Sort:               {0}", sortTime.TotalSeconds);
                Console.WriteLine("OrderBy:            {0}", orderByTime.TotalSeconds);
            }

            Console.ReadLine();
        }
        public void RemoveValueThatDoesNotExist()
        {
            var tree = new BinarySearchTree<int> {10, 7, 11, 8};

            Assert.That(tree.Remove(13), Is.False);
            Assert.That(tree, Is.EqualTo(new[] { 7, 8, 10, 11 }));
        }
示例#22
0
        public void Insert_OneValue_ToArray()
        {
            IBinarySearchTree<int> tree = new BinarySearchTree<int>();
            tree.Insert(1);
            int[] sortedArray = tree.ToArray();

            Assert.IsTrue(tree.Find(1));
            Assert.AreEqual(1, sortedArray.Length);
            Assert.AreEqual(1, sortedArray[0]);
        }
示例#23
0
        public void TestSize()
        {
            var bst = new BinarySearchTree<char, int>();
            Assert.AreEqual(0, bst.Size());
            bst.Put('A', 1);
            Assert.AreEqual(1, bst.Size());

            Assert.AreEqual(_result.Length, CreateBST().Size());
        }
示例#24
0
        public void Insert_SmallUnsortedArray()
        {
            int[] data = new[] {5, 2, 6, 3, 1, 4};
            int[] expected = {1, 2, 3, 4, 5, 6};
            IBinarySearchTree<int> tree = new BinarySearchTree<int>();

            foreach (int value in data)
                tree.Insert(value);

            int[] actualArray = tree.ToArray();
            CollectionAssert.AreEqual(expected, actualArray);
        }
        public void SearchForValueInEmptyTree()
        {
            var tree = new BinarySearchTree<int>();

            Assert.That(tree.Contains(13), Is.EqualTo(false));
        }
        public void Count()
        {
            var tree = new BinarySearchTree<int> {1, 2, 3};

            Assert.That(tree.Count, Is.EqualTo(3));
        }
        public void CountOnEmptyTree()
        {
            var tree = new BinarySearchTree<int>();

            Assert.That(tree.Count, Is.EqualTo(0));
        }
示例#28
0
 public void TestIsEmpty()
 {
     var bst = new BinarySearchTree<char, int>();
     Assert.True(bst.IsEmpty());
     bst.Put('A', 1);
     Assert.False(bst.IsEmpty());
 }
        public void CreateEmptyTree()
        {
            var tree = new BinarySearchTree<int>();

            Assert.That(tree, Is.EqualTo(new int[] {}));
        }
示例#30
0
 private static BinarySearchTree<string, int> CreateBST()
 {
     ;
     var result = new BinarySearchTree<string, int>(StringComparer.Ordinal);
     for (int i = 0; i < _fixture.Length; i++) {
         result.Put(_fixture[i], i);
     }
     return result;
 }
示例#31
0
        public void DoTreeStuff()
        {
            var tree = new BinarySearchTree();

            tree.Insert(9);
            tree.Insert(4);
            tree.Insert(6);
            tree.Insert(20);
            tree.Insert(170);
            tree.Insert(15);
            tree.Insert(1);

            for (int i = 0; i < 10; i++)
            {
                TreeNode randomNode = tree.GetRandomNode2();
                Debug.WriteLine("random tree node: " + randomNode.Data);
            }

            Debug.WriteLine(tree.MaxDepth(tree.root));
            var ancestor = tree.LowestCommonAncestor(6, 1);

            tree.TraverseInOrder();
            //tree.Insert(100);
            //tree.Insert(50);
            //tree.Insert(70);
            //tree.Insert(40);
            //tree.Insert(43); //subtree
            //tree.Insert(42);
            //tree.Insert(44);
            //tree.Lookup(6);
            //tree.Remove(20);
            //var queue = new Queue<TreeNode>();
            //queue.Enqueue(tree.root);
            //var values = tree.BfsRecursive(queue, new List<int>());

            var math = new MyMath();

            math.MyPow(23.4, -6);

            var values = tree.DFSInOrder();

            foreach (var num in values)
            {
                Debug.Write(num + " ");
            }


            //        9
            //   4        20
            // 1   6   15    170

            //DFS

            //left, data, right: data is the parent node
            //ldr
            //InOrder - 1,4,5,9,15,20,170
            //dlr
            //PreOrder - 9,4,1,6,20,15,170
            //lrd
            //PostOrder - 1,6,4,15,20,170,9
        }