1178 Data Structures and Collections Chapter 23 capabilities
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