{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# 12+-+ADT+Efficiency+and+Subtypes - Abstract Data Types...

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

5/17/2009 1 ADT Efficiency Ve 280 Programming and Introductory Data Structures 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; } 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

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

View Full Document
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; } 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. 9 Abstract Data Types Improving Efficiency y Still use an array (of 100 elements) to store the elements of the set and the values will still occupy the first numElt s slots. y However, now we'll keep the elements in sorted order .
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern