Table Of ContentP1:KSF/ICD
0521547652pre CB820-McMillan-v1 April21,2005 16:14
This page intentionally left blank
ii
P1:KSF/ICD
0521547652pre CB820-McMillan-v1 April21,2005 16:14
D S
ATA TRUCTURES AND
A U
LGORITHMS SING
V B .NET
ISUAL ASIC
ThisisthefirstVisualBasic.NET(VB.NET)booktoprovideacomprehensive
discussionofthemajordatastructuresandalgorithms.Here,insteadofhaving
to translate material on C++ or Java, the professional or student VB.NET
programmerwillfindatutorialonhowtousedatastructuresandalgorithms
and a reference for implementation using VB.NET for data structures and
algorithms from the .NET Framework Class Library as well as those that
mustbedevelopedbytheprogrammer.
In an object-oriented fashion, the author presents arrays and ArrayLists,
linkedlists,hashtables,dictionaries,trees,graphs,andsortingandsearching
as well as more advanced algorithms, such as probabilistic algorithms and
dynamic programming. His approach is very practical, for example using
timing tests rather than Big O analysis to compare the performance of data
structuresandalgorithms.
This book can be used in both beginning and advanced computer pro-
gramming courses that use the VB.NET language and, most importantly, by
theprofessionalVisualBasicprogrammer.
MichaelMcMillanisInstructorofComputerInformationSystemsatPulaski
TechnicalCollege.Withmorethantwentyyearsofexperienceinthecomputer
industry,hehaswrittennumerousarticlesfortradejournalssuchasSoftware
DevelopmentandWindowsNTSystems.HeistheauthorofPerlfromtheGround
Up and Object-Oriented Programming with Visual Basic.Net and coauthor of
severalbooks.
i
P1:KSF/ICD
0521547652pre CB820-McMillan-v1 April21,2005 16:14
ii
P1:KSF/ICD
0521547652pre CB820-McMillan-v1 April21,2005 16:14
D S
ATA TRUCTURES AND
A U
LGORITHMS SING
V B .NET
ISUAL ASIC
M M M
ICHAEL C ILLAN
PulaskiTechnicalCollege
iii
P1:KSF/ICD
0521547652pre CB820-McMillan-v1 April21,2005 16:14
cambridge university press
Cambridge, New York, Melbourne, Madrid, Cape Town, Singapore, São Paulo
Cambridge University Press
The Edinburgh Building, Cambridge cb2 2ru, UK
Published in the United States of America by Cambridge University Press, New York
www.cambridge.org
Information on this title: www.cambridge.org/9780521547659
© Michael McMillan 2005
This book is in copyright. Subject to statutory exception and to the provision of
relevant collective licensing agreements, no reproduction of any part may take place
without the written permission of Cambridge University Press.
First published in print format 2005
isbn-13 978-0-511-11197-6 eBook (Adobe Reader)
isbn-10 0-511-11197-5 eBook (Adobe Reader)
isbn-13 978-0-521-54765-9 paperback
isbn-10 0-521-54765-2 paperback
Cambridge University Press has no responsibility for the persistence or accuracy of
urls for external or third-party internet websites referred to in this book, and does not
guarantee that any content on such websites is, or will remain, accurate or appropriate.
iv
P1:KSF/ICD
0521547652pre CB820-McMillan-v1 April21,2005 16:14
Contents
Preface vii
Introduction 1
Chapter 1
Collections 14
Chapter 2
Arrays and ArrayLists 46
Chapter 3
Basic Sorting Algorithms 72
Chapter 4
Basic Searching Algorithms 86
Chapter 5
Stacks and Queues 99
Chapter 6
The BitArray Class 124
Chapter 7
Strings, the String Class, and the StringBuilder Class 150
v
P1:KSF/ICD
0521547652pre CB820-McMillan-v1 April21,2005 16:14
vi CONTENTS
Chapter 8
Pattern Matching and Text Processing 181
Chapter 9
Building Dictionaries: The DictionaryBase Class and the
SortedList Class 200
Chapter 10
Hashing and the HashTable Class 210
Chapter 11
Linked Lists 227
Chapter 12
Binary Trees and Binary Search Trees 249
Chapter 13
Sets 268
Chapter 14
Advanced Sorting Algorithms 283
Chapter 15
Advanced Data Structures and Algorithms for Searching 298
Chapter 16
Graphs and Graph Algorithms 320
Chapter 17
Advanced Algorithms 352
References 379
Index 381
P1:KSF/ICD
0521547652pre CB820-McMillan-v1 April21,2005 16:14
Preface
TheVisualBasic.NET(VB.NET)programminglanguageisnotusuallyassoci-
atedwiththestudyofdatastructuresandalgorithms.Theprimaryreasonfor
this must be because most university and college computer science depart-
ments don’t consider VB.NET to be a “serious” programming language that
can be used to study serious topics. This is primarily a historical bias based
onBasic’spastasa“nonprogrammer’s”languageoftentaughttojuniorhigh,
seniorhigh,andliberalartscollegestudents,butnottocomputerscienceor
computerengineeringmajors.
Thepresentstateofthelanguage,however,alignsitwithother,moreserious
programminglanguages,mostspecificallyJava.VB.NET,initscurrentform,
containseverythingexpectedinamodernprogramminglanguage,fromtrue
object-orientedfeaturestothe.NETFrameworklibrary,whichrivalstheJava
librariesinbothdepthandbreadth.
Includedinthe.NETFrameworklibraryisasetofcollectionclasses,which
rangefromtheArray,ArrayList,andCollectionclasses,totheStackandQueue
classes,totheHashtableandtheSortedListclasses.Studentsofdatastructures
andalgorithmscannowseehowtouseadatastructurebeforelearninghow
toimplementit.Previously,aninstructorhadtodiscusstheconceptof,say,a
stack,abstractlyuntilthecompletedatastructurewasconstructed.Instructors
can now show students how to use a stack to perform some computations,
suchasnumberbaseconversions,demonstratingtheutilityofthedatastruc-
tureimmediately.Withthisbackground,studentscanthengobackandlearn
the fundamentals of the data structure (or algorithm) and even build their
ownimplementation.
This book is written primarily as a practical overview of the data struc-
tures and algorithms all serious computer programmers need to know and
vii
P1:KSF/ICD
0521547652pre CB820-McMillan-v1 April21,2005 16:14
viii PREFACE
understand.Giventhis,thereisnoformalanalysisofthedatastructuresand
algorithms covered in the book. Hence, there is not a single mathematical
formula and not one mention of Big O analysis (for the latter the reader is
referred to any of the books listed in the bibliography). Instead, the various
data structures and algorithms are presented as problem-solving tools. We
usesimpletimingteststocomparetheperformanceofthedatastructuresand
algorithmsdiscussedinthebook.
PREREQUISITES
There are very few prerequisites for this book. The reader should be com-
petent in one or more programming languages, preferably VB.NET, though
a course or two in Java will serve as well. C/C++ programmers should not
have too much trouble picking up the language. There are no mathematical
prerequisitessincewedon’ttakeaformalapproachinthebook.
CHAPTER-BY-CHAPTER ORGANIZATION
TheIntroductionprovidesanoverviewofobject-orientedprogrammingusing
VB.NET and introduces the benchmark tool used for comparing the perfor-
manceofthedatastructuresandalgorithmsstudiedinthebook.Thistoolis
a Timing class developed by the author as a practical means for timing code
inthe.NETenvironment.
Chapter 1 introduces the reader to the concept of the data structure as
a collection of data. The concepts of linear and nonlinear collections are
introduced.TheCollectionclassisdemonstrated.
Chapter 2 provides a review of how arrays are constructed in VB.NET,
along with demonstrating the features of the Array class. The Array class
encapsulatesmanyofthefunctionsassociatedwitharrays(UBound,LBound,
and so on) into a single package. Arraylists are special types of arrays that
providedynamicresizingcapabilities.
Chapter3givesanintroductiontothebasicsortingalgorithms,suchasthe
bubble sort and the insertion sort, and Chapter 4 examines the most funda-
mentalalgorithmsforsearchingmemory,thesequentialandbinarysearches.
Two classic data structures are examined in Chapter 5—the stack and the
queue. This chapter emphasizes the practical use of these data structures in
solvingeverydayproblemsindataprocessing.Chapter6coverstheBitArray