Archive for May, 2007

Web hosting domains - 1180 Data Structures and Collections Chapter 23 56

Tuesday, May 8th, 2007

1180 Data Structures and Collections Chapter 23 56 return rightNode; 57 } 58 59 set 60 { 61 rightNode = value; 62 } 63 } 64 65 // insert TreeNode into Tree that contains nodes; 66 // ignore duplicate values 67 public void Insert( IComparable insertValue ) 68 { 69 // insert in left subtree 70 if ( insertValue.CompareTo( data ) < 0 ) 71 { 72 // insert new TreeNode 73 if ( leftNode == null ) 74 leftNode = new TreeNode( insertValue ); 75 76 // continue traversing left subtree 77 else 78 leftNode.Insert( insertValue ); 79 } 80 81 // insert in right subtree 82 else if ( insertValue.CompareTo( data ) > 0 ) 83 { 84 // insert new TreeNode 85 if ( rightNode == null ) 86 rightNode = new TreeNode( insertValue ); 87 88 // continue traversing right subtree 89 else 90 rightNode.Insert( insertValue ); 91 } 92 93 } // end method Insert 94 95 } // end class TreeNode 96 97 // class Tree definition 98 public class Tree 99 { 100 private TreeNode root; 101 102 // construct an empty Tree of integers 103 public Tree() 104 { 105 root = null; 106 } 107 Fig. 23.20 Fig. 23.2FiFig. 23.20g. 23.20Definitions of class TreeNodeand Treefor manipulating Fig. 23.20 IComparableobjects. (Part 3 of 5.)
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision j2ee hosting services

Chapter 23 Data Structures and Collections 1179 using (Web hosting providers)

Tuesday, May 8th, 2007

