Introduction to Data Structures
We’ve covered the fundamental parts of Java programs: classes, abstract classes, and interfaces. Classes capture new concrete kinds of data. Abstract classes share fields, methods, and helper functions that are common to classes. Interfaces define types based on required methods, but omit the details of fields and how the methods are implemented.
Today we shift a gears a bit, beginning to consider one of the other course themes: data structures. We’ll take a shrt break from new Java content in favor of some new computer science content.
1 Three Motivating Problems
Consider the following problems:
Store all of the URLs visited from a web browser, with the ability to check whether a specific URL has been visited.
Gather all the words that someone generated during a word game (such as "write down all words can you make from the letters d e h l l l o o r w"), with the ability to add words, find out how many words the player generated, and easily access the longest words for purposes of scoring.
Maintain information on who is coming to a dinner party. In addition to adding and removing names, you need to be able to compare the current number of attendees to the number of chairs in your apartment.
Each of these problems involves working with a bunch of strings that cannot have duplicates. This suggests that all three problems could use a similar data definition or set of classes to capture their bunch of strings. But if we look more closely, each problem is more concerned with certain operations on the bunch of strings:
The web browser problem needs to check membership (whether a particular string is in the bunch).
The word game needs to access the strings in order, in this case based on the length of the string.
The dinner party RSVP needs to check the size of the bunch and make it easy to remove guests who initially expect to come then have a change of plans.
There are many ways to organize a bunch of strings as data (whether as Java classes, Racket structs, or constructs in other languages). Different organizations make different operations easier or harder to perform. Today, we begin to look at several different data structures that you can use to capture a bunch of strings and the tradeoffs between them in terms of which operations they best support.
2 Sets and Their Operations
Getting more precise, we will be studying data structures for sets, collections of elements without duplicates. We’ll return to the question of duplicates a bit later.
There are many standard operations on sets. Here, we will be concerned with supporting the following operations:
addElt: Adds an item to the set
remElt: Removes an item from the set
size: Report how many items are in the set
hasElt: Check whether a specific item is in the set
We’ll start by studying several specific data structures for sets, then come back to some issues that arise when we implement them in Java.