12.07.2015 Views

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

PrefacexviiMy implementations are meant <strong>to</strong> provide concrete illustrations of data structureprinciples, as an aid <strong>to</strong> the textual exposition. Code examples should not beread or used in isolation from the associated text because the bulk of each example’sdocumentation is contained in the text, not the code. The code complementsthe text, not the other way around. They are not meant <strong>to</strong> be a series of commercialqualityclass implementations. If you are looking for a complete implementationof a st<strong>and</strong>ard data structure for use in your own code, you would do well <strong>to</strong> do anInternet search.For instance, the code examples provide less parameter checking than is soundprogramming practice, since including such checking would obscure rather than illuminatethe text. Some parameter checking <strong>and</strong> testing for other constraints (e.g.,whether a value is being removed from an empty container) is included in the formof calls <strong>to</strong> methods in class Assert. These methods are modeled after the C st<strong>and</strong>ardlibrary function assert. Method Assert.notFalse takes a Booleanexpression. If this expression evaluates <strong>to</strong> false, then a message is printed <strong>and</strong>the program terminates immediately. Method Assert.notNull takes a reference<strong>to</strong> class Object, <strong>and</strong> terminates the program if the value of the referenceis null. (To be precise, these functions throw an IllegalArgument-Exception, which typically results in terminating the program unless the programmertakes action <strong>to</strong> h<strong>and</strong>le the exception.)Terminating a program when a function receives a bad parameter is generallyconsidered undesirable in real programs, but is quite adequate for underst<strong>and</strong>inghow a data structure is meant <strong>to</strong> operate. In real programming applications, Java’sexception h<strong>and</strong>ling features should be used <strong>to</strong> deal with input data errors. However,assertions provide a simpler mechanism for indicating required conditions in a waythat is both adequate for clarifying how a data structure is meant <strong>to</strong> operate, <strong>and</strong> iseasily modified in<strong>to</strong> true exception h<strong>and</strong>ling.I make a distinction in the text between “Java implementations” <strong>and</strong> “pseudocode.”Code labeled as a Java implementation has actually been compiled <strong>and</strong>tested on one or more Java compilers. Pseudocode examples often conform closely<strong>to</strong> Java syntax, but typically contain one or more lines of higher-level description.Pseudocode is used where I perceived a greater pedagogical advantage <strong>to</strong> a simpler,but less precise, description.Exercises <strong>and</strong> Projects: Proper implementation <strong>and</strong> analysis of data structurescannot be learned simply by reading a book. You must practice by implementingreal programs, constantly comparing different techniques <strong>to</strong> see what really worksbest in a given situation.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!