Chapter 23 Data Structures and Collections 1179 using System; 6 7 namespace BinaryTreeLibrary2 8 { 9 // class TreeNode definition class TreeNode 11 { 12 private TreeNode leftNode; 13 private IComparable data; 14 private TreeNode rightNode; 16 // initialize data and make this a leaf node 17 public TreeNode( IComparable nodeData ) 18 { 19 data = nodeData; leftNode = rightNode = null; // node has no children 21 } 22 23 // LeftNode property 24 public TreeNode LeftNode { 26 get 27 { 28 return leftNode; 29 } 31 set 32 { 33 leftNode = value; 34 } } 36 37 // Data property 38 public IComparable Data 39 { get 41 { 42 return data; 43 } 44 set 46 { 47 data = value; 48 } 49 } 51 // RightNode property 52 public TreeNode RightNode 53 { 54 get { Fig. 23.20 Fig. 23.2FiFig. 23.20g. 23.20Definitions of class TreeNodeand Treefor manipulating Fig. 23.20 IComparableobjects. (Part 2 of 5.)
Note: In case you are looking for affordable and reliable webhost to host and run your business application check Vision ftp web hosting services

1178 Data Structures and Collections Chapter 23 capabilities

Monday, May 7th, 2007

1178 Data Structures and Collections Chapter 23 capabilities that enable all objects to be manipulated in a uniform manner. Using such capabilities enables us to design a more flexible data structure. In our next example, we take advantage of C# s polymorphic capabilities by implementing TreeNodeand Treeclasses that manipulate objects of any type that implements interface IComparable (namespace System). It is imperative that we be able to compare objects stored in a binary search, so we can determine the path to the insertion point of a new node. Classes that implement IComparable define method CompareTo, which compares the object that invokes the method with the object that the method receives as an argument. The method returns an intvalue less than zero if the calling object is less than the argument object, zero if the objects are equal, a positive value if the calling object is greater than the argument object. Also, both the calling and argument objects must be of the same data type; otherwise, the method throws an ArgumentException. The program of Fig. 23.20 and Fig. 23.21 enhances the program from Section 23.6.1 to manipulate IComparable objects. One restriction on the new versions of classes TreeNode and Tree in Fig. 23.20 is that each Treeobject can contain objects of only one data type (e.g., all strings or all doubles). If a program attempts to insert multiple data types in the same Tree object, ArgumentExceptions will occur. We modified only six lines of code in class TreeNode(lines 13, 17, 38, 67, 70 and 82) and one line of code in class Tree (line 111) to enable processing of IComparable objects. With the exception of lines 70 and 82, all other changes simply replaced the type intwith the type IComparable. Lines 70 and 82 previously used the < and > operators to compare the value being inserted with the value in a given node. These lines now compare IComparableobjects via the interface s method CompareTo, then test the method s return value to determine whether it is less than zero (the calling object is less than the argument object) or greater than zero (the calling object is greater than the argument object), respectively. Class TreeTest(Fig. 23.21) creates three Treeobjects to store int, doubleand stringvalues, all of which the .NET Framework defines as IComparabletypes. The program populates the trees with the values in arrays intArray (line 15), double- Array(lines 16 17) and stringArray(lines 18 19), respectively. Method populateTree(lines 38 48) receives an Arraycontaining the initializer values for the Tree, a Treeinto which the array elements will be placed and a string representing the Treename as arguments, then inserts each Arrayelement in the Tree. Method traverseTree(lines 51 68) receives a Treeand a stringrepresenting the Treename as arguments, then outputs the preorder, inorder and postorder traversals of the Tree. Note that the inorder traversal of each Treeoutputs the data in sorted order regardless of the data type stored in the Tree. Our polymorphic implementation of class Tree invokes the appropriate data type s CompareTo method to determine the path to each value s insertion point by using the standard binary search tree insertion rules. Also, notice that the Treeof strings appears in alphabetical order. 1 // Fig. 23.20: BinaryTreeLibrary2.cs 2 // Definition of class TreeNode and class Tree for IComparable 3 // objects. 4 Fig. 23.20 Fig. 23.2FiFig. 23.20g. 23.20Definitions of class TreeNodeand Treefor manipulating Fig. 23.20 IComparableobjects. (Part 1 of 5.)
Note: If you are looking for high quality webhost to host and run your jsp application check Vision florida web design services

Web hosts - Chapter 23 Data Structures and Collections 1177 The

Monday, May 7th, 2007

Chapter 23 Data Structures and Collections 1177 The preorder traversal processes the value in each node as the node is visited. After processing the value in a given node, the preorder traversal processes the values in the left subtree, then the values in the right subtree. The preorder traversal of the tree in Fig. 23.19 is 27 13 6 17 42 33 48 Method PostorderHelper (lines 183 198) defines the steps for a postorder traversal. Those steps are as follows: 1. If the argument is null, return immediately. 2. Traverse the left subtree with a call to PostorderHelper (line 189). 3. Traverse the right subtree with a call to PostorderHelper (line 192). 4. Process the value in the node (line 195). The postorder traversal processes the value in each node after the values of all that node s children are processed. The postorder traversal of the tree in Fig. 23.19 is 6 17 13 33 48 42 27 The binary search tree facilitates duplicate elimination. While building a tree, the insertion operation recognizes attempts to insert a duplicate value, because a duplicate follows the same go left or go right decisions on each comparison as the original value did. Thus, the insertion operation eventually compares the duplicate with a node containing the same value. At this point, the insertion operation might simply discard the duplicate value. Searching a binary tree for a value that matches a key value is fast, especially for tightly packed trees. In a tightly packed tree, each level contains about twice as many elements as the previous level. Figure 23.19 is a tightly packed binary tree. A binary search tree with n elements has a minimum of log2n levels. Thus, at most log2n comparisons are required either to find a match or to determine that no match exists. Searching a (tightly packed) 1000-element binary search tree requires at most 10 comparisons, because 210 > 1000. Searching a (tightly packed) 1,000,000-element binary search tree requires at most 20 comparisons, because 220 > 1,000,000. The chapter exercises present algorithms for other binary tree operations, such as performing a level-order traversal of a binary tree. The level-order traversal of a binary tree visits the nodes of the tree row by row, starting at the root-node level. On each level of the tree, a level-order traversal visits the nodes from left to right. 23.6.2 Binary Search Tree of IComparable Objects The binary tree example in Section 23.6.1 works nicely when all the data is of type int. Suppose that you want to manipulate a binary tree of double values. You could rewrite the TreeNode and Tree classes with different names and customize the classes to manipulate double values. Similarly, for each data type you could create customized versions of classes TreeNode and Tree. This results in a proliferation of code, which can become difficult to manage and maintain. The C++ programming language provides a technology called templates that enables us to write a class definition once, then have the compiler generate new versions of the class for any data type we choose. Ideally, we would like to define the functionality of a binary tree once and reuse that functionality for many data types. Languages like Java and C# provide polymorphic
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision tomcat hosting services

1176 Data Structures and Collections Chapter 23 left (Multiple domain web hosting)

Sunday, May 6th, 2007

1176 Data Structures and Collections Chapter 23 left subtree. If the insert value is greater than the root-node data, the program determines whether the right subtree is empty (line 85). If so, line 86 allocates a new TreeNode, initializes it with the integer being inserted and assigns the new node to reference right- Node. Otherwise, line 90 recursively calls Insertfor the right subtree to insert the value in the right subtree. Methods InorderTraversal, PreorderTraversal and PostorderTraversal call helper methods InorderHelper (lines 158 171), PreorderHelper (lines 133 146) and PostorderHelper (lines 183 196), respectively, to traverse the tree and print the node values. The purpose of the helper methods in class Treeis to allow the programmer to start a traversal without the need to obtain a reference to the rootnode first, then call the recursive method with that reference. Methods InorderTraversal, PreorderTraversaland PostorderTraversalsimply take the privatereference root and pass it to the appropriate helper method to initiate a traversal of the tree. For the following discussion, we use the binary search tree shown in Fig. 23.19. Method InorderHelper(lines 158 171) defines the steps for an inorder traversal. Those steps are as follows: 1. If the argument is null, return immediately. 2. Traverse the left subtree with a call to InorderHelper(line 164). 3. Process the value in the node (line 167). 4. Traverse the right subtree with a call to InorderHelper(line 170). The inorder traversal does not process the value in a node until the values in that node s left subtree are processed. The inorder traversal of the tree in Fig. 23.19 is 6 13 17 27 33 42 48 Note that the inorder traversal of a binary search tree prints the node values in ascending order. The process of creating a binary search tree actually sorts the data thus, this process is called the binary tree sort. Method PreorderHelper (lines 133 146) defines the steps for a preorder traversal. Those steps are as follows: 1. If the argument is null, return immediately. 2. Process the value in the node (line 139). 3. Traverse the left subtree with a call to PreorderHelper(line 142). 4. Traverse the right subtree with a call to PreorderHelper(line 145). 27 13 42 6 17 33 48 Fig. 23.19 Fig. 23.19Fig. 23.FF19ig. 23.19ig. 23.19A binary search tree.
Note: If you are looking for cheap webhost to host and run your apache application check Vision jboss web hosting services

Chapter 23 Data Structures and Collections (Web hosting domains) 1175 32

Sunday, May 6th, 2007

Chapter 23 Data Structures and Collections 1175 32 tree.PreorderTraversal(); 33 34 // perform inorder traversal of tree 35 Console.WriteLine( “nnInorder traversal” ); 36 tree.InorderTraversal(); 37 38 // perform postorder traversal of tree 39 Console.WriteLine( “nnPostorder traversal” ); 40 tree.PostorderTraversal(); 41 Console.WriteLine(); 42 } 43 44 } // end class TreeTest 45 } Inserting values: 39 69 94 47 50 72 55 41 97 73 Preorder traversal 39 69 47 41 50 55 94 72 73 97 Inorder traversal 39 41 47 50 55 69 72 73 94 97 Postorder traversal 41 55 50 47 73 72 97 94 69 39 Fig. 23.18 Fig. 23.1FiFig. 23.18g. 23.18 Fig. 23.18 Creating and traversing a binary tree. (Part 2 of 2.) Class Tree (lines 98 198 of Fig. 23.17) manipulates objects of class TreeNode. Class Tree has as private data root (line 100) a reference to the root node of the tree. The class contains public method InsertNode (lines 111 121) to insert a new node in the tree and publicmethods PreorderTraversal (lines 124 130), InorderTraversal (lines 149 155) and PostorderTraversal (lines 174 180) to begin traversals of the tree. Each of these methods calls a separate recursive utility method to perform the traversal operations on the internal representation of the tree. The Treeconstructor (lines 103 106) initializes rootto nullto indicate that the tree initially is empty. The Tree class s method InsertNode (lines 67 74) first locks the Tree object for thread safety, then determines whether the tree is empty. If so, line 116 allocates a new TreeNode, initializes the node with the integer being inserted in the tree and assigns the new node to root. If the tree is not empty, InsertNodecalls TreeNode method Insert (lines 67 93), which recursively determines the location for the new node in the tree and inserts the node at that location. A node can be inserted only as a leaf node in a binary search tree. The TreeNode method Insert compares the value to insert with the data value in the root node. If the insert value is less than the root-node data, the program determines whether the left subtree is empty (line 73). If so, line 74 allocates a new TreeNode, initializes it with the integer being inserted and assigns the new node to reference leftNode. Otherwise, line 78 recursively calls Insertfor the left subtree to insert the value into the
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision virtual web hosting services

1174 Data Structures and Collections Chapter 23 182 (Web site domain)

Sunday, May 6th, 2007

1174 Data Structures and Collections Chapter 23 182 // recursive method to perform postorder traversal 183 private void PostorderHelper( TreeNode node ) 184 { 185 if( node == null ) 186 return; 187 188 // traverse left subtree 189 PostorderHelper( node.LeftNode ); 190 191 // traverse right subtree 192 PostorderHelper( node.RightNode ); 193 194 // output node data 195 Console.Write( node.Data + ” ” ); 196 } 197 198 } // end class Tree 199 } Fig. 23.17 Fig. 23.1FiFig. 23.17g. 23.17 Fig. 23.17 Definitions of TreeNodeand Treefor a binary search tree. (Part 5 of 5.) 1 // Fig. 23.18: TreeTest.cs 2 // This program tests class Tree. 3 4 using System; 5 using BinaryTreeLibrary; 6 7 namespace TreeTest 8 { 9 // class TreeTest definition 10 public class TreeTest 11 { 12 // test class Tree 13 static void Main( string[] args ) 14 { 15 Tree tree = new Tree(); 16 int insertValue; 17 18 Console.WriteLine( “Inserting values: ” ); 19 Random random = new Random(); 20 21 // insert 10 random integers from 0-99 in tree 22 for ( int i = 1; i <= 10; i++ ) 23 { 24 insertValue = random.Next( 100 ); 25 Console.Write( insertValue + " " ); 26 27 tree.InsertNode( insertValue ); 28 } 29 30 // perform preorder traversal of tree 31 Console.WriteLine( "nnPreorder traversal" ); Fig. 23.18 Fig. 23.1FiFig. 23.18g. 23.18 Fig. 23.18 Creating and traversing a binary tree. (Part 1 of 2.)
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision web and email hosting services

Http web server - Chapter 23 Data Structures and Collections 1173 132

Saturday, May 5th, 2007

Chapter 23 Data Structures and Collections 1173 132 // recursive method to perform preorder traversal 133 private void PreorderHelper( TreeNode node ) 134 { 135 if ( node == null ) 136 return; 137 138 // output node data 139 Console.Write( node.Data + ” ” ); 140 141 // traverse left subtree 142 PreorderHelper( node.LeftNode ); 143 144 // traverse right subtree 145 PreorderHelper( node.RightNode ); 146 } 147 148 // begin inorder traversal 149 public void InorderTraversal() 150 { 151 lock ( this ) 152 { 153 InorderHelper( root ); 154 } 155 } 156 157 // recursive method to perform inorder traversal 158 private void InorderHelper( TreeNode node ) 159 { 160 if( node == null ) 161 return; 162 163 // traverse left subtree 164 InorderHelper( node.LeftNode ); 165 166 // output node data 167 Console.Write( node.Data + ” ” ); 168 169 // traverse right subtree 170 InorderHelper( node.RightNode ); 171 } 172 173 // begin postorder traversal 174 public void PostorderTraversal() 175 { 176 lock ( this ) 177 { 178 PostorderHelper( root ); 179 } 180 } 181 Fig. 23.17 Fig. 23.1FiFig. 23.17g. 23.17 Fig. 23.17 Definitions of TreeNodeand Treefor a binary search tree. (Part 4 of 5.)
Note: If you are looking for high quality webhost to host and run your jsp application check Vision christian web host services

Free web hosts - 1172 Data Structures and Collections Chapter 23 81

Saturday, May 5th, 2007

1172 Data Structures and Collections Chapter 23 81 // insert in right subtree 82 else if ( insertValue > data ) 83 { 84 // insert new TreeNode 85 if ( rightNode == null ) 86 rightNode = new TreeNode( insertValue ); 87 88 // continue traversing right subtree 89 else 90 rightNode.Insert( insertValue ); 91 } 92 93 } // end method Insert 94 95 } // end class TreeNode 96 97 // class Tree definition 98 public class Tree 99 { 100 private TreeNode root; 101 102 // construct an empty Tree of integers 103 public Tree() 104 { 105 root = null; 106 } 107 108 // Insert a new node in the binary search tree. 109 // If the root node is null, create the root node here. 110 // Otherwise, call the insert method of class TreeNode. 111 public void InsertNode( int insertValue ) 112 { 113 lock ( this ) 114 { 115 if ( root == null ) 116 root = new TreeNode( insertValue ); 117 118 else 119 root.Insert( insertValue ); 120 } 121 } 122 123 // begin preorder traversal 124 public void PreorderTraversal() 125 { 126 lock ( this ) 127 { 128 PreorderHelper( root ); 129 } 130 } 131 Fig. 23.17 Fig. 23.1FiFig. 23.17g. 23.17 Fig. 23.17 Definitions of TreeNodeand Treefor a binary search tree. (Part 3 of 5.)
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision personal web hosting services

Chapter 23 Data Structures and Collections 1171 30

Friday, May 4th, 2007

Chapter 23 Data Structures and Collections 1171 30 set 31 { 32 leftNode = value; 33 } 34 } 35 36 // Data property 37 public int Data 38 { 39 get 40 { 41 return data; 42 } 43 44 set 45 { 46 data = value; 47 } 48 } 49 50 // RightNode property 51 public TreeNode RightNode 52 { 53 get 54 { 55 return rightNode; 56 } 57 58 set 59 { 60 rightNode = value; 61 } 62 } 63 64 65 // insert TreeNode into Tree that contains nodes; 66 // ignore duplicate values 67 public void Insert( int insertValue ) 68 { 69 // insert in left subtree 70 if ( insertValue < data ) 71 { 72 // insert new TreeNode 73 if ( leftNode == null ) 74 leftNode = new TreeNode( insertValue ); 75 76 // continue traversing left subtree 77 else 78 leftNode.Insert( insertValue ); 79 } 80 Fig. 23.17 Fig. 23.1FiFig. 23.17g. 23.17 Fig. 23.17 Definitions of TreeNodeand Treefor a binary search tree. (Part 2 of 5.)
Note: In case you are looking for affordable and reliable webhost to host and run your business application check Vision php5 hosting services