12+-+ADT+Efficiency+and+Subtypes

12+-+ADT+Efficiency+and+Subtypes - 5/17/2009 1 ADT...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 5/17/2009 1 ADT Efficiency Ve 280 Programming and Introductory Data Structures ADT Efficiency Subtypes 1 Abstract Data Types Recall Using Classes y Recall our abstraction that held a mutable set of integers. y This is a set in the mathematical sense: y A collection of zero or more integers, with no duplicates. y The set is mutable because we can insert values into, and remove objects from, the set. y 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; 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 5/17/2009 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; return MAXELTS; } y 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 y We say the time for indexOf grows linearly with the size of the set. y 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. y 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

Page1 / 6

12+-+ADT+Efficiency+and+Subtypes - 5/17/2009 1 ADT...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online