按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!
least let’s try it and see what happens。
As per the attributes illustrated in Figure 4…7; the data structure that is added to the
SearchSolution project is implemented as shown in Figure 4…9。
Name of the city
Public Structure Node
Public CityName As String
Public X As Double X and Y coordinates
Public Y As Double
of the city
Public Connections As Node()
End Structure
Array of cities that are
reachable from the
current city
Figure 4…9。 The data structure for the depth…first search
The data structure is declared as a Structure; with the connections represented as an array
of Node elements。 An array of Node elements is formed when one Node contains a list of refer
ences to other Node elements。 Think of an array as a collection of sticky notes that say; “Here is
a reference to A; B; C; and so on。” By having one node reference another node; a sort of never
ending tree is created; because it is possible to travel back and forth between two cities。 Thus;
the depth…first search algorithm will need to avoid repeating itself。
The Connections data member is an array used to define cities that are the next connection。
To reference another city; you can create the reference as an array of Node elements; as in the
declaration shown in Figure 4…9。 An alternative is to use an array of strings that contain the
name of the next city; like this:
…………………………………………………………Page 112……………………………………………………………
90 CH AP T E R 4 ■ L E A R N IN G AB OU T D AT A S TR U CT U R E S; DE CI SI ON S; A N D L O OP S
Public Structure Node
Public CityName As String
Public X As Double
Public Y As Double
Public Connections As String()
End Structure
In this declaration; Connections is an array of strings that references other city names and
is what humans would see when they look at a table showing all of the connections for a partic
ular city。 The problem with using strings is that it is inefficient from a puting perspective。
To traverse a tree of cities; you would first traverse the city names; resolve the city name to a
Node object; and then traverse the node。 The string array approach requires an extra; unneces
sary step。 So the more efficient and programmatic approach is to have an array of Node instances。
By using the declaration where Connections is an array of Node instances; you have both
the city name and available connections in one cohesive object。
Instantiating and Initializing a Node
In previous code; you have seen how objects can be instantiated using the New keyword。 To
instantiate a type; you always use the New keyword。 After the New keyword is the type that you
want to instantiate; followed by a set of parentheses。 A node is instantiated using the following
code:
Dim city As Node = New Node()
If you look only at the identifier Node with parentheses; you would get the impression that
you are calling a method that has no parameters。 The impression is correct; but it is a special
type of method call; and that is made apparent by the use of the New keyword。 The method that
is being called is known as a constructor。 Every type has a constructor; and it can be used to
initialize the state of the object before being returned to the caller。
In the declaration of Node; there is no defined constructor; and thus a default constructor
is provided by the CLR。 The default constructor does nothing and has no parameters。
After having instantiated a node; we can assign the data members; as in the following
code。
city。CityName = 〃Montreal〃
city。X = 0。0
city。Y = 0。0
Assigning the data members results in setting the city name to Montreal and the coordi
nates to (0;0)。
This is all fine; but shouldn’t we need to provide some data members when creating a city
node? Does it make sense to instantiate a node without defining the name and coordinates of
the city? Technically; a node does not need to be assigned; but logically speaking; an unassigned
node is quite useless。 And remember that we are working on defining an intelligent data struc
ture; thus a Node instance without city name and coordinates is logically not a valid Node。 We
also need to have a root for the Node structure; which we will set up as an array。
You can enforce a verifiable correct initial state by defining a constructor with parameters;
rather than using the default constructor; as in the following example。 When your code provides
…………………………………………………………Page 113……………………………………………………………
CH AP T E R 4 ■ L E A R N I N G A B OU T D AT A S TR U CT U R E S; DE CI SI ON S; A N D L O OP S 91
a constructor; regardless of the declaration; the default constructor is not generated and is not
accessible。
Public Structure Node
Public Shared RootNodes As Node()
Public CityName As String
Public X As Double
Public Y As Double
Public Connections As Node()
Public Sub New(ByVal city As String; ByVal X As Double; ByVal Y As Double)
Me。CityName = city
Me。X = x
Me。Y = y
Me。Connections = Nothing
End Sub
End Structure
■Note Nothing is a special value that you can