Beginning Algorithms - Code Examples

This file contains information on and instructions for building1 the source code accompanying the book Beginning Algorithms, Simon Harris & James Ross, Wrox Press, 2005.

Understanding the Directory Layout

The source files are divided into two directories -- one for production classes (main) and one for test classes (test) -- arranged into packages by chapter as follows:

Packages
com.wrox.algorithms.iterationChapter 2 - Iteration and Recursion
com.wrox.algorithms.listsChapter 3 - Lists
com.wrox.algorithms.queuesChapters 4 & 8 - Queues & Priority Queues
com.wrox.algorithms.stacksChapter 5 - Stacks
com.wrox.algorithms.sortingChapters 6 & 8 - Basic & Advanced Sorting
com.wrox.algorithms.bsearchChapter 9 - Binary Searching
com.wrox.algorithms.bstreesChapter 10 - Binary Search Trees
com.wrox.algorithms.hashingChapter 11 - Hashing
com.wrox.algorithms.setsChapter 12 - Sets
com.wrox.algorithms.mapsChapter 13 - Maps
com.wrox.algorithms.tstreesChapter 14 - Ternary Search Trees
com.wrox.algorithms.btreesChapter 15 - B-Trees
com.wrox.algorithms.ssearchChapter 16 - String Searching
com.wrox.algorithms.wmatchChapter 17 - Word Matching
com.wrox.algorithms.geometryChapter 18 - Computational Geometry

Compiling the Classes

The production classes are self-contained -- they don't have any dependencies on external class libraries etc. So, to compile the production classes for Chapter 3 -- Lists -- enter the following command:

  javac -sourcepath main main/com/wrox/algorithms/lists/*.java

This invokes the compiler telling it to compile all production files in the com.wrox.algorithms.lists package.

Notice the the -sourcepath main option. This tells the compiler where to look for any other files it may need. For example, in this case, the code for Chapter 3 depends on the code for Chapter 2 -- Iteration and Recursion -- so the compiler needs to know where to look to find the dependent source files.

Compiling the test classes is similar except for one thing: they depend on JUnit. So, in order to compile the test classes, you will first need to download a copy of JUnit from here and extract the junit.jar file. Once this is done, you can compile the test classes for Chapter 3 by entering the following command:

  javac -classpath junit.jar -sourcepath main test/com/wrox/algorithms/lists/*.java

This invokes the compiler telling it to compile all test files in the com.wrox.algorithms.lists package2.

Once again, notice the extra -sourcepath main option telling the compiler where to look for any other files it may need.

Running the Tests

Once you have the java classes compiled, you can try running some of the test cases using one of the JUnit test runners. For example, to run the ArrayListTest from Chapter 3 using the text-based runner, enter the following command:

  java -classpath junit.jar:main:test junit.textui.TestRunner com.wrox.algorithms.lists.ArrayListTest

You should then see some output similar to the following:

  .........................
  Time: 0.021

  OK (25 tests)

This indicates that a total of 25 tests were executed in a time of 0.021 seconds and that all tests passed (OK).

For a report that uses a graphical user interface, try the following instead:

  java -classpath junit.jar:main:test junit.swingui.TestRunner com.wrox.algorithms.lists.ArrayListTest

Notice the use of swingui instead of textui.


Notes

1Assuming you are building from the command-line. If you are using an IDE, please refer to the appropriate documentation supplied with the tool.

3Interestingly, compiling the test classess in this way also compiles the production classes as well, enabling you to quickly compile both in one go.