/// <summary> /// This helper method will randomize the dictionary created in the last /// method call, and decrease it's size to that specified by dictionarySize /// from the last method call. /// </summary> /// <param name="dictionary">The dictionary to randomize.</param> /// <param name="size">The size of the dictionary specified by the user.</param> /// <returns>The user specified RedBlack Tree dictionary.</returns> private RedBlack RandomizeDictionary( RedBlack dictionary, ref int size ) { RedBlack returnValue = new RedBlack(), nextWord = null; tempTree = new RedBlack(); tempTree.DeepCopy( dictionary, ref tempTree ); Random random = new Random(); for( int dictionSize = 0; dictionSize < size; dictionSize++ ) { /* Next random first character in a word to select for insertion. * Randomly select a lower case alphabet letter from (97)'a' to (122)'z'. */ char firstChar = (char)random.Next( 97, 123 ); nextWord = NextWord( dictionary, firstChar ); string word = new string( (char[])nextWord.Key ); if( nextWord == Sentinel.Node ) { dictionSize = dictionSize - 1; usedWords[firstChar] = 0; int actualSize = 0; dictionary.GetActualSize( dictionary, ref actualSize ); if( actualSize == 0 && dictionSize == size - 1 ) { size = dictionSize; return returnValue; } else if( actualSize == 0 && dictionSize != size - 1 ) dictionary = tempTree; } else { dictionary.RB_Delete( ref dictionary, word.ToCharArray() ); returnValue.RB_Insert( ref returnValue, word.ToCharArray() ); } nextWord = null; } return returnValue; }
/// <summary> /// This test will perform 100000 nodal element RB_Insert() operations and then /// attempt to call RB_Delete() on all 100000 elements leaving only the Sentinel.Node /// left in the test root RedBlack Tree and the same thing done on a second RedBlack /// Binary Search Tree. /// </summary> public void Test13() { root = new RedBlack(); RedBlack test2 = new RedBlack(); for(int y = 0; y < 2; y++ ) { for( int x = 0; x < 100000; x++ ) { char [] next = Convert.ToString( x ).ToCharArray(); if( y == 0 ) root.RB_Insert( ref root, next ); else test2.RB_Insert( ref test2, next ); } for( int x = 99999; x > -1; x-- ) { char [] next = Convert.ToString( x ).ToCharArray(); counter = counter + 1; if( y == 0 ) root.RB_Delete( ref root, next ); else test2.RB_Delete( ref test2, next ); } Assert.IsTrue( root == Sentinel.Node || test2 == Sentinel.Node ); } root.Inorder_Tree_Walk( root ); test2.Inorder_Tree_Walk( test2 ); }