Download presentation
Presentation is loading. Please wait.
1
CHAPTER 3 COLLECTIONS SET Collection
2
2 Abstract Data Types A data type consists of a set of values or elements, called its domain, and a set of operators acting on that domain. Abstract Data Type: A set of data values and associated operations that are precisely specified independent of any particular implementation
3
3 ADT specification ADT specification is language independent. It consists of specifying the data values and the operations
4
4 Data values By enumeration (if the set of values is finite) By a rule that specifies an infinite set of values. Examples DaysOfWeek : {Sunday, Monday, …. Saturday} Interval: {(a,b) | a b, a and b are real numbers} RationalNumbers {(a,b) | b 0, a and b are integers}
5
5 Operations Operators' functionality textual description of the behavior Imprecise and may be ambiguous operational specification Example: add two rational numbers (a,b) + (c,d) = (a*d + b*c, b*d) algebraic specification Example: isEmpty(newset) = true
6
6 Syntax of operations Enumerate operators names Identify type of operands Identify type of returned value Example: nextDay: DaysOfWeek x DaysOfWeek Boolean add: RationalNumber x RationalNumber RationalNumber (the notation above is called signature of the operation)
7
7 ADT design and implementation Based on encapsulation : The user of the ADT should not be concerned with how the values are represented, and how the operations are implemented The user should be concerned only with how to create and use objects of a particular ADT ADT interface description
8
8 Interfaces The interface to an ADT tells the user how to use the ADT The interface says what the allowed operations are. The interface says nothing about how the operations are implemented. The class that implements the interface provides the bodies of the methods
9
9 Collections A collection is an object that gathers and organizes other objects (elements). Some fundamental collections are: stack, queue, list, tree, graph, etc. Collections can be broadly categorized as linear (organizes the elements in a straight line) or nonlinear
10
10
11
11 Element Organization The elements within a collection are usually organized based on: the order in which they were added to a collection, some inherent relationship among the elements themselves
12
12 Collections as ADTs Define the collection conceptually as a set of values and a set of operations Define the interface to the ADT Design and implement the corresponding Java class that will contain the data representation and will implement the interface
13
13 Basic operations for collections add and remove elements determine if the collection is empty determine the collection's size iterators, to process each element in the collection operations that interact with other collections
14
14 Iterators An iterator is an object that allows the user to acquire and use each element in a collection in turn The program design determines: the order in which the elements are delivered the way the iterator is implemented Iterator methods: hasNext – returns true if there are more elements in the iteration next – returns the next element in the iteration
15
15 Issues How does the collection operate conceptually? How do we formally define its interface? What kinds of problems does it help us solve? What ways might we implement it? What are the benefits and costs of each implementation?
16
16 Set collection Set Definition: An unordered collection of values where each value occurs at most once. A set collection groups elements without regard to their relationship to each other. That is why it is a nonlinear collection
17
17 Set operations Determine functionality: what operations are needed to create instances of the data type, modify these instances, and obtain values from the data type Example: create a new set. This is usually done by the constructor of the set class add elements to a set. Thus, we need an operation called add. delete an element from a set. Thus, we need an operation called delete
18
18 The operations on a set collection
19
19 Operators’ signatures Generic ‘T’ type notation addAll: SET x SET SET removeRandom: SET T remove:SET x T void union:SET x SET SET contains:SET x T boolean equals:SET x SET boolean isEmpty:SET boolean size:SET int
20
20 The Interface class
21
21
22
22 Operators’ semantics Algebraic definitions. newset is the empty set remove(newset,I) = newset remove(add(S,I),J) = S if I=J else add(delete(S,J),I) contains(newset,I) = false contains(add(S,I),J) = true if I=J else contains(S,J) isEmpty(newset) = true isEmpty(add(S,I)) = false size(newset) = 0 size(add(S,I)) = size(S)+1 if I is not in S else size(S)
23
23 Array Implementation of a Set The goal is to design an efficient implementation that provides the functionality of each operation in the set ADT Arrays are linear structures, sets are non- linear structures The methods should not depend on the particular ordering of the elements in the array
24
24 Array Implementation of a Set
25
25 Managing Capacity An array has a particular number of cells when it is created – its capacity What do we do when the set is full and a new element is added? throw an exception return some kind of status indicator automatically expand the capacity
26
26 The ArraySet Class Representation design: Set elements are kept contiguously at one end of the array An integer (count) represents: the number of elements in the set the next empty index in the array
27
27 Methods of the ArraySet Class Constructors Two versions: default capacity, specified capacity Creates an array with size equal to default/specified capacity Sets count to 0.
28
28 Methods to report set properties public int size() Returns count public boolean isEmpty() Returns true if count = 0, false otherwise
29
29 Element methods public void add (T element) If there the array is full, expands the capacity records the element at array[count], increments count
30
30 Element methods public T removeRandom() throws EmptySetException Using the random generator generates an index for an element to be removed. Moves the last element to the position of the removed element. Decrements count. Returns the removed element.
31
31 Element methods public T remove (T target) throws EmptySetException, NoSuchElementException Uses equals to find the element to be removed. If found, proceeds as in removeRandom. Throws exception if the element is not found or if the set is empty.
32
32 Element methods public boolean contains (T target) Searches the specified element. Returns true if found, false otherwise
33
33 Whole Set Methods public void addAll (SetADT set) Uses the iterator to access and add to the current set each element in the specified set public SetADT union (SetADT set) Creates a new array and adds to it the contents of the current set and the contents of the specified set. Returns the new set public boolean equals (SetADT set) Makes a copy of the current set and the specified set. Using the iterator scans the elements of the current set and removes each scanned element from the copies. The sets are equal if at the end the copies are empty.
34
34 The Iterator and Expand Capacity methods public Iterator iterator() Uses the ArrayIterator ADT that provides hasNext and Next methods to be used with the array that represents the set. private void expandCapacity() Declares a new array with twice the size of the current array Copies the contents of the current array to the new array Sets the new array to be the current array
35
35 Analysis of ArraySet If the array is not full, adding an element to the set is O(1) Expanding the capacity is O(n) Removing a particular element, because it must be found, is O(n) Removing a random element is O(1) Adding all elements of another set is O(n) The union of two sets is O(n+m), where m is the size of the second set
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.