CS211 - Data Structure and Algorithms
CS-211 Data Structure &
Algorithms
Lecture # 1
Abstract Data Types
1
Dr. Sharaf Hussain
CS211 - Data Structure and Algorithms
Introduction
• Data Structure
• A data structure is a specialized format for organizing, processing, retrieving and
storing data.
• There are several basic and advanced types of data structures, all designed to
arrange data to suit a specific purpose.
• Linked List
• Queues
• Hash Tables
• Trees
• Data structures make it easy for users to access and work with the data they need in
appropriate ways.
• Most importantly, data structures frame the organization of information so that
machines and humans can better understand it.
2
Dr. Sharaf Hussain
CS211 - Data Structure and Algorithms
Introduction
• Algorithms
• An algorithm is a sequence of unambiguous instructions for solving a problem, i.e.,
for obtaining a required output for any legitimate input in a finite amount of time.
3
Dr. Sharaf Hussain
CS211 - Data Structure and Algorithms
Introduction
• Primitive Data Types - Programming languages commonly provide data types as part
of the language itself. These data types, known as primitives.
• The simple data types consist of values that are in the most basic form and cannot be
decomposed into smaller parts. Integer and real types, for example, consist of single numeric
values.
• The complex data types, on the other hand, are constructed of multiple components
consisting of simple types or other complex types. In Python, objects, strings, lists, and
dictionaries, which can contain multiple values, are all examples of complex types.
• The primitive types provided by a language may not be sufficient for solving large
complex problems.
• Thus, most languages allow for the construction of additional data types, known as
user-defined types since they are defined by the programmer and not the language.
Some of these data types can themselves be very complex.
4
Dr. Sharaf Hussain
CS211 - Data Structure and Algorithms
Abstraction
• An abstraction is a mechanism for separating the properties of an
object and restricting the focus to those relevant in the current
context.
• The user of the abstraction does not have to understand all of the
details in order to utilize the object, but only those relevant to the
current task or problem.
• Procedural abstraction is the use of a function or method knowing
what it does but ignoring how it’s accomplished.
• Data abstraction is the separation of the properties of a data type (its
values and operations) from the implementation of that data type.
5
Dr. Sharaf Hussain
CS211 - Data Structure and Algorithms
Abstract Data Types (ADT)
• An abstract data type (or ADT) is a programmer-defined data type
that species a set of data values and a collection of well-defined
operations that can be performed on those values.
6
Dr. Sharaf Hussain
CS211 - Data Structure and Algorithms
Abstract Data Types (ADT)
• Constructors: Create and initialize new instances of the ADT.
• Accessors: Return data contained in an instance without modifying it.
• Mutators: Modify the contents of an ADT instance.
• Iterators: Process individual data components sequentially.
7
Dr. Sharaf Hussain
CS211 - Data Structure and Algorithms
General Definitions
• A collection is a group of values with no implied organization or
relationship between the individual values.
• A container is any data structure or abstract data type that stores and
organizes a collection.
• A sequence is a container in which the elements are arranged in linear
order from front to back, with each element accessible by position.
• A sorted sequence is one in which the position of the elements is
based on a prescribed relationship between each element and its
successor.
8
Dr. Sharaf Hussain
CS211 - Data Structure and Algorithms
Defining the ADT
• A date represents a single day in the proleptic Gregorian calendar in which the first day starts
on November 24, 4713 BC.
• Date( month, day, year ): Creates a new Date instance initialized to the given Gregorian date which must
be valid. Year 1 BC and earlier are indicated by negative year components.
• day(): Returns the Gregorian day number of this date.
• month(): Returns the Gregorian month number of this date.
• year(): Returns the Gregorian year of this date.
• monthName(): Returns the Gregorian month name of this date.
• dayOfWeek(): Returns the day of the week as a number between 0 and 6 with 0 representing Monday
and 6 representing Sunday.
• numDays( otherDate ): Returns the number of days as a positive integer between this date and the
otherDate.
• isLeapYear(): Determines if this date falls in a leap year and returns the appropriate boolean value.
• advanceBy( days ): Advances the date by the given number of days. The date is incremented if days is
positive and decremented if days is negative. The date is capped to November 24, 4714 BC, if necessary.
• comparable ( otherDate ): Compares this date to the otherDate to determine their logical ordering. This
comparison can be done using any of the logical operators <, <=, >, >=, ==, !=.
• toString (): Returns a string representing the Gregorian date in the format mm/dd/yyyy. Implemented as
9
the Python operator that is automatically called via the str() constructor.
Dr. Sharaf Hussain
CS211 - Data Structure and Algorithms
Using the ADT
10
Dr. Sharaf Hussain
CS211 - Data Structure and Algorithms
Implementing ADT
11
Dr. Sharaf Hussain
CS211 - Data Structure and Algorithms
Implementing ADT
12
Dr. Sharaf Hussain
CS211 - Data Structure and Algorithms
Implementing ADT
13
Dr. Sharaf Hussain
CS211 - Data Structure and Algorithms
Complex ADT
• Bags
• A bag is a simple container like a shopping bag that can be used to store a
collection of items.
• The bag container restricts access to the individual items by only defining
operations for adding and removing individual items, for determining if an
item is in the bag, and for traversing over the collection of items.
14
Dr. Sharaf Hussain
CS211 - Data Structure and Algorithms
Bags
15
Dr. Sharaf Hussain
CS211 - Data Structure and Algorithms
Selecting a Data Structure
• The implementation of a complex abstract data type typically requires
the use of a data structure for organizing and managing the collection
of data items.
• We have to evaluate the suitability of a data structure for implementing
a given abstract data type, which we base on the following criteria:
• Does the data structure provide for the storage requirements as specified by
the domain of the ADT?
• Does the data structure provide the necessary data access and manipulation
functionality to fully implement the ADT?
• Does the data structure lend itself to an efficient implementation of the
operations?
16
Dr. Sharaf Hussain
CS211 - Data Structure and Algorithms
Bag ADT Implementation
• To help verify a correct implementation of the Bag ADT using the list, we
can outline how each bag operation will be implemented:
• An empty bag can be represented by an empty list.
• The size of the bag can be determined by the size of the list.
• Determining if the bag contains a specific item can be done using the equivalent
list operation.
• When a new item is added to the bag, it can be appended to the end of the list
since there is no specific ordering of the items in a bag.
• Removing an item from the bag can also be handled by the equivalent list
operation.
• The items in a list can be traversed using a for loop and Python provides for user-
defined iterators that be used with a bag.
17
Dr. Sharaf Hussain
CS211 - Data Structure and Algorithms
18
Dr. Sharaf Hussain
CS211 - Data Structure and Algorithms
Iterators
• An iterator is an object that provides a mechanism for performing
generic traversals through a container without having to expose the
underlying implementation.
• To use Python's traversal mechanism with our own abstract data
types, we must define an iterator class, which is a class in Python
containing two special methods, __iter__ and __next__ . Iterator
classes are commonly defined in the same module as the
corresponding container class.
19
Dr. Sharaf Hussain
CS211 - Data Structure and Algorithms
Iterators
20
Dr. Sharaf Hussain
CS211 - Data Structure and Algorithms
Application: Student Records
21
Dr. Sharaf Hussain
CS211 - Data Structure and Algorithms
Application:
Student File Reader
ADT
22
Dr. Sharaf Hussain
CS211 - Data Structure and Algorithms
Application: Student File Reader ADT -
Implementation
23
Dr. Sharaf Hussain
CS211 - Data Structure and Algorithms
Application: Student File
Reader ADT -
Implementation
24
Dr. Sharaf Hussain
CS211 - Data Structure and Algorithms
Application: Student File
Reader ADT -
Implementation
25
Dr. Sharaf Hussain
CS211 - Data Structure and Algorithms
Application: Student File Reader ADT -
Implementation
26
Dr. Sharaf Hussain