Week 01 - Introdutcion To Data Structures and ADT

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

CS-250 Data Structure and Algorithms

Dr. Muhammad Kamran Khan


Assistant Professor (Comp. Sc)
[email protected]

Department of Electrical Engineering


Military College of Signals
National University of Sciences and Technology (NUST
1
Computer Science Department
Introduction to Data Structure
and
Abstract Data Types (ADT)

(Week 01)

2
Computer Science Department
What is Data?
 Data
• “facts and statistics collected together for reference or
analysis.” – Google

 Types of data
• Textual, numeric, audio, video

 Importance of data?
• primary purpose of computer programs is to store
and retrieve information, usually as fast as possible

3
Computer Science Department
Computer Program
• To exactly know, what is data structure? We must know:
-What is a computer program?
Computer Program
Solution
Problem

Process
Input (Algorithm) Output
(DS) (DS)
(Algorithm)

Data Structures + Algorithms = Programs


4
Computer Science Department
What is a Data Structure?
- A particular way of organizing data in a
computer so that it can be used effectively
E.g. we can store a list of items having the
same data-type using the array data
structure

- Method of representing logical relationships


between individual data elements related to
the solution of a given problem
Resource constraints  space, time

5
Computer Science Department
Basic Data Structures

Basic
Data Structures
Linear Non-Linear
Data Structures Data Structures
Linked Hash
Arrays Stacks Queues Trees Graphs
Lists Tables

6
Computer Science Department
array

Linked list

queue
tree stack
7
Computer Science Department
Data Structures Operations
 Traversing: Accessing each record exactly once so that each data
items in the record is traversed (or visited)

 Searching: Finding the location of the record with the given key
value or finding the location of all records which satisfy one or more
conditions

 Insertion: Adding a new record to the structure

 Deletion: Removing a record from the structure

 Sorting: Arrange the records in a logical order

 Merging: Combining records from two or more files or data


structures into one
8
Computer Science Department
Types of Data Structure
• Linear: Values are arranged in linear fashion.
– Array: Fixed-size
– Linked-list: Variable-size
– Stack: Add to top and remove from top
– Queue: Add to back and remove from front
– Priority queue: Add anywhere, remove the highest
priority

9
Computer Science Department
Types of Data Structure
• Non-Linear: The data values in this structure are not
arranged in order.
– Hash tables: Unordered lists
which use a ‘hash function’ to insert and search

– Tree: Data is organized in branches.

– Graph: A more general branching structure, with less strict


connection conditions than for a tree

10
Computer Science Department
Type of Data Structures
• Homogenous: In this type of data structures, values of
the same types of data are stored.
– Array

• Non-Homogenous: In this type of data structures, data


values of different types are grouped and stored.
– Structures
– Classes

11
Computer Science Department
Selection of Data Structure
- Each data structure has costs and benefits
(Rarely is one data structure better than another in
all situations)
- A data structure requires:
1: space for each data item it stores
2: time to perform each basic operation
3: programming effort
- Each problem has constraints on available time and
space (only after a careful analysis of problem
characteristics can we know the best data structure for
the task) 12
Computer Science Department
Selection of Data Structure

No single data structure works well for all purposes, and so it is


important to know the strengths and limitations of several of them

13
Computer Science Department
Characteristics of Data Structure

14
Computer Science Department
Characteristics of Data Structure

15
Computer Science Department
Characteristics of Data Structure

“I will, in fact, claim that the difference between a bad programmer and a
good one is whether he considers his code or his data structures more
important. Bad programmers worry about the code. Good programmers worry
about data structures and their relationships.”

– Linus Trovalds, 2006

16
Computer Science Department
Abstract Data Type (ADT) and
Data Structures
Abstract Data Type Data Structures
• ADT stores data and allow various • Physical implementation of an
operations on the data to access and ADT
change it • Data structures used in
• A mathematical model, together with implementations are provided in
various operations defined on the a language (primitive or built-in)
model or are built from the language
• An ADT is a collection of data and constructs (user-defined)
associated operations for • Each operation associated with
manipulating that data the ADT is implemented by one or
more subroutines in the
implementation

17
Computer Science Department
Abstract Data Type
• ADT is a way of looking at a data structure focusing on
what it does and ignoring how it does its job
• E.g. A stack is an ADT supporting push, pop and
isEmpty operations
• ADTs support abstraction, encapsulation, and
information hiding.
• Abstraction is the structuring of a problem into well-
defined entities by defining their data and operations.
• The principle of hiding the used data structure and to
only provide a well-defined interface is known as
encapsulation.
18
Computer Science Department
The Core Operations of ADT
• Every Collection ADT should provide a way to:
– add an item
– remove an item
– find, retrieve, or access an item

• Many, many more possibilities


– is the collection empty
– make the collection empty
– give me a sub set of the collection

19
Computer Science Department
Some frequently used DS.

20
Computer Science Department
Stacks
• Collection with access only to the last
element inserted
• Last in first out Data4 Top

• insert/push Data3

• remove/pop Data2

• top Data1

• make empty

21
Computer Science Department
Queues
• Collection with access only to the item that
has been present the longest
• Last in last out or first in first out
• enqueue, dequeue, front
• priority queues and dequeue
Front Back

Data1 Data2 Data3 Data4


22
Computer Science Department
List
• A Flexible structure, because can grow and
shrink on demand.
Elements can be:
 Inserted
 Accessed
 Deleted
At any position
last
first

23
Computer Science Department
Tree
• A Tree is a collection of elements called nodes.
• One of the node is distinguished as a root, along with a
relation (“parenthood”) that places a hierarchical
structure on the nodes.
Root

24
Computer Science Department

You might also like