Example #1
0
        public void BinarySearchTreeAddTest()
        {
            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 100; j++)
                {
                    int[] data = ArrayUtilities.CreateRandomArray(j, 0, BinarySearchTreeTests.MaxValue);

                    BinaryTreeNode <int> root = null;

                    for (int k = 0; k < data.Length; k++)
                    {
                        if (k == 0)
                        {
                            root = new BinaryTreeNode <int>(data[k]);
                        }
                        else
                        {
                            BinarySearchTree.Insert(root, data[k]);
                        }

                        BinarySearchTreeTests.ValidateTree(root, data, k + 1);
                    }
                }
            }
        }
Example #2
0
        public void BinarySearchTreeDeleteTest()
        {
            for (int i = 0; i < 10; i++)
            {
                for (int j = 1; j < 100; j++)
                {
                    int[] data = Utilities.ArrayUtilities.CreateRandomArray(j, 0, BinarySearchTreeTests.MaxValue);

                    BinaryTreeNode <int> root = new BinaryTreeNode <int>(data[0]);

                    for (int k = 1; k < data.Length; k++)
                    {
                        BinarySearchTree.Insert(root, data[k]);
                    }

                    BinarySearchTreeTests.ValidateTree(root, data, data.Length);

                    List <int> list   = new List <int>(data);
                    Random     random = new Random();

                    while (list.Count > 0)
                    {
                        int index = random.Next(0, list.Count);
                        int x     = list[index];

                        BinaryTreeNode <int> toRemove  = BinarySearchTree.Find(root, x);
                        BinaryTreeNode <int> successor = BinaryTree.Delete(toRemove);

                        if (toRemove == root)
                        {
                            root = successor;
                        }

                        list.RemoveAt(index);
                        int[] remaining = list.ToArray();
                        ValidateTree(root, remaining, remaining.Length);
                    }
                }
            }
        }