Danh mục

Creating a Generic Class

Số trang: 12      Loại file: pdf      Dung lượng: 28.46 KB      Lượt xem: 1      Lượt tải: 0    
Thư viện của tui

Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Tạo một Generic Class Các NET Framework. Class Thư viện chứa một số lớp học chung loại có sẵn cho bạn. Bạn cũng có thể định nghĩa các lớp học của riêng chung của bạn, mà là những gì bạn sẽ làm gì trong phần này.
Nội dung trích xuất từ tài liệu:
Creating a Generic Class Creating a Generic ClassThe .NET Framework Class Library contains a number of generic classes readilyavailable for you. You can also define your own generic classes, which is what you willdo in this section. Before we do this, I will provide a bit of background theory.The Theory of Binary TreesIn the following exercises, you will define and use a class that represents a binary tree.This is a very practical exercise because this class happens to be one that is missing fromthe System.Collections.Generic namespace. A binary tree is a very useful data structureused for a variety of operations, including sorting and searching through data veryquickly. There are volumes written on the minutiae of binary trees, but it is not thepurpose of this book to cover binary trees in detail. Instead, we will just look at thepertinent details. If you are interested you should consult a book such as The Art ofComputer Programming, Volume 3: Sorting and Searching by Donald E. Knuth(Addison-Wesley Professional; 2nd edition 1998).A binary tree is a recursive (self-referencing) data structure that can either be empty orcomprise three elements: some data that is typically referred to as the node and two sub-trees, which are themselves binary trees. The two sub-trees are conventionally called theleft sub-tree and the right sub-tree because they are typically depicted to the left and rightof the node respectively. Each left sub-tree or right sub-tree is either empty, or contains anode and other sub-trees. In theory, the whole structure can continue ad infinitum. Figure17-1 shows the structure of a small binary tree.The real power of binary trees becomes evident when you use them for sorting data. Ifyou start with an unordered sequence of objects of the same type, you can use them toconstruct an ordered binary tree and then walk through the tree to visit each node in anordered sequence. The algorithm for inserting an item into an ordered binary tree isshown below:If the tree, T, is emptyThen Construct a new tree T with the new item I as the node, and empty left and right sub-treesElse Examine the value of the node, N, of the tree, T If the value of N is greater than that of the new item, I Then If the left sub-tree of T is empty Then Construct a new left sub-tree of T with the item I as the node, and empty left and right sub-trees Else Insert I into the left sub-tree of T End If Else If the right sub-tree of T is empty Then Construct a new right sub-tree of T with the item I as the node, and empty left and right sub-trees Else Insert I into the right sub-tree of T End If End IfEnd IfFigure 17-1 A binary tree.Notice that this algorithm is recursive, calling itself to insert the item into the left or rightsub-tree depending on the value of the item and the current node in the tree.NOTEThe definition of the expression greater than depends on the type of data in the item andnode. For numeric data, greater than can be a simple arithmetic comparison, for text datait can be a string comparison, but other forms of data must be given their own means ofcomparing values. This is discussed in more detail when we implement a binary tree inthe section “Building a Binary Tree Class Using Generics” later in this chapter.If you start with an empty binary tree and an unordered sequence of objects, you caniterate through the unordered sequence inserting each one into the binary tree by usingthis algorithm, resulting in an ordered tree. Figure 17-2 shows the steps in the process forconstructing a tree from a set of 5 integers.Figure 17-2 Constructing an ordered binary tree.Once you have built an ordered binary tree, you can display its contents in sequence byvisiting each node in turn and printing the value found. The algorithm for achieving thistask is also recursive:If the left sub-tree is not emptyThen Display the contents of the left sub-treeEnd IfDisplay the value of the nodeIf the right sub-tree is not emptyThen Display the contents of the right sub-treeEnd IfFigure 17-3 shows the steps in the process for outputting the tree constructed earlier.Notice that the integers are now displayed in ascending order.Figure 17-3 Printing an ordered binary tree.Building a Binary Tree Class Using GenericsIn the following exercise, you will use generics to define a binary tree class capable ofholding almost any type of data. The only restriction is that the data type must provide ameans of comparing values between different instances.The binary tree class is a class that you might find useful in many different applications.Therefore, you will implement it as a class library rather than an application in its ownright. You can then reuse this class elsewhere without needing to copy the source codeand recompile it. A class library is a set of compiled classes (and other types such asstructs and delegates) stored in an assembly. An assembly is a file that usually has the“.dll” suffix. Other projects and applications can make use of the items in a class libraryby adding a reference to its assembly, and then bringing its namespaces into scope withusing statements. You will do this when you test the binary tree class.The System.IComparable and System.IComparable InterfacesIf you need to create a class that requires you to be able to compare values according tosome natural (or possibly unnatural) ordering, you should implement the IComparableinterface. This interface contains a method called CompareTo, which takes a singleparameter specifying the object to be compared to the current instance and returns aninteger that indicates the result of the comparison as shown in the following table.Value MeaningLess than zero The current instance is less than the value of the parameterZero The current instance is equal to the val ...

Tài liệu được xem nhiều: