data structurestructure, list based implementation, maps – map abstract data type (adt), list...

24

Upload: others

Post on 20-Apr-2020

31 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Data StructureStructure, List Based Implementation, Maps – Map Abstract Data Type (ADT), List Based Implementation, Multi-dimensional Arrays – Multi-Array Abstract Data Type (ADT),
Page 2: Data StructureStructure, List Based Implementation, Maps – Map Abstract Data Type (ADT), List Based Implementation, Multi-dimensional Arrays – Multi-Array Abstract Data Type (ADT),

Data Structure(As per the Syllabus 2016-17 of Mumbai University for F.Y.B.Sc. – CS, Semester II)

Prof. Krupa Balsara KamdarM.Sc. in Computer Science,

B.Sc. Computer Science Coordinator,Bhavan’s College, Mumbai.

Prof. Kiran GurbaniB.E., MCA, M.Phil.,

Head of Computer Science Department,R.K. Talreja College of Arts, Science & Commerce,

Ulhasnagar - 3.

ISO 9001 : 2008 CERTIFIED

Page 3: Data StructureStructure, List Based Implementation, Maps – Map Abstract Data Type (ADT), List Based Implementation, Multi-dimensional Arrays – Multi-Array Abstract Data Type (ADT),

© AuthorsNo part of this publication may be reproduced, stored in a retrieval system, or transmitted in anyform or by any means, electronic, mechanical, photocopying, recording and/or otherwise withoutthe prior written permission of the publishers.

First Edition : 2017

Published by : Mrs. Meena Pandey for Himalaya Publishing House Pvt. Ltd.,“Ramdoot”, Dr. Bhalerao Marg, Girgaon, Mumbai - 400 004.Phone: 022-23860170/23863863, Fax: 022-23877178E-mail: [email protected]; Website: www.himpub.com

Branch Offices :New Delhi : “Pooja Apartments”, 4-B, Murari Lal Street, Ansari Road, Darya Ganj,

New Delhi - 110 002. Phone: 011-23270392, 23278631; Fax: 011-23256286Nagpur : Kundanlal Chandak Industrial Estate, Ghat Road, Nagpur - 440 018.

Phone: 0712-2738731, 3296733; Telefax: 0712-2721216

Bengaluru : Plot No. 91-33, 2nd Main Road Seshadripuram, Behind Nataraja Theatre,Bengaluru - 560020. Phone: 080-41138821,Mobile: 09379847017, 09379847005

Hyderabad : No. 3-4-184, Lingampally, Besides Raghavendra Swamy Matham, Kachiguda,Hyderabad - 500 027. Phone: 040-27560041, 27550139

Chennai : New No. 48/2, Old No. 28/2, Ground Floor, Sarangapani Street, T. Nagar,Chennai - 600 012. Mobile: 09380460419

Pune : First Floor, "Laksha" Apartment, No. 527, Mehunpura, Shaniwarpeth(Near Prabhat Theatre), Pune - 411 030. Phone: 020-24496323/24496333;Mobile: 09370579333

Lucknow : House No. 731, Shekhupura Colony, Near B.D. Convent School, Aliganj,Lucknow - 226 022. Phone: 0522-4012353; Mobile: 09307501549

Ahmedabad : 114, “SHAIL”, 1st Floor, Opp. Madhu Sudan House, C.G. Road, Navrang Pura,Ahmedabad - 380 009. Phone: 079-26560126; Mobile: 09377088847

Ernakulam : 39/176 (New No.: 60/251) 1st Floor, Karikkamuri Road, Ernakulam,Kochi – 682011. Phone: 0484-2378012, 2378016 Mobile: 09387122121

Bhubaneswar : 5 Station Square, Bhubaneswar - 751 001 (Odisha).Phone: 0674-2532129, Mobile: 09338746007

Kolkata : 108/4, Beliaghata Main Road, Near ID Hospital, Opp. SBI Bank,Kolkata - 700 010, Phone: 033-32449649, Mobile: 07439040301

DTP by : Sunanda

Printed at : Rose Fine Art, Mumbai. On behalf of HPH.

Page 4: Data StructureStructure, List Based Implementation, Maps – Map Abstract Data Type (ADT), List Based Implementation, Multi-dimensional Arrays – Multi-Array Abstract Data Type (ADT),

PREFACE

It gives us immense pleasure to present the “Data Structures” book to the teachers and studentsof B.Sc. Computer Science, First Year, Second Semester. This book meticulously follows the revisedsyllabus pattern of Mumbai University which has been proposed in the current academic year, June2016.

The standard second course in Computer Science has traditionally covered the fundamental datastructures and algorithms, but more recently these topics have been included in the broader topic ofabstract data types. The importance of designing and using abstract data types for easier modularprogramming is emphasized throughout the text.

The traditional data structures are also presented throughout the text in terms of implementing thevarious abstract data types. Multiple implementations using different data structures are usedthroughout the text to reinforce the abstraction concept. Common algorithms are also presentedthroughout the text as appropriate to provide complete coverage of the typical data structures course.

The basic concepts related to abstract data types, data structures, and algorithms are presented inthe first four chapters. Later chapters are build on these earlier concepts to present more advancedtopics and introduce the student to additional abstract data types and more advanced data structures.

In recent years, many universities have begun to adopt the Python language for introducingstudents to programming and problem solving. Python provides several benefits over other languagessuch as C++ and Java, the most important of which is Python that has a simple syntax which is easierto learn. This book expands upon the use of Python by providing a Python-centric text for the datastructures course.

Suggestions and feedbacks related to the book will be appreciated.

Krupa Balsara Kamdar Kiran [email protected] [email protected]

8452041062 9637128628/7769979964

Page 5: Data StructureStructure, List Based Implementation, Maps – Map Abstract Data Type (ADT), List Based Implementation, Multi-dimensional Arrays – Multi-Array Abstract Data Type (ADT),

ACKNOWLEDGEMENTS

First and foremost, we would like to thank Lord Swaminarayana and our beloved Guru HDHPramukh Swami Maharaj and HDH Mahant Swami Maharaj. In the process of putting this booktogether, we realized how valuable this gift of writing is for us. Swamibapa! You have given us thepower to believe in our passion and pursuing our dreams. We would have never done this without yourkind blessings. Miss you SWAMIBAPA.

There are a numbers of individuals I would like to thank for helping to make this book possible.My father Mr. Ramesh Bhai Balsara, role model of my life, my mother, my in-laws for having faith inme, my mentor in the early days of my career Mrs. Neeta Patil. A special thanks to myBhavan’s Collegues who are like family to me, have optimistically allowed me to take thisopportunity and helped me in every step of it.

I would like to thank my husband Mr. Pramod Kamdar and son Master Sharad Kamdar, whohas been a strong support throughout and has helped identify the potential in me.

Mrs. Krupa Kamdar

I would like to dedicate this book to my mother and father Kavita S. Bajaj and Sahijram Bajaj.I would like to thank my son Chirag Gurbani for being my stress buster and a motivating force

for putting a better performance every time.My sincerest big thanks for a lifetime to Mr. S.K. Srivastava of Himalaya Publishing House Pvt.

Ltd. for giving me best writing advice and motivation and whose belief in this book has kept memotivated and inspired and encouraged on even the darkest of days.

Mrs. Kiran Gurbani

Page 6: Data StructureStructure, List Based Implementation, Maps – Map Abstract Data Type (ADT), List Based Implementation, Multi-dimensional Arrays – Multi-Array Abstract Data Type (ADT),

SYLLABUS

Unit Topics LecturesUnit -I Abstract Data Types: Introduction, The Date Abstract Data Type, Bags,

Iterators. ApplicationArrays: Array Structure, Python List, Two Dimensional Arrays, MatrixAbstract Data Type, ApplicationSets and Maps: Sets – Set Abstract Data Type (ADT), Selecting DataStructure, List Based Implementation, Maps – Map Abstract Data Type(ADT), List Based Implementation, Multi-dimensional Arrays – Multi-Array Abstract Data Type (ADT), Implementing Multiarrays, ApplicationAlgorithm Analysis: Complexity Analysis-Big-O Notation, EvaluatingPython Code, Evaluating Python List, Amortized Cost, Evaluating SetAbstract Data Type (ADT), ApplicationSearching and Sorting: Searching – Linear Search, Binary Search,Sorting – Bubble, Selection and Insertion Sort, Working with SortedLists – Maintaining Sorted List, Maintaining Sorted Lists.

[15]

Unit-II Linked Structures: Introduction, Singly Linked List – Traversing,Searching, Prepending and Removing Nodes, Bag Abstract Data Type(ADT) – Linked List Implementation. Comparing Implementations,Linked List Iterators, More Ways to Build Linked Lists, Applications –PolynomialsStacks: Stack Abstract Data Type (ADT), Implementing Stacks – UsingPython List, Using Linked List, Stack Applications – Balanced Delimiters,Evaluating Postfix ExpressionsQueues: Queue Abstract Data Type (ADT), Implementing Queue – UsingPython List, Circular Array, Using List, Priority Queues – Priority QueueAbstract Data Type (ADT), Bounded and unbounded Priority QueuesAdvanced Linked List: Doubly Linked Lists – Organization andOperation, Circular Linked List – Organization and Operation, Multi Lists

[15]

Unit-III Recursion: Recursive Functions, Properties of Recursion, Its Working,Recursive ApplicationsHash Table: Introduction, Hashing – Linear Probing, Clustering,Rehashing, Separate Chaining, Hash FunctionsAdvanced Sorting: Merge Sort, Quick Sort, Radix Sort, Sorting LinkedListBinary Trees: Tree Structure, Binary Tree – Properties, Implementationand Traversals, Expression Trees, Heaps and Heapsort, Search Trees

[15]

Page 7: Data StructureStructure, List Based Implementation, Maps – Map Abstract Data Type (ADT), List Based Implementation, Multi-dimensional Arrays – Multi-Array Abstract Data Type (ADT),

PAPER PATTERN

Max. Marks: 75 Time: 2 ½ Hrs.All questions are compulsory.

Q-1 FROM ALL UNITS (I, II, III)Attempt All (Each of 5 Marks)(a) Multiple choice questions (MCQ) 5 Marks(b) Fill in the blanks 5 Marks(c) Short notes 5 Marks

15 MARKS

Q-2 FROM UNIT-I (100% OPTION)Attempt any three from following(a) 5 Marks(b) 5 Marks(c) 5 Marks(d) 5 Marks(e) 5 Marks(f) 5 Marks

15 MARKS

Q-3 FROM UNIT-II (100% OPTION)Attempt any three from following(a) 5 Marks(b) 5 Marks(c) 5 Marks(d) 5 Marks(e) 5 Marks(f) 5 Marks

15 MARKS

Q-4 FROM UNIT-III (100% OPTION)Attempt any three from following(a) 5 Marks(b) 5 Marks(c) 5 Marks(d) 5 Marks(e) 5 Marks(f) 5 Marks

15 MARKS

Q-5 FROM ALL UNITS (I, II, III) (75% OPTION)Attempt any three from following(a) 5 Marks(b) 5 Marks(c) 5 Marks(d) 5 Marks(e) 5 Marks(f) 5 Marks

15 MARKS

Page 8: Data StructureStructure, List Based Implementation, Maps – Map Abstract Data Type (ADT), List Based Implementation, Multi-dimensional Arrays – Multi-Array Abstract Data Type (ADT),

CONTENTS

UNIT I

1. Abstract Data Types 1 – 152. Arrays 16 – 303. Sets and Maps 31 – 454. Algorithm Analysis 46 – 575. Searching and Sorting 58 – 71

UNIT II

6. Linked Structure 72 – 877. Stack 88 – 968. Queue 97 – 1089. Advanced Linked List 109 – 122

UNIT III

10. Recursion 123 – 13311. Hash Table 134 – 14512. Advanced Sorting 146 – 16613. Binary Tree 167 – 19014. Search Tree 191 – 200

Practicals

Practical 1: Linear Search 201 – 202Practical 2: Binary Search 203 – 204Practical 3: Sorting Algorithms 205 – 210Practical 4: Set 211 – 214Practical 5: Stack Implementation 215 – 216Practical 6: (a) Infix to Postfix Conversion

(b) Postfix Evaluation217 – 220

Practical 7: Queue 221 – 223Practical 8: Linked List 224 – 226Practical 9: Binary Tree 227 – 228Practical 10: Recursion 229 – 232

Page 9: Data StructureStructure, List Based Implementation, Maps – Map Abstract Data Type (ADT), List Based Implementation, Multi-dimensional Arrays – Multi-Array Abstract Data Type (ADT),
Page 10: Data StructureStructure, List Based Implementation, Maps – Map Abstract Data Type (ADT), List Based Implementation, Multi-dimensional Arrays – Multi-Array Abstract Data Type (ADT),

Unit I

CHAPTER

1 Abstract Data Types

Structure:1.1 Introduction

1.1.1 Abstractions1.1.2 Abstract Data Type (ADT)

1.2 The Date Abstract Data Type (ADT)1.2.1 Preconditions and Postconditions

1.3 Implementing Date Abstract Data Type (ADT)1.4 The Bag Abstract Data Type (ADT)

1.4.1 Why a Bag Abstract Data Type (ADT)?1.4.2 Selecting a Data Structure for Bag Implementation

1.4.2.1 List-Based Implementation1.5 Iterators

1.5.1 Designing an Iterator1.5.2 Using Iterators

1.6 Advantages of Abstract Data Types (ADT’s)1.7 Application: Student Records

1.7.1 Designing a Solution1.7.2 Implementation

1.1 Introduction The foundation of computer science is based on the study of algorithms. An algorithm is a sequence of clear and precise step-by-step instructions for solving a

problem in a finite amount of time. Algorithms are implemented by translating the step-by-step instructions in to a computer

program that can be executed by a computer. This translation process is called computer programming or simply programming. Data is stored in a computer as a sequence of binary digits.

Page 11: Data StructureStructure, List Based Implementation, Maps – Map Abstract Data Type (ADT), List Based Implementation, Multi-dimensional Arrays – Multi-Array Abstract Data Type (ADT),

2 Data Structure

Type is a collection of values. Data type is a given collection of values along with a collection of operations.

Data types can be divided into two groups: Simple data types consists of single values. E.g integer, real, char etc. Complex (or composite) data types consist of multiple components. E.g. objects, Strings,

List, dictionary etc. The primitive data types provided by a language may not be sufficient for solving large

complex problems. Thus, most language allow for the construction of additional data type,called user defined types as these are defined by programmer and not the language.

1.1.1 Abstractions An abstraction is a mechanism for separating the properties of an object and restricting the

focus only on relevant data. There are two types of abstraction procedural abstraction andData abstraction. Procedural abstraction is the use of a function or method knowing what itdoes but ignoring how it is accomplished. Data Abstraction is the separation of the propertiesof a data type (its values and operations) from the implementation of that data type.

Abstraction is the quality of dealing with ideas rather than events. For example, when youconsider the case of e-mail, complex details such as what happens as soon as you send an e-mail, the protocol your e-mail server uses are hidden from the user. Therefore, to send an e-mail you just need to type the content, mention the address of the receiver, and click send.

Likewise in Object-oriented programming, abstraction is a process of hiding theimplementation details from the user, only the functionality will be provided to the user. Inother words, the user will have the information on what the object does instead of how it doesit.

1.1.2 Abstract Data Type (ADT) An abstract data type (ADT) is a programmer defined data type that specifies a set of data

values and a collection of well-defined operations that can be performed on those values.Abstract Data Type (ADT) are defined independent of their implementation, allowing us tofocus on the use of the new data type instead of how it is implemented.

By hiding the implementation details, we can work with an abstraction and focus on whatfunctionality the Abstract Data Type (ADT) provides instead of how that functionality isimplemented.

Other user programs interact with instances of the Abstract Data Type (ADT) by involvingone of the several operations defined by its interface. The set of operations can be groupedinto four categories:1. Constructors: create and initialize new instances of the Abstract Data Type (ADT).2. Assessors: return data contained in an instance without modifying it.3. Mutators: modify the contents of an Abstract Data Type (ADT) instance.4. Iterators: process individual data components sequentially.

Page 12: Data StructureStructure, List Based Implementation, Maps – Map Abstract Data Type (ADT), List Based Implementation, Multi-dimensional Arrays – Multi-Array Abstract Data Type (ADT),

3Abstract Data Types

UserProgram

String ADT

-str()-upper()-lower()

User program interacts withADTs through their interfaceor set of operations.

The implementationDetails are hiddeninside a black box

Fig. 1: Separating ADT Definition from its Implementation

There are several Advantages of working with Abstract Data Type (ADT).1. We can focus on solving the problem at hand instead of getting bogged down in the

implementation details.2. We can reduce logical errors that can occur from accidental misuse of storage structure and

data types by preventing direct access to the implementation.3. The implementation of the Abstract Data Type (ADT) can be changed without having to

modify the program code that uses the Abstract Data Type (ADT).4. It’s easier to manage and divide larger programs into smaller modules, allowing different

members of a team to work on the separate modules.

1.2 The Date ABSTRACT DATA TYPE (ADT):The Abstract Data Type (ADT) definition should provide a clear description of the Abstract Data

Type (ADT)) including both its domain and each of its operations as only those operations specifiedcan be performed on an instance of the Abstract Data Type (ADT) . Let’s see the definition of a simpleabstract data type for representing a date in the Gregorian calendar.

A date represents a single day in the Gregorian calendar in which the first day starts onNovember 24, 4713 BC.

1. Date( month, day, year ): Creates a new Date instance initialized to the given Gregorian datewhich must be valid. Year 1 BC and earlier are indicated by negative year components.

2. day(): Returns the Gregorian day number of this date.3. month(): Returns the Gregorian month number of this date.4. year(): Returns the Gregorian year of this date.5. monthName(): Returns the Gregorian month name of this date.6. dayOfWeek(): Returns the day of the week as a number between 0 and 6 with 0 representing

Monday and 6 representing Sunday.7. numDays( otherDate ): Returns the number of days as a positive integer between this date

and the otherDate.

Page 13: Data StructureStructure, List Based Implementation, Maps – Map Abstract Data Type (ADT), List Based Implementation, Multi-dimensional Arrays – Multi-Array Abstract Data Type (ADT),

4 Data Structure

8. isLeapYear(): Determines if this date falls in a leap year and returns the appropriate booleanvalue.

9. advanceBy( days ): Advances the date by the given number of days. The date is incrementedif days is positive and decremented if days is negative. The date is capped to November 24,4714 BC, if necessary.

10. comparable ( otherDate ): Compares this date to the otherDate to determine their logicalordering. This comparison can be done using any of the logical operators <, <=, >, >=, ==, !=.

11. toString (): Returns a string representing the Gregorian date in the format mm/dd/yyyy.Implemented as the Python operator that is automatically called via the str() constructor.

Example 1: Extracts a collection of birth dates from the user and determines if each individual isat least 21 years of age.

from date import Datedef main():

bornBefore = Date(6, 1, 1988)date = promptAndExtractDate()while date is not None :

if date <= bornBefore :print( "Is at least 21 years of age: ", date )

date = promptAndExtractDate()def promptAndExtractDate():

print( "Enter a birth date." )month = int( input("month (0 to quit): ") )if month == 0 :

return Noneelse :

day = int( input("day: ") )year = int( input("year: ") )return Date( month, day, year )

main()

1.2.1 Preconditions and Postconditions-While defining the operations, we must include a specification of required inputs and the

resulting output, if any. We must also specify the preconditions and postconditions for each operation. A precondition indicates the condition or state of the Abstract Data Type (ADT) instance and

inputs before the operation can be performed. An operation have minimum one precondition. The precondition is assumed to be true. An attempt to perform an operation in which the precondition is not satisfied will be tagged as

an error.

Page 14: Data StructureStructure, List Based Implementation, Maps – Map Abstract Data Type (ADT), List Based Implementation, Multi-dimensional Arrays – Multi-Array Abstract Data Type (ADT),

5Abstract Data Types

A postcondition indicates the result or ending state of the Abstract Data Type (ADT) instanceafter the operation is performed. The precondition is assumed to be true while thepostcondition is a guarantee as long as the preconditions are met.

Some operations may not have any precondition other than initialization. Some operations mayalso do not have postconditions it totally depends on the type of Abstract Data Type (ADT) used.

1.3 Implementing Date Abstract Data Type (ADT) -There are two common approaches to storing a date in an object.1. One approach stores the three components—month, day, and year—as three separate fields.

With this format, it is easy to access the individual components, but it's difficult to comparetwo dates or to compute the number of days between two dates since the number of days in amonth varies from month to month.

2. The second approach stores the date as an integer value representing the Julian day, which isthe number of days elapsed since the initial date of November 24, 4713 BC (using theGregorian calendar notation).

CODE EXPLANATIONclass Date :def __init__(self, mth, dy, yr):

self._julianDay = 0assert self._isValidGregorian( mth, dy, yr ), \

"Invalid Gregorian date."

Constructor with precondition tocheck if the Gregorian date is valid.

tmp = 0if mth < 3 :

tmp = −1self._julianDay = dy - 32075 + \(1461 * (yr + 4800 + tmp)// 4) + \ (367 * (mth - 2 - tmp * 12) // 12) - \

(3 * ((yr + 4900 + tmp) // 100) // 4)

Code to convert Gregorian date toJulian day number.

def get_month(self):return (self._toGregorian())[0]

Returns month number.

def get_day( self ):return (self._toGregorian())[1]

Returns day number.

def year( self ):return (self._toGregorian())[2]

Returns the year.

def dayOfWeek( self ):mth, dy, yr = self._toGregorian()if mth < 3 :

mth = mth + 12yr = yr - 1return ((13 * mth + 3) // 5 + dy + \ yr + yr // 4 - yr //100 + yr // 400) % 7

Returns the day of week (i.e 0…6)

Page 15: Data StructureStructure, List Based Implementation, Maps – Map Abstract Data Type (ADT), List Based Implementation, Multi-dimensional Arrays – Multi-Array Abstract Data Type (ADT),

6 Data Structure

def __str__(self):mth, dy, yr = self._toGregorian()return "%02d/%02d/%04d" % (mth, dy, yr)

Returns the day as a string.

def __eq__(self, otherDate):return self._julianDay == otherDate._julianDay

It compresses any two dates.

def __lt__(self, otherDate):return self._julianDay < otherDate._julianDay

It returns true if one Julian date is lessthan another Julian date.

def __le__(self, otherDate):return self._julianDay <= otherDate._julianDay

It returns true if one Julian date is lessthan or equal to another Julian date.

def _toGregorian(self):A = self._julianDay + 68569B = 4 * A // 146097A = A - (146097 * B + 3) // 4yr = 4000 * (A + 1) // 1461001A = A - (1461 * yr // 4) + 31mth = 80 * A // 2447dy = A - (2447 * mth // 80)A = mth // 11mth = mth + 2 - (12 * A)yr = 100 * (B - 49) + yr + Areturn mth, dy, yr

Returns the Gregorian date in form oftuple month, day and year.

1.4 The Bag Abstract Data Type (ADT) :A bag is a basic container like a shopping bag that can be used to store collections. There are

several variations: Simple bag, Grab bag, counting bag etc. A simple bag is a container that stores acollection with duplicate values allowed. The elements are stored individually, have no particularorder but they must be comparable. A grab bag is similar to the simple bag but the items are removedfrom the bag at random. A counting bag includes an operation that returns the number of occurrencesin the bag of a given item. Consider Simple Bag Operations

1. Bag()-Creates a bag that is initially empty.2. length()-Returns the number of items stored in the bag. Accessed using the Len() function.3. contains(item)- Determines if the given target item is stored in the bag and returns the

appropriate Boolean value. Accessed using the in operator.4. add(item)- Adds the given item to the bag.5. remove(item)- Removes and returns an occurrence of item from the bag. An exception is

raised if the element is not in the bag6. iterator()-Creates and returns an iterator that can be used to iterate over the collection of

items.Bag Example 1: Following example creates a bag and fills it with values. The user is then

prompted to guess a value contained in the bag.

Page 16: Data StructureStructure, List Based Implementation, Maps – Map Abstract Data Type (ADT), List Based Implementation, Multi-dimensional Arrays – Multi-Array Abstract Data Type (ADT),

7Abstract Data Types

myBag = Bag()myBag.add_data( 19 )myBag.add_data( 74 )myBag.add_data( 23 )myBag.add_data( 19 )myBag.add_data( 12 )

val = int( input("Guess a value contained in the bag.") )if val in myBag:

print( "The bag contains the value", val )else :

print( "The bag does not contain the value", val )

1.4.1 Why a Bag Abstract Data Type (ADT)?Python's list can accomplish the same thing as a Bag Abstract Data Type (ADT). So, why do we

need a new Abstract Data Type (ADT)? For a small program, the use of a list may be appropriate. Forlarge programs the use of new Abstract Data Type (ADT) provide several advantages. By workingwith the abstraction of a bag, we can:

1. Focus on solving the problem at hand instead of how the list will be used.2. Reduce the chance of errors or misuse of the list.3. Provide better coordination between different modules and programmers.4. Easily swap out one Bag implementation for a possibly more efficient version.

1.4.2 Selecting a Data Structure for Bag ImplementationWe have to evaluate the suitability of a data structure for implementing a given abstract data type,

which we base on the following criteria:1. Does the data structure provide for the storage requirements as specified by the domain of the

Abstract Data Type (ADT))?2. Does the data structure provide the necessary data access and manipulation functionality to

fully implement the Abstract Data Type (ADT))?3. Does the data structure lend itself to an efficient implementation of the operations? - An

important goal in the implementation of an abstract data type is to provide an efficientsolution. Some data structures allow for a more efficient implementation than others, but notevery data structure is suitable for implementing every Abstract Data Type (ADT)).Efficiency considerations can help to select the best structure from among multiple candidates.

Both the list and dictionary structures could be used to implement the Bag Abstract Data Type(ADT). For the simple version of the bag, however, the list is a better choice since the dictionarywould require twice as much space to store the contents of the bag in the case where most of the itemsare unique.

To help verify a correct implementation of the Bag Abstract Data Type (ADT) using the list, wecan outline how each bag operation will be implemented:

Page 17: Data StructureStructure, List Based Implementation, Maps – Map Abstract Data Type (ADT), List Based Implementation, Multi-dimensional Arrays – Multi-Array Abstract Data Type (ADT),

8 Data Structure

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.

1.4.2.1 List-Based Implementation-Implements the Bag ABSTRACT DATA TYPE (ADT) container using a Python list.

CODE EXPLAINATIONclass Bag :def __init__(self):

self._ theItems = list()Constructs an empty bag.

def __len__(self):return len(self._theItems)

Returns the number ofelements in the bag.

def __contains__(self,element):return element in self._theItems

Determines if an element iscontained in the bag.

def add_data(self, element):self._theItems.append(elements)

Adds a new element to thebag.

def remove_data(self, element):assert element in self._theItems, "The element must be inthe bag."ndx = self._theItems.index(element)return self._theItems.pop(ndx)

Removes and returns aninstance of the item from thebag. It has one preconditionto check whether the elementis present then only removalis possible.

Fig. 2: Sample Instance of the Bag Class implemented Using a List

To conserve space and reduce the clutter, we can illustrate objects in the text as boxes withrounded edges and show them stored directly within the list structure. Variables will be illustrated assquare boxes with a bullet in the middle and the name of the variable printed nearby.

Page 18: Data StructureStructure, List Based Implementation, Maps – Map Abstract Data Type (ADT), List Based Implementation, Multi-dimensional Arrays – Multi-Array Abstract Data Type (ADT),

9Abstract Data Types

1.5 IteratorsTraversals are very common operations, especially on containers. A traversal iterates over the

entire collection, providing access to each individual element. Traversals can be used for a number ofoperations, including searching for a specific item or printing an entire collection.

Python's container types-strings, tuples, lists, and dictionaries-can be traversed using the for loopconstruct. For our user-defined abstract data types, we can add methods that perform specific traversaloperations when necessary.

An iterator is an object that provides a mechanism for performing generic traversalsthrough a container without having to expose the underlying implementation.

Iterators are used with Python’s for loop construct to provide a traversal mechanism for bothbuilt-in and user-defined containers.

Example of for loop to traverse the collection of dates:for date in bag :

if date <= bornBefore :print( "Is at least 21 years of age: ", date )

1.5.1 Designing an IteratorAn iterator for the Bag Abstract Data Type (ADT) implemented as a Python list.

CODE EXPLANATIONclass _BagIterator :def __init__( self, myList ):

self._bagElements = myListself._curElements = 0

Initializes the constructor.

def __iter__( self ):return self

It returns a reference to theobject itself.

def __next__( self ):if self._curElements <len( self._bagElements ) :item = self._bagElements [ self._curElements ]self._curElements+= 1

return itemelse :

raise StopIteration

It returns the next item inthe container

1.5.2 Using Iterators-Now we can directly use Bag instance with for loop.for item in bag :

print(item)

Page 19: Data StructureStructure, List Based Implementation, Maps – Map Abstract Data Type (ADT), List Based Implementation, Multi-dimensional Arrays – Multi-Array Abstract Data Type (ADT),

10 Data Structure

1.6 Advantages of Abstract Data Type (ADT)s:Some advantages of Abstract Data Type (ADT) over the conventional data structures are: Abstract Data Type (ADT) is reusable, robust, and is based on principles of Object Oriented

Programming (OOP) and Software Engineering (SE). An Abstract Data Type (ADT) can be re-used at several places and it reduces coding efforts. Encapsulation ensures that data cannot be corrupted. Working of various integrated operation cannot be tampered with by the application program Abstract Data Type (ADT) ensures a robust data structure.Abstract Data Type (ADT) approach is based on the Software Engineering concepts of coupling

and cohesion. Coupling property determines how strongly two separate parts of programs are linkedtogether and at what extend changes made in one part impacts the other parts of a software module.

Well-designed software has a minimum coupling. An Abstract Data Type (ADT) promotes weakcoupling. Cohesion determines how well-integrated are components of software. An Abstract DataType (ADT) inherently promotes maximum cohesion.

1.7 Application: Student RecordsData is commonly extracted from files stored on disk, from databases, and even from remote sites

through web services.For example, suppose we have a collection of records stored on disk that contain information

related to students at College. We have been assigned the task to extract this information and producea report similar to the following in which the records are sorted by identification number.

List of StudentsID NAME CLASS GPA----- ------------------------- ---------- ----10015 Smith, John Sophomore 3.0110167 Jones, Wendy Junior 2.8510175 Smith, Jane Senior 3.9210188 Wales, Sam Senior 3.2510200 Roberts, Sally Freshman 4.0010208 Green, Patrick Freshman 3.9510226 Nelson, Amy Sophomore 2.9510334 Roberts, Jane Senior 3.8110387 Taylor, Susan Sophomore 2.1510400 Logan, Mark Junior 3.3310485 Brown, Jessica Sophomore 2.91--------------------------------------------------

Page 20: Data StructureStructure, List Based Implementation, Maps – Map Abstract Data Type (ADT), List Based Implementation, Multi-dimensional Arrays – Multi-Array Abstract Data Type (ADT),

11Abstract Data Types

Number of students: 111. the student's id number represented as an integer;2. their first and last names, which are strings;3. an integer classification code in the range [1 : : : 4] that indicates if the student is a freshman,

sophomore, junior, or senior; and4. their current grade point average represented as a floating point value. What we have not been

told, however, is how the data is stored on disk.

1.7.1 Designing a SolutionEven though we have not yet been told the type of _le or the format used to store the data, we can

begin designing and implementing a solution by working with an abstraction of the input source.The extraction of data records from external storage requires similar steps:1. Open a connection, extract the individual records, then close the connection.2. We define a Student File Reader ADT to represent the extraction of data from an external File

or database.In computer programming, an object used to input data into a program is sometimes referred to as

a reader while an object used to output data is referred to as a writer.

Student File Reader ADTA student File reader is used to extract student records from external storage.The data components of the individual records are extracted and stored in a storage object speci_c

for this collection of student records.StudentFileReader( filename ): Creates a student reader instance for extracting student records

from the given File. The type and format of the File is dependent on the specific implementation. open(): Opens a connection to the input source and prepares it for extracting student records.

If a connection cannot be opened, an exception is raised. close(): Closes the connection to the input source. If the connection is not currently open, an

exception is raised. fetchRecord(): Extracts the next student record from the input source and returns a reference

to a storage object containing the data. None is returned when there are no additional recordsto be extracted. An exception is raised if the connection to the input source was previouslyclosed.

fetchAll(): The same as fetchRecord(), but extracts all student records (or those remaining)from the input source and returns them in a Python list.

Creating the Report The program is used to produce the sample report. The program extracts the student records

from the input source, sorts the records by student identification number, and produces thereport.

By using the Student File Reader ADT, we are able to design a solution and construct aprogram for the problem at hand without knowing exactly how the data is stored in theexternal source.

Page 21: Data StructureStructure, List Based Implementation, Maps – Map Abstract Data Type (ADT), List Based Implementation, Multi-dimensional Arrays – Multi-Array Abstract Data Type (ADT),

12 Data Structure

We import the StudentFileReader class from the studentfile.py module, which we assume willbe an implementation of the ADT that handles the actual data extraction.

Further, if we want to use this same program with a data _le having a different format, theonly modifications required will be to indicate a different module in the import statement andpossibly a change to the filename specified by the constant variable FILE NAME.

The studentreport.py program consists of two functions: printReport() and main(). The mainroutine uses an instance of the ADT to connect to the external source in order to extract thestudent records into a list.

The studentreport.py program.

CODE EXPLANATIONfrom studentfile import StudentFileReader

FILE_NAME = "students.txt"def main():

reader = StudentFileReader( FILE_NAME )reader.open()studentList = reader.fetchAll()reader.close()

16 studentList.sort( key = lambda rec:rec.idNum )

printReport( studentList )

def printReport( theList ):classNames = ( None, "Freshman", "Sophomore","Junior", "Senior" )

print( "LIST OF STUDENTS".center(50) )print( "" )

print( "%-5s %-25s %-10s %-4s" % ('ID','NAME', 'CLASS', 'GPA' ) )

print( "%5s %25s %10s %4s" % ('-' * 5, '-' *25, '-' * 10, '-' * 4))for record in theList :

print( "%5d %-25s %-10s %4.2f" % \(record.idNum, \record.lastName + ', ' + record.firstName,classNames[record.classCode], record.gpa) )print( "-" * 50 )print( "Number of students:", len(theList) )main()

Importing a fileName of the file to open.

Extract the student records from the giventext file.

Sort the list by id number. Each object ispassed to the lambda

expression which returns the idNum field ofthe object.Print the student report.Prints the student report.The class names associated with the classcodes.Print the header.

Print the body.

Add a footer.

Executes the main routine.

Page 22: Data StructureStructure, List Based Implementation, Maps – Map Abstract Data Type (ADT), List Based Implementation, Multi-dimensional Arrays – Multi-Array Abstract Data Type (ADT),

13Abstract Data Types

1.7.2 Implementation The implementation of the Student File Reader ADT does not require a data structure since it

does not store data but instead extracts data from an external source. The ADT has to be implemented to extract data based on the format in which the data is

stored. For this example, we are going to extract the data from a text file in which the recordsare listed one after the other.

The fields of the record are each stored on a separate line. The first line contains the idnumber, the second and third contain the first and last names, the fourth line contains

the classification code, and the grade point average follows on the fifth line. The following text block illustrates the format for a file containing two records:10015JohnSmith23.0110334JaneRoberts43.81Following program provides the implementation of the ADT for extracting the records from the

text file in the given format. The constructor simply initializes an instance of the class by creating two attributes, one to

store the name the text file and the other to store a reference to the file object after it's opened. The open() method is responsible for opening the input file using the name saved in the

constructor. The resulting file object is saved in the inputFile attribute so it can be used in the other

methods. After the records are extracted, the file is closed by calling the close() method.The studentfile.py module.

CODE EXPLANATION

class StudentFileReader :def __init__( self, inputSrc ):self._inputSrc = inputSrcself._inputFile = None

def open( self ):self._inputFile = open( self._inputSrc, "r" )def close( self ):self._inputFile.close()

Implementation of the StudentFileReaderADT using a text file as theinput source in which each field is stored ona separate lineCreate a new student reader instance.

Open a connection to the input file.

Close the connection to the input file.

Page 23: Data StructureStructure, List Based Implementation, Maps – Map Abstract Data Type (ADT), List Based Implementation, Multi-dimensional Arrays – Multi-Array Abstract Data Type (ADT),

14 Data Structure

self._inputFile = None.def fetchAll( self ):theRecords = list()student = self.fetchRecord()while student != None :theRecords.append( student )student = self.fetchRecord()

return theRecordsdef fetchRecord( self ):line = self._inputFile.readline()if line == "" :return Nonestudent = StudentRecord()student.idNum = int( line )student.firstName =self._inputFile.readline().rstrip()student.lastName =self._inputFile.readline().rstrip()student.classCode =int( self._inputFile.readline() )student.gpa = float( self._inputFile.readline() )return studentclass StudentRecord :def __init__( self ):self.idNum = 0self.firstName = Noneself.lastName = Noneself.classCode = 0self.gpa = 0.0

Extract all student records and store them ina list

Extract the next student record from the file.Read the first line of the record.

If there is another record, create a storageobject and fill it.

Storage class used for an individual studentrecord.

The fetchAll() method, returns a list of StudentRecord objects. This is done by repeatedly calling the fetchRecord() method. Thus, the actual extraction of a record from the text file is handled by the fetchRecord()

method. The Student File Reader ADT provides a framework that can be used to extract any type of

records from a text file. The only change required would be in the fetchRecord() method tocreate the appropriate storage object and to extract the data from the File in the given format.

Exercise Questions1. What is data types? List and explain various data types.2. What do you mean abstraction? Explain various types of abstractions.

Page 24: Data StructureStructure, List Based Implementation, Maps – Map Abstract Data Type (ADT), List Based Implementation, Multi-dimensional Arrays – Multi-Array Abstract Data Type (ADT),

15Abstract Data Types

3. Explain Abstract Data Type (ADT). Discuss its advantages.4. Discuss difference between procedural and data abstraction.5. Explain Date Abstract Data Type (ADT).6. Discuss various methods of Date Abstract Data Type (ADT).7. Explain preconditions and postconditions in detail.8. Explain Bag Abstract Data Type (ADT) .9. Discuss various operations on Bag Abstract Data Type (ADT) .

10. Explain the use of Iterator in python.