14 - ADT Efficiency and Subtypes

14 - ADT Efficiency and Subtypes - EECS 280 Pr ogr amming...

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

View Full Document Right Arrow Icon
Click to edit Master subtitle style 12/15/10 ADT Efficiency Subtypes EECS 280 Programming and Introductory Data Structures 1 1
Background image of page 1

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

View Full DocumentRight Arrow Icon
12/15/10 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. 2 2
Background image of page 2
12/15/10 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); 3 3
Background image of page 3

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

View Full DocumentRight Arrow Icon
12/15/10 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 4 4
Background image of page 4
12/15/10 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 5
Background image of page 5

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

View Full DocumentRight Arrow Icon
12/15/10 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 6
Background image of page 6
12/15/10 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--; 7 7
Background image of page 7

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

View Full DocumentRight Arrow Icon
12/15/10 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 8 8
Background image of page 8
12/15/10 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 9 9
Background image of page 9

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

View Full DocumentRight Arrow Icon
Abstract Data Types Improving Efficiency 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. However, now we'll keep the elements in
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/14/2010 for the course EECS 280 taught by Professor Noble during the Fall '08 term at University of Michigan.

Page1 / 34

14 - ADT Efficiency and Subtypes - EECS 280 Pr ogr amming...

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

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