0% found this document useful (0 votes)
7 views

Lecture 01

Uploaded by

Danula Perera
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Lecture 01

Uploaded by

Danula Perera
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

IT 3007

Algorithms
Data Structures and

All rights Reserved © Kasun Madhusanka 10/9/2020


1
10/9/2020
Introduction to Data

All rights Reserved © Kasun Madhusanka


Structures
Kasun Madhusanka
2
10/9/2020
• Introduction to Data Structures

Content • ADTs
• Stacks
• Queues
• Lists

All rights Reserved © Kasun Madhusanka


• Trees
• Further on Stacks
• References

3
10/9/2020
What is a Data Structure?
• Data structure is a representation of data and the operations allowed on that
data.
• A data structure is a way to store and organize data in order to facilitate
the access and modifications.
• Data Structures are the method of representing of logical relationships

All rights Reserved © Kasun Madhusanka


between individual data elements related to the solution of a given problem.

4
10/9/2020
Basic Data Structures
Basic Data Structures

Linear Data Structures Non-Linear Data Structures

All rights Reserved © Kasun Madhusanka


Arrays Linked Lists Stacks Queues Trees Graphs Hash Tables

5
e
tre e
queu

k
stac
y

list
arra

Linked

All rights Reserved © Kasun Madhusanka 10/9/2020


6
10/9/2020
Selection of a Data Structure
❑The choice of data model depends on :

▪ It must be rich enough in structure to represent the relationship


between data elements

▪ The structure should be simple enough that one can effectively

All rights Reserved © Kasun Madhusanka


process the data when necessary

7
10/9/2020
Types of Data Structures
❑ Linear: In Linear data structure, values are arranged in a linear
fashion.

▪ Array: Fixed-size

All rights Reserved © Kasun Madhusanka


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

8
10/9/2020
Types of Data Structures
❑ 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

All rights Reserved © Kasun Madhusanka


Tree: Data is organized in branches.

▪ Graph: A more general branching structure, with less strict connection conditions than for a
tree

9
10/9/2020
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

All rights Reserved © Kasun Madhusanka


different types are grouped and stored.

▪ Structures

▪ Classes

10
10/9/2020
Abstract Data Types and Data Structures
❑ Definition:
▪ Abstract Data Types (ADTs) stores data and allow various operations on the
data to access and change it.
▪ A mathematical model, together with various operations defined on the model
▪ An ADT is a collection of data and associated operations for manipulating
that data
❑ Data Structures

All rights Reserved © Kasun Madhusanka


▪ Physical implementation of an ADT
▪ data structures used in implementations are provided in a language (primitive
or built-in) or are built from the language constructs (user-defined)
▪ Each operation associated with the ADT is implemented by one or more
subroutines in the implementation

11
10/9/2020
Abstract Data Types
▪ ADTs support abstraction, encapsulation, and information
hiding.

▪ Abstraction is the structuring of a problem into well-defined


entities by defining their data and operations.

All rights Reserved © Kasun Madhusanka


▪ The principle of hiding the used data structure and to only
provide a well-defined interface is known as encapsulation.

12
10/9/2020
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

All rights Reserved © Kasun Madhusanka


● Many, many more possibilities
○ Is the collection empty?
○ make the collection empty
○ give me a subset of the collection

13
“No single data structure works well
for all purposes, and so it is
important to know the strengths
and limitations of several of them”

14
10/9/2020
Stacks
• Collection with access only to the last element inserted
• Last in first out
• insert/push
• remove/pop
• top/peek

All rights Reserved © Kasun Madhusanka


Data4 Top

• make empty
Data3

Data2

Data1

15
10/9/2020
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

All rights Reserved © Kasun Madhusanka


Front Back

Data1 Data2 Data3 Data4

16
10/9/2020
List
• A Flexible structure, because can grow and shrink on demand.
Elements can be:

▪ Inserted
▪ Accessed
▪ Deleted

All rights Reserved © Kasun Madhusanka


At any position

last
first

17
10/9/2020
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

All rights Reserved © Kasun Madhusanka


Leaf Nodes

18
Stacks
❑ A stack is a list in which insertion and deletion take place at the same
end
▪ This end is called top
▪ The other end is called bottom

AliSaivi BSCS 0340-7534641


❑ Stacks are known as LIFO (Last In, First Out) lists.
▪ The last element inserted will be the first to be retrieved
e.g. a stack of Plates, books, boxes etc.
Insertion and deletion on stack

AliSaivi BSCS 0340-7534641


Stack Applications
❑ “Back” button of Web Browser
▪ History of visited web pages is pushed onto the stack and popped when
“back” button is clicked

❑ “Undo” functionality of a text editor

❑ Reversing the order of elements in an array

AliSaivi BSCS 0340-7534641


❑ Saving local variables when one function calls another, and this
one calls another, and so on.
Operation On Stack
▪ Creating a stack
▪ Checking stack---- either empty or full
▪ Insert (PUSH) an element in the stack
▪ Delete (POP) an element from the stack
▪ Access the top element
▪ Display the elements of stack

AliSaivi BSCS 0340-7534641


Push and Pop
❑ Primary operations: Push and Pop

▪ Push - Add an element to the top of the stack.

▪ Pop - Remove the element at the top of the stack.

AliSaivi BSCS 0340-7534641


Stack-Related Terms
❑ Top
▪ A pointer that points the top element in the stack.

❑ Stack Underflow
▪ When there is no element in the stack, the status of stack is known as stack
underflow.

AliSaivi BSCS 0340-7534641


❑ Stack Overflow
▪ When the stack contains equal number of elements as per its capacity and no
more elements can be added, the status of stack is known as stack overflow
Stack Implementation
❑ Implementation can be done in two ways
▪ Static implementation
▪ Dynamic Implementation

❑ Static Implementation
▪ Stacks have fixed size, and are implemented as arrays
▪ It is also inefficient for utilization of memory

AliSaivi BSCS 0340-7534641


❑ Dynamic Implementation
▪ Stacks grow in size as needed, and implemented as linked lists
▪ Dynamic Implementation is done through pointers
▪ The memory is efficiently utilized with Dynamic Implementations
No Homework Today ! :)
Homework

All rights Reserved © Kasun Madhusanka 10/9/2020


26
10/9/2020
References
• Data Structure and Algorithms - Mohsin Mushtaq, University of the Punjab, Lahore,
Pakistan

Textbooks
• Introduction to Data Structures in C by Ashok N. Kamthane
• Data Structures and Algorithms by A. V. Aho, J. E. Hopcroft, J. D. Ullman

All rights Reserved © Kasun Madhusanka


• Data Structures Using C and C++ by Y. Langsam, M. J. Augenstein, A. M. Tenenbaum
• Algorithms in C++ by Robert Sedgewick

27
10/9/2020
“The secret of getting ahead is getting
started. The secret of getting started is
breaking your complex overwhelming
tasks into small manageable tasks, and

All rights Reserved © Kasun Madhusanka


then starting on the first one.”
- Mark Twain

28

You might also like