Docsity

Data Structures and Algorithms: Lecture 01 - Abstract Data Type, Summaries of Data Structures and Algorithms

Partial preview of the text

Download Data Structures and Algorithms: Lecture 01 - Abstract Data Type and more Summaries Data Structures and Algorithms in PDF only on Docsity!

Data Structures and Algorithms

Lecture 01: Abstract Data Type

• What is Data Abstraction? What is ADT

• Model for an Abstract Data Type

• ADT Example

• ADT vs Object-Oriented Programming

Contents

  • Abstract Data Type (ADT)
    • Defines a particular data structure in terms of data and operations
    • Offers an interface of the objects (instances of an ADT)
  • An ADT consists of
    • Declaration of data
    • Declaration of operations
    • Encapsulation of data and operations : data is hidden from user and can be manipulated only by means of operations What is an Abstract Data Type?
  • You don't need to know how much about floating point arithmetic works to use float - Indeed, the details can vary depending on processor, even virtual coprocessor - But the compiler hides all the details from you--some numeric ADTs are built-in - All you need to know is the syntax and meaning of operators, +, - , *, /, etc.
  • Hiding the details of implementation is called encapsulation ( data hiding ) ADT example: Floating point numbers
  • An ADT is a formal description, not code; independent of any programming language - Why is code independence a good idea?
  • Promotes design by contract
    • Specify responsibilities of suppliers and clients explicitly, so they can be enforced, if necessary Formal, language-independent ADTs
  • Implementaion of an Abstract Data Type (ADT)
    • Hidden from the user
    • Same ADT may be implemented in different ways in different languages
    • Some languages offer built-in ADTs and/or features to be used to implement ADTs (user-define types)
  • ADTs support modular design which is very important in software development ADT Implementation
  • Manufacturer (who creates ADT) benefits:
    • easy to modify, maintain
    • profitable
    • reusable
  • Client (who uses ADT) benefits:
    • simple to use, understand
    • familiar
    • cheap
    • component–based Benefits
  • A list contains elements of same type arranged in sequential order and following operations can be performed on the list - get() – Return an element from the list at any given position. - insert() – Insert an element at any position of the list. - remove() – Remove the first occurrence of any element from a non- empty list. - removeAt() – Remove the element at a specified location from a non- empty list. - replace() – Replace an element at any position by another element. - size() – Return the number of elements in the list. - isEmpty() – Return true if the list is empty, otherwise return false. - isFull() – Return true if the list is full, otherwise return false. ADT example: LIST ADT

-. ADT in VDM example: Boolean type

  • ADTs are not a part of a particular programming language
  • Rather they are implemented by a programmer to solve a particular problem or some class of problems
  • In OOP, an ADT can be easily modeled as a class.
    • An instance as an object
    • Data of ADT as properties or fields of a class
    • Operations as methods
  • ADT ≠ OOP
  • Classes in OOP offers more features than ADTs : Inheritance (Superclass-Subclass), Polymorphisms, etc. ADT vs Object-Oriented Programming
  • The operations supported by our Bag ADT:
    • add(item): add item to the Bag
    • remove(item): remove one occurrence of item (if any) from the Bag
    • contains(item): check if item is in the Bag
    • numItems(): get the number of items in the Bag
    • grab(): get an item at random, without removing it
    • toArray(): get an array containing the current contents of the bag ADT in Java: A Bag
  • In Java, we can use interface to specify an ADT: ADT in Java: A Bag

The End