This preview shows pages 1–3. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 9/1/10 1 ADT Efficiency Subtypes EECS 280 Programming and Introductory Data Structures 1 Abstract Data Types Recall – Using Classes Recall our abstraction that held a mutable set of integers. This is a “set” in the mathematical sense: A collection of zero or more integers, with no duplicates. The set is “mutable” because we can insert values into, and remove objects from, the set. We implemented this using C++'s "class" mechanism. 2 Abstract Data Types Recall – Using Classes const int MAXELTS = 100; class IntSet { // OVERVIEW: a mutable set of integers, // where |set| <= 100 int elts[MAXELTS]; int numElts; int indexOf(int v); // EFFECTS: returns the index of v // if it exists in the array, // MAXELTS otherwise. public: // methods on next slide }; 3 Abstract Data Types Recall – Using Classes public: IntSet(); // default constructor // EFFECTS: creates an empty IntSet void insert(int v); // MODIFIES: this // EFFECTS: this = this + {v} if room, // throws int numElts otherwise void remove(int v); // MODIFIES: this // EFFECTS: this = this - {v} bool query(int v); // EFFECTS: returns true if v is in this, // false otherwise int size(); // EFFECTS: returns |this|. 4 Abstract Data Types Recall – Using Classes int IntSet::indexOf(int v) { for (int i = 0; i < numElts; i++) { if (elts[i] == v) return i; } return MAXELTS; } bool IntSet::query(int v) { return (indexOf(v) != MAXELTS); } 5 Abstract Data Types Recall – Using Classes int IntSet::indexOf(int v) { for (int i = 0; i < numElts; i++) { if (elts[i] == v) return i; } return MAXELTS; } void IntSet::insert(int v) { if (indexOf(v) == MAXELTS) { if (numElts == MAXELTS) throw MAXELTS; elts[numElts++] = v; } } 6 9/1/10 2 Abstract Data Types Recall – Using Classes int IntSet::indexOf(int v) { for (int i = 0; i < numElts; i++) { if (elts[i] == v) return i; } return MAXELTS; } void IntSet::remove(int v) { int victim = indexOf(v); if (victim != MAXELTS) { elts[victim] = elts[numElts-1]; numElts--; } else { throw v; } } 7 Abstract Data Types Class Exercise int IntSet::indexOf(int v) { for (int i = 0; i < numElts; i++) { if (elts[i] == v) return i; } return MAXELTS; } Question : How many elements of the array must indexOf examine in the worst case if there are 10 elements? If there are 90 elements? 8 Abstract Data Types Improving Efficiency We say the time for indexOf grows linearly with the size of the set. If there are N elements in the set, we have to examine all N of them in the worst case. For large sets that perform lots of queries, this might be too expensive. Luckily, we can replace this implementation with a different one that can be more efficient. The only change we need to make is to the representation – the abstraction can stay precisely the same....
View Full Document

{[ snackBarMessage ]}