ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
SortedListSet.h

The SET as a sorted LIST of section 8.3. More...

Files

file  ADS/set/SortedListSet.h
 The SET as a sorted LIST of section 8.3: insert() in \( O( n ) \), min() and max() in \( O( 1 ) \), the operations of two sets by merging the two lists in \( O( n + m ) \), optimal since merging needs \( \Omega( n + m ) \) (exercises 8.3 and 8.4).

Classes

class  ADS::SortedListSet< T >
 a set as a list sorted in increasing order More...

Functions

template<class T>
SortedListSet< T > ADS::merge (const SortedListSet< T > &A, const SortedListSet< T > &B, bool a, bool b, bool both)
 the merge of the sorted lists of \( A \) and \( B \): an item of \( A \) only, of \( B \) only or of both goes into the result if a, b or both say so; \( O( n + m ) \) time
template<class T>
SortedListSet< T > ADS::setUnion (const SortedListSet< T > &A, const SortedListSet< T > &B)
 the union, \( O( n + m ) \) time (exercise 8.3)
template<class T>
SortedListSet< T > ADS::setIntersection (const SortedListSet< T > &A, const SortedListSet< T > &B)
 the intersection, \( O( n + m ) \) time (the procedure of section 8.3)
template<class T>
SortedListSet< T > ADS::setDifference (const SortedListSet< T > &A, const SortedListSet< T > &B)
 the difference, \( O( n + m ) \) time (exercise 8.4)
template<class T>
SortedListSet< T > ADS::setSymmetricDifference (const SortedListSet< T > &A, const SortedListSet< T > &B)
 the symmetric difference, \( O( n + m ) \) time (exercise 8.5)

Detailed description

The SET as a sorted LIST of section 8.3.

The definitions, the invariants and the costs of the area are in Theory of sets.

Function documentation

◆ merge()

template<class T>
SortedListSet< T > ADS::merge ( const SortedListSet< T > & A,
const SortedListSet< T > & B,
bool a,
bool b,
bool both )

the merge of the sorted lists of \( A \) and \( B \): an item of \( A \) only, of \( B \) only or of both goes into the result if a, b or both say so; \( O( n + m ) \) time

In pseudocode (section 8.3), each comparison passing at least one item:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{merge}( A , B , a , b , \mathit{both} )\quad \triangleright\ \text{which items to keep} \cr 2 & \quad C \gets \emptyset;\ p \gets A.\mathrm{head}();\ q \gets B.\mathrm{head}() \cr 3 & \quad \textbf{while not}\ A.\mathrm{finished}( p )\ \textbf{and not} \ B.\mathrm{finished}( q )\ \textbf{do} \cr 4 & \qquad x \gets A.\mathrm{read}( p );\ y \gets B.\mathrm{read}( q ) \cr 5 & \qquad \textbf{if}\ x < y\ \textbf{then}\quad \triangleright\ x \text{ is only in } A \cr 6 & \qquad\quad \textbf{if}\ a\ \textbf{then}\ C.\mathrm{insertLast}( x ) \cr 7 & \qquad\quad p \gets A.\mathrm{next}( p ) \cr 8 & \qquad \textbf{else if}\ y < x\ \textbf{then}\quad \triangleright\ y \text{ is only in } B \cr 9 & \qquad\quad \textbf{if}\ b\ \textbf{then}\ C.\mathrm{insertLast}( y ) \cr 10 & \qquad\quad q \gets B.\mathrm{next}( q ) \cr 11 & \qquad \textbf{else} \cr 12 & \qquad\quad \textbf{if}\ \mathit{both}\ \textbf{then} \ C.\mathrm{insertLast}( x ) \cr 13 & \qquad\quad p \gets A.\mathrm{next}( p );\ q \gets B.\mathrm{next}( q ) \cr 14 & \quad \text{append to } C \text{ the rest of } A \text{ from } p \text{ if } a \text{, and the rest of } B \text{ from } q \text{ if } b \cr 15 & \quad \textbf{return}\ C \end{array}} \]

◆ setDifference()

template<class T>
SortedListSet< T > ADS::setDifference ( const SortedListSet< T > & A,
const SortedListSet< T > & B )

the difference, \( O( n + m ) \) time (exercise 8.4)

It is the merge above with the items it keeps:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{setDifference}( A , B ) \cr 2 & \quad \textbf{return}\ \mathrm{merge}( A , B , \textbf{true} , \textbf{false} , \textbf{false} ) \end{array}} \]

◆ setIntersection()

template<class T>
SortedListSet< T > ADS::setIntersection ( const SortedListSet< T > & A,
const SortedListSet< T > & B )

the intersection, \( O( n + m ) \) time (the procedure of section 8.3)

It is the merge above with the items it keeps:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{setIntersection}( A , B ) \cr 2 & \quad \textbf{return}\ \mathrm{merge}( A , B , \textbf{false} , \textbf{false} , \textbf{true} ) \end{array}} \]

◆ setSymmetricDifference()

template<class T>
SortedListSet< T > ADS::setSymmetricDifference ( const SortedListSet< T > & A,
const SortedListSet< T > & B )

the symmetric difference, \( O( n + m ) \) time (exercise 8.5)

It is the merge above with the items it keeps:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{setSymmetricDifference}( A , B ) \cr 2 & \quad \textbf{return}\ \mathrm{merge}( A , B , \textbf{true} , \textbf{true} , \textbf{false} ) \end{array}} \]

◆ setUnion()

template<class T>
SortedListSet< T > ADS::setUnion ( const SortedListSet< T > & A,
const SortedListSet< T > & B )

the union, \( O( n + m ) \) time (exercise 8.3)

It is the merge above with the items it keeps:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{setUnion}( A , B ) \cr 2 & \quad \textbf{return}\ \mathrm{merge}( A , B , \textbf{true} , \textbf{true} , \textbf{true} ) \end{array}} \]