/// <summary> /// Polymorphically overrideable method for respective parsing. /// </summary> /// <param name="head">This RedBlack Tree node is the head node of the /// tree representing the dictionary.</param> protected override void Parse( RedBlack head ) { int threeSlot = 0; while( y < yBoundary ) // stop condition { char firstChar = (char)( random.Next( 97, 123 ) ); RedBlack tempNode = dictionaryCreator.NextWord( head, firstChar ); if( tempNode == Sentinel.Node ) { dictionaryCreator.UsedWords[firstChar] = 0; continue; } char[] word = (char[]) tempNode.Key; if( ( random.Next( 2 ) ) == 1 ) word = Reverse( word ); slots = CountInsertLength( x, y, z ); if( word.Length <= slots ) InsertWord( word, x, y, z ); else if( slots > 2 && word.Length > slots && threeSlot < 10 ) { // Last insert failed, leave both for loops to get next word // and available set of slots. threeSlot = (slots == 3) ? threeSlot + 1 : threeSlot; continue; } if( (slots = slots - word.Length) > 2 ) // Check for slots remaining after the last InsertWord() continue; else { z = z - 1; if( z < 3 ) { x = x + 1; z = zBoundary - 1; } if( x > xBoundary - 3 ) { y = y + 1; x = 0; } if( y >= yBoundary ) { switch( checkAgain ) { case 0: y = yBoundary - 1; checkAgain++; break; case 1: y = yBoundary; break; default: break; } } } threeSlot = 0; } // end of while( y < yBoundary ) }
/// <summary> /// RedBlack default constructor /// </summary> public RedBlack() { color = "black"; key = null; left = null; right = null; parent = null; }
public void SetUpTests() { root = new RedBlack(); val = ""; myString = ""; counter = 0; }
/// <summary> /// This method will correctly find the RedBlack node with key k in the tree /// whose reference is x. This search will always be more efficient since this /// is not making recursive calls, except just moving memory references. /// </summary> /// The RedBlack node <param name="x"> is the root node to search through. </param> /// The key <param name="k"> to search for in the tree on or under node x. </param> /// <returns> The RedBlack node in the tree on or under node x with key k, but /// performs the search more efficiently by "unrolling" the recursion into a /// while loop. If not found, the Sentinel node is returned. /// If not found, the Sentinel node is returned.</returns> public RedBlack Iterative_Tree_Search( RedBlack x, object k ) { while( x != Sentinel.Node && !RB_EqualityOperator( k, x.key ) ) { if( RB_LessThanOperator( k , x.key ) ) x = x.left; else x = x.right; } if( x != Sentinel.Node ) // Did the search succeed? Assert.IsTrue( RB_EqualityOperator( x.key, k ) ); return x; }
/// <summary> /// After this method executes, the subnode x at or under the absolute /// root node T will rotate references with x's left child node. /// </summary> /// <param name="T">This is the absolute root node containing or is /// the RedBlack node x.</param> /// <param name="x">This is the RedBlack node that will be rotated /// left with x's right child node</param> private void Right_Rotate( ref RedBlack T, RedBlack x ) { RedBlack y = x.left; if( y != null ) x.left = y.right; if( y != null && y.right != Sentinel.Node) y.right.parent = x; if( y != null && y != Sentinel.Node ) y.parent = x.parent; if( x != null && x.parent != null && x.parent != Sentinel.Node ) { if( x == x.parent.right ) x.parent.right = y; else x.parent.left = y; } else { T = GetRoot( T ); if( y != null ) T = y; } if( y != null ) y.right = x; if( x != null && x != Sentinel.Node ) x.parent = y; // Assert.IsTrue( x.parent == y && y.right == x ); }
/// <summary> /// The method will initialize the DictionaryListBox on the first load of the /// Main.aspx page. /// </summary> /// <param name="next">This is the root RedBlack Tree Solution.dictionary node.</param> /// <param name="sortChar">Beginning letter of dictionary word to add to list.</param> public void InitializeDictionaryListBox( RedBlack next, char sortChar ) { if( !Page.IsPostBack && next == managestats.solution.Dictionary ) { list_counter = 0; DictionaryViewControl.Items.Add( new ListItem( "Select All Words" ) ); for( int x = 'a'; x <= (int)'z'; x++ ) DictionaryViewControl.Items.Add( new ListItem( ((char)x).ToString() )); } if( next != Sentinel.Node ) { InitializeDictionaryListBox( next.Left, sortChar ); if( list_counter >= 2000 ) return; else if( ((char[])next.Key)[0] == sortChar || sortChar == ' ' ) { list_counter = list_counter + 1; string str = new string((char[])next.Key); DictionaryListBox.Items.Add( new ListItem( str ) ); } InitializeDictionaryListBox( next.Right, sortChar ); } }
/// <summary> /// (Deprecated) This operation will serialize the RedBlack Binary Search Tree /// Dictionary object into a soap message for transport to the DataBase. /// </summary> /// <param name="dictionaryRoot">The dictionary root RedBlack node.</param> /// <returns>True if the serialization succeeded, false if an exception ocurred.</returns> public bool SoapSerializeDictionary( RedBlack dictionaryRoot ) { bool retval = false; try { //soapFormatter.Serialize( /* Need new Stream destination */, Solution.dictionary ); retval = true; } catch (System.Runtime.Serialization.SerializationException e) { StreamWriter error = new StreamWriter( "\\\\" + Dns.GetHostName() + "\\thepuzzler_3dstyle\\DictionaryCreator\\SerializationError.txt", false ); error.Write("Failed to serialize. Reason: \n\n" + e.Message); retval = false; throw; } return retval; }
/// <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 helper method will retrieve each dictionary word (those found in the /// randomly generated dictonary), in desending order, one at a time, and check /// each one of them against every sub list entry in the possibilities RedBlack Binary /// Search Tree which represents every word in the puzzle with three or more /// characters. Each matched word is then added to a separate RedBlack Tree called /// wordsFound. /// </summary> /// <param name="nextDictionaryWord">The root RedBlack node of the dictionary /// tree list.</param> /// <param name="direction">This specifies which direction to search all the word /// possibilities that matchup with something, if anything, inside the /// dictionary.</param> private void GetMatchedWords( RedBlack nextDictionaryWord, int direction ) { if( nextDictionaryWord != Sentinel.Node ) { GetMatchedWords( nextDictionaryWord.Left, direction ); RedBlack foundWord = possibilities[direction].Iterative_Tree_Search( possibilities[direction], nextDictionaryWord.Key ); if( foundWord != Sentinel.Node ) { wordFindings[direction].RB_Insert( ref wordFindings[direction], foundWord.Key ); numWordsCOPY[direction] = numWordsCOPY[direction] + 1; } GetMatchedWords( nextDictionaryWord.Right, direction ); } }
/// <summary> /// This method will correctly find the RedBlack node with key k /// in the tree whose reference is x. /// </summary> /// The RedBlack node <param name="x"> is the root node to search through. </param> /// The key <param name="k"> to search for in the tree on or under node x. </param> /// <returns> The RedBlack node in the tree on or under node x with key k. /// If not found, the Sentinel node is returned.</returns> public RedBlack Tree_Search( RedBlack x, object k ) { Assert.IsTrue( x.color == "black" || x.color == "red" ); if( x == Sentinel.Node || RB_EqualityOperator( k, x.key ) ) return x; if( RB_LessThanOperator( k , x.key ) ) return Tree_Search( x.left, k ); else return Tree_Search( x.right, k ); }
/// <summary> /// This method finds the smallest key greater than the key of node x. /// </summary> /// <param name="x">Is any given RedBlack node of which ever root node.</param> /// <returns>This method returns the RedBlack node whose key is the smallest /// key greater than the key of node x.</returns> public RedBlack Tree_Successor( RedBlack x ) { if( x.right != Sentinel.Node ) return Tree_Minimum( x.right ); RedBlack y = x.parent; while( y != Sentinel.Node && x == y.right ) { x = y; y = y.parent; } Assert.IsTrue( y == Sentinel.Node && x != y.right ); return y; }
/// <summary> /// This method finds the minimum node whose value is the smallest beneath /// a given root node x by following the left most node of that tree on and /// under the root node x. /// </summary> /// The RedBlack node <param name="x"> is the root node of the subtree.</param> /// <returns>The node whose key is the smallest value under node x.</returns> public RedBlack Tree_Minimum( RedBlack x ) { while( x.left != Sentinel.Node ) x = x.left; Assert.IsTrue( x.left == Sentinel.Node ); return x; }
/// <summary> /// This method finds the largest key smaller than the key of node x. /// </summary> /// <param name="x">Is any given RedBlack node of which ever root node.</param> /// <returns>This method returns the RedBlack node whose key is the largest /// key smaller than the key of node x.</returns> public RedBlack Tree_Predecessor( RedBlack x ) { if( x.left != Sentinel.Node ) return Tree_Maximum( x.left ); RedBlack y = x.parent; while( y != Sentinel.Node && x == y.left ) { x = y; y = y.parent; } Assert.IsTrue( y != Sentinel.Node && x == y.left ); return y; }
/// <summary> /// This method returns the node with the maximum value on and under a given /// root RedBlack node x. The key with the maximum value is always found by /// following the right child references is that subtree. This exists as a /// characteristic property of a binary search tree. /// </summary> /// The RedBlack node <param name="x"> is the root node of the tree to find /// the maximum valued node. </param> /// <returns>The RedBlack node on or under x with the maximum key value.</returns> public RedBlack Tree_Maximum( RedBlack x ) { while( x.right != Sentinel.Node ) x = x.right; Assert.IsTrue( x.right == Sentinel.Node ); return x; }
/// <summary> /// This two arguement operation accepts a System.object value and creates /// a new RedBlack node by first calling the default RedBlack() default /// constructor then inserting the resultant new node. /// Please note that RB_Insert() operation will return any duplicates /// attempted as a RedBlack node back to the caller. /// </summary> /// <param name="T">The root/head of the RedBlack Tree to insert char [] value.</param> /// <param name="valueToInsert">The object value to insert.</param> /// <returns>Returns the duplicate node compared to the node to be /// inserted, other wise inserts the node.</returns> public RedBlack RB_Insert( ref RedBlack T, object valueToInsert ) { RedBlack x = new RedBlack(); if( Sentinel.Node == null ) Sentinel.Create(); if( T.Key == null ) T = Sentinel.Node; if( valueToInsert is string ) x.key = (string)valueToInsert; else if( valueToInsert is char[] ) x.key = (char[])valueToInsert; else x.key = valueToInsert; if( Iterative_Tree_Search( T, x.key ) != Sentinel.Node ) return x; else return x.Insert( ref T, x ); }
/// <summary> /// The method must be provided to properly delete a given RedBlack node /// from the RedBlack Tree that we're working with to properly maintain /// the five properties of the Red Black Binary Search Tree. /// </summary> /// <param name="T">Head or Root of the RedBlack Tree we're working with.</param> /// <param name="Value">The RedBlack node whose key is specified by this /// paramenter that we're going to delete.</param> /// <returns>The last RedBlack node worked with.</returns> public RedBlack RB_Delete( ref RedBlack T, object Value ) { RedBlack x, y, z; if( (z = this.Iterative_Tree_Search( T, Value )) == Sentinel.Node ) return z; else if( z.left == Sentinel.Node || z.right == Sentinel.Node ) y = z; else y = Tree_Successor( z ); if( y.left != Sentinel.Node ) x = y.left; else x = y.right; x.parent = y.parent; if( y.parent != null && y.parent != Sentinel.Node ) { if( y == y.parent.left ) y.parent.left = x; else y.parent.right = x; } else { T = GetRoot( T ); T = x; } if( y != z ) { // Member-wise copy for all satellie data z.key = y.key; } if( y.color == "black" ) RB_Delete_Fixup( ref T, x ); Assert.IsTrue( y != null && y.key != null ); return y; }
/// <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 ); }
/// <summary> /// The purpose of this method is to find the root node of any given /// RedBlack Binary Search Tree. /// </summary> /// <param name="T">An arbitrary RedBlack Tree to find the root.</param> /// <returns>This method returns the root of a given RedBlack Tree T.</returns> private RedBlack GetRoot( RedBlack T ) { // The root node's parent will always refer back to Sentinel node. while( T.parent != Sentinel.Node ) { if( T.parent == null ) return T; // T == Sentinel.Node, return T if( T.key == null && T.parent.key != null && T.left == null && T.Right == null ) return T; // T == Sentinel.Node, return T else T = T.parent; } Assert.IsTrue( T.parent == Sentinel.Node ); return T; }
/// <summary> /// This helper operation will search for a word in the base dictionary /// whose first letter will match the randomly generated charater in /// the previous method call. If nothing is found, then the Sentinel /// Node is returned. /// </summary> /// <param name="word">Base dictionary to look through.</param> /// <param name="firstChar">Randomly selected first character to look for.</param> /// <returns>The RedBlack node word whose key has a first character /// that matches the randomly generated character as an in/out passed by reference /// parameter.</returns> public RedBlack NextWord( RedBlack word, char firstChar ) { string temp = null; int wordEncountered = 0; while( word != Sentinel.Node ) { temp = new string((char[])word.Key); if( temp[0] == firstChar && wordEncountered >= (int)usedWords[firstChar] ) break; if( temp[0] == firstChar ) wordEncountered++; if( firstChar < temp[0] ) word = word.Left; else word = word.Right; } if( word != Sentinel.Node ) { usedWords[firstChar] = (int)usedWords[firstChar] + 1; Assert.IsTrue( temp[0] == firstChar ); // Did the search succeed? } return word; }
/// <summary> /// This method accepts a reference to the root or head of the RedBlack Tree T /// and a reference to the RedBlack node to insert z, whose key fill must filled /// in with sortable data. Lastly this method calls RB_Insert_Fixup( T, z ) to /// maintain properly the five properties of the RedBlack Tree. /// </summary> /// <param name="T">A reference to the root or head of the RedBlack Tree T.</param> /// <param name="z">A reference to the RedBlack node we'll insert z.</param> /// <returns>Returns the node just inserted.</returns> private RedBlack Insert( ref RedBlack T, RedBlack z ) { RedBlack y = Sentinel.Node; RedBlack x = GetRoot( T ); while( x != Sentinel.Node ) { y = x; if( RB_LessThanOperator( z.key, x.key ) ) x = x.left; else x = x.right; } z.parent = y; if( y == Sentinel.Node ) { T = GetRoot( T ); T = z; } else if( RB_LessThanOperator( z.key, y.key ) ) y.left = z; else y.right = z; z.left = Sentinel.Node; z.right = Sentinel.Node; z.color = "red"; RB_Insert_Fixup( ref T, z ); Assert.IsTrue( T.left != null || T.right != null ); return z; }
/// <summary> /// Default Solution Constuctor Sets up all of the Solution Class Components /// </summary> public Solution() { if( possibilities == null && dictionary == null && puzzle == null && wordsFound == null ) { numWordsFound = 0; wordsFound = new RedBlack(); possibilities = new RedBlack[ 13 ]; numWordsCOPY = new int[ 13 ]; wordFindings = new RedBlack[ 13 ]; for( int x = 0; x < 13; ++x ) { possibilities[x] = new RedBlack(); wordFindings[x] = new RedBlack(); numWordsCOPY[x] = 0; } dictionary = new RedBlack(); puzzle = new char[ PuzzleConfiguration.puzzleSizes[0], PuzzleConfiguration.puzzleSizes[1], PuzzleConfiguration.puzzleSizes[2] ]; // <-- Set the puzzle's dimentions here! xbound = puzzle.GetUpperBound( 0 ) + 1; ybound = puzzle.GetUpperBound( 1 ) + 1; zbound = puzzle.GetUpperBound( 2 ) + 1; isMultiThreaded = true; } }
/// <summary> /// This helper method/operation is meant to provide support for maintaining /// the neccessary five RedBlack Tree properties after having called RB_Delete() /// </summary> /// <param name="T">Head or Root of the RedBlack Tree we're working with.</param> /// <param name="x">RedBlack node we're going to "fixup".</param> private void RB_Delete_Fixup( ref RedBlack T, RedBlack x ) { RedBlack w; while( x != ( T = GetRoot( T ) ) && x.color == "black" ) { if( x.parent.left != null && x == x.parent.left ) { w = x.parent.right; if( w != null && w.color == "red" ) { w.color = "black"; x.parent.color = "red"; Left_Rotate( ref T, x.parent ); w = x.parent.right; } if( w != null && w.right != null && w.left != null && w.left.color == "black" && w.right.color == "black" ) { w.color = "red"; x = x.parent; } else { if( w != null && w.right != null && w.right.color == "black" ) { w.left.color = "black"; w.color = "red"; Right_Rotate( ref T, w ); w = x.parent.right; } if( w != null && x != null && x.parent != null ) w.color = x.parent.color; if( x != null && x.parent != null ) x.parent.color = "black"; if( w != null && w.right != null ) w.right.color = "black"; Left_Rotate( ref T, x.parent ); x = GetRoot( T ); } } // End if( x == x.parent.left ) {} else { w = x.parent.left; if( w != null && w.color == "red" ) { w.color = "black"; x.parent.color = "red"; Right_Rotate( ref T, x.parent ); w = x.parent.left; } if( w != null && w.right != null && w.left != null && w.right.color == "black" && w.left.color == "black" ) { w.color = "red"; x = x.parent; } else { if( w != null && w.left != null && w.left.color == "black" ) { w.right.color = "black"; w.color = "red"; Left_Rotate( ref T, w ); w = x.parent.left; } if( w != null && x != null && x.parent != null ) w.color = x.parent.color; if( x != null && x.parent != null ) x.parent.color = "black"; if( w != null && w.left != null ) w.left.color = "black"; Right_Rotate( ref T, x.parent ); x = GetRoot( T ); } // End if( w.right.color == "black" && w.left.color == "black" ) {} } // End else clause } // End while( x != GetRoot( T ) && x.color == "black" ) {} x.color = "black"; }
/// <summary> /// This helper method visits all of the individual sub lists /// for this words found and combines them into on wordsFound list. /// </summary> /// <param name="nextFoundWord">Specifies the next words found /// sub list to sort through all it's individual nodes.</param> private void VisitWordFoundSubList( RedBlack nextFoundWord ) { if( nextFoundWord != Sentinel.Node ) { VisitWordFoundSubList( nextFoundWord.Left ); wordsFound.RB_Insert( ref wordsFound, nextFoundWord.Key ); VisitWordFoundSubList( nextFoundWord.Right ); } }
/// <summary> /// This helper method is used to help with the conversion process from a RedBlack /// Binary Search Tree to a string[] for easier serialization. /// </summary> /// <param name="node">The root/next RedBlack Binary Search Tree node in the list /// to visit.</param> /// <param name="convertToItem">The string [] reference to convert the RedBlack Binary /// Search Tree to.</param> private void VisitRedBlackTree( RedBlack node, ref string [] convertToItem ) { if( node != Sentinel.Node ) { VisitRedBlackTree( node.Left, ref convertToItem ); convertToItem[ index++ ] = new string( (char[])node.Key ); VisitRedBlackTree( node.Right, ref convertToItem ); } }
/// <summary> /// This method will populate the WordsFound ListBox control. /// </summary> /// <param name="next">This is the Solution.wordsFound RedBlack Tree root node.</param> /// <param name="sortChar">Beginning letter of dictionary word to add to list.</param> public void InitializeWordsFoundListBox( RedBlack next, char sortChar ) { if( WordsMatchedViewControl.Items.Count == 0 && next == managestats.solution.WordsFound ) { list_counter = 0; WordsMatchedViewControl.Items.Add( new ListItem( "Select All Words" ) ); for( int x = 'a'; x <= (int)'z'; x++ ) WordsMatchedViewControl.Items.Add( new ListItem( ((char)x).ToString() )); } if( next != Sentinel.Node ) { InitializeWordsFoundListBox( next.Left, sortChar ); if( list_counter >= 2000 ) return; else if( ((char[])next.Key)[0] == sortChar || sortChar == ' ' ) { list_counter = list_counter + 1; string str = new string( (char[]) next.Key ); WordsFound.Items.Add( new ListItem( str ) ); } InitializeWordsFoundListBox( next.Right, sortChar ); } }
/// <summary> /// The helper method will walk through a given tree, in order, and /// count the actual number of words in the tree. /// </summary> /// <param name="next">The root RedBlack node of the RedBlack Tree.</param> /// <param name="actualSize">The integer counter used to do the counting.</param> public void GetActualSize( RedBlack next, ref int actualSize ) { if( next != Sentinel.Node ) { GetActualSize( next.Left, ref actualSize ); actualSize = actualSize + 1; GetActualSize( next.Right, ref actualSize ); } }
/// <summary> /// This operation will ready the RedBlack dictionary to an array of strings, and the /// RedBlack wordsFound to an array of strings. This done so the serialization of the data /// objects become extremely easy for section 5 array encoding, and even easier for each data /// object's ultimate destination to the server side database. /// </summary> /// <param name="RedBlack_Dictionary">The RedBlack dictionary to convert.</param> /// <param name="puzzle">The char [,,] to convert.</param> /// <param name="RedBlack_wordsFound">The RedBlack wordsFound to convert.</param> /// <param name="solution">The solution object class reference.</param> private void SetDataForStorage( RedBlack RedBlack_Dictionary, char[,,] puzzle, RedBlack RedBlack_wordsFound, ref Solution solution ) { try { // Serialize the dictinary string path = @"\\" + System.Net.Dns.GetHostName() + @"\thepuzzler_3dstyle\ManageDB\dictionary.xml"; writer = new StreamWriter( path ); serializer = new XmlSerializer( typeof(string[]), "http://ns_DictionaryCreator" ); RedBlack_Dictionary.GetActualSize( RedBlack_Dictionary, ref index ); string [] dictionary = new string[index]; index = 0; VisitRedBlackTree( RedBlack_Dictionary, ref dictionary ); serializer.Serialize( writer.BaseStream, dictionary ); writer.Close(); reader = new StreamReader( path ); dictionaryXML = reader.ReadToEnd(); reader.Close(); // Serialize the puzzle path = @"\\" + System.Net.Dns.GetHostName() + @"\thepuzzler_3dstyle\ManageDB\puzzle.xml"; writer = new StreamWriter( path ); serializer = new XmlSerializer( typeof(char[][][]), "http://Puzzle_Creation" ); char[][][] jaggedPuzzle = ConvertTo3DJaggedArray( puzzle ); serializer.Serialize( writer.BaseStream, jaggedPuzzle ); writer.Close(); reader = new StreamReader( path ); puzzleXML = reader.ReadToEnd(); reader.Close(); // Serialize the wordsFound path = @"\\" + System.Net.Dns.GetHostName() + @"\thepuzzler_3dstyle\ManageDB\wordsFound.xml"; writer = new StreamWriter( path ); serializer = new XmlSerializer( typeof(string[]), "http://Puzzle_Solution" ); index = 0; RedBlack_wordsFound.GetActualSize(RedBlack_wordsFound, ref index); string [] wordsFound = new string[index]; index = 0; VisitRedBlackTree( RedBlack_wordsFound, ref wordsFound ); serializer.Serialize( writer.BaseStream, wordsFound ); writer.Close(); reader = new StreamReader( path ); wordsFoundXML = reader.ReadToEnd(); reader.Close(); } catch( Exception ex ) { new ManageDB_TestClass().HandleException( ref ex ); } finally { writer.Close(); reader.Close(); } }
/// <summary> /// This method will correctly and in order access each key /// in the binary search red-black tree, and in sorted order. /// </summary> /// <param name="x">Node to start accessing keys in the subtree of this node. </param> public void Inorder_Tree_Walk( RedBlack x ) { Assert.IsNotNull( x.color ); if( x != Sentinel.Node ) { Inorder_Tree_Walk( x.left ); string Value = ( x.key is char[] ) ? new string( (char[]) x.key ) : (string) x.key; Trace.WriteLine( "key value=\"" + Value + "\" color = \"" + x.color + "\""); Inorder_Tree_Walk( x.right ); } }
/// <summary> /// This is primarily a helper method/operation meant to maintain the /// five properties of the RedBlack Binary Search Tree. /// </summary> /// <param name="T">Root or Head of the RedBlack tree we're working with.</param> /// <param name="z">RedBlack node being inserted.</param> private void RB_Insert_Fixup( ref RedBlack T, RedBlack z ) { RedBlack y; while( z.parent.color == "red" ) { if( z.parent == z.parent.parent.left ) { y = z.parent.parent.right; if(y != null && y.color == "red" ) { z.parent.color = "black"; y.color = "black"; z.parent.parent.color = "red"; z = z.parent.parent; } else { if( z == z.parent.right ) { z = z.parent; Left_Rotate( ref T, z ); } z.parent.color = "black"; z.parent.parent.color = "red"; Right_Rotate( ref T, z.parent.parent ); } } else { y = z.parent.parent.left; if( y != null && y.color == "red" ) { z.parent.color = "black"; y.color = "black"; z.parent.parent.color = "red"; z = z.parent.parent; } else { if( z == z.parent.left ) { z = z.parent; Right_Rotate( ref T, z ); } z.parent.color = "black"; z.parent.parent.color = "red"; Left_Rotate( ref T, z.parent.parent ); } } // end else clause } // end while loop T = GetRoot( T ); T.color = "black"; }
/// <summary> /// Provides a mean to execute a deep copy from one RedBlack Tree /// to Another. /// </summary> /// <param name="treeSource">The source RedBlack Binary Search Tree /// to copy from.</param> /// <param name="treeDestination">The destination RedBlack Binary Search /// Tree to send the copy to.</param> /// <returns>The root node reference to the deep copied tree.</returns> public void DeepCopy( RedBlack treeSource, ref RedBlack treeDestination ) { if( treeDestination == null ) treeDestination = new RedBlack(); else if( treeSource != Sentinel.Node ) { DeepCopy( treeSource.left, ref treeDestination ); treeDestination.RB_Insert( ref treeDestination, treeSource.Key ); DeepCopy( treeSource.right, ref treeDestination ); } }