Docsity

Data Structures: Abstract Data Types and Stack, Slides of Data Structures and Algorithms

Partial preview of the text

Download Data Structures: Abstract Data Types and Stack and more Slides Data Structures and Algorithms in PDF only on Docsity!

Data Structures

Exam: 70%

Midterm: Magen 20%

HomeWork: 10%

Book:

Find x :

Lists, Queues, Stacks, Arrays Binary Search Trees Graphs Tries and Suffix Trees Heaps Sorting Hashing Union-Find Lowest Common Ansectors

“A data structure is a particular way of storing and

organizing data in a computer to support fast

queries, maybe fast updates, using little space.”

Data Structures

Lists

Insert b at position i :

Delete the item at position i :

[ a 0 a 1 a 2 a 3 … ai -1 ai ai +1 … an -2 an -1 ]

b

Retrieve the item at position i :

Lists

Insert b at position i :

Delete the item at position i :

[ a 0 a 1 a 2 a 3 … ai -1 ai ai +1 … an -2 an -1 ]

Retrieve the item at position i :

Abstract Data Types (ADT)

Stacks, Queues, Dequeues

Stacks – Implement only: Insert-Last( L , b ), Retrieve-Last( L ), Delete-Last( L ) Also known as: Push( L , b ), Top( L ), Pop( L )

Last In First Out (LIFO)

Queues – Implement only:

Insert-Last( L , b ), Retrieve-First( L ), Delete-First( L )

First In First Out (FIFO)

Basic Definitions

  • A data structure = operations on the data
  • An implementation of a data structure = procedures implementing these operations

The Data Structure “Stack”

  • Create(S) = returns empty stack
  • Push(S,x) = insert element with value x to top of stack S
  • Top(S) = returns element in the top of stack S
  • Pop(S) = removes element in the top of stack S
  • IsEmpty(S) = returns TRUE iff stack S is empty
  • Some rules the Data Structure obeys:
    • Top(S) and Pop(S) can only be done on a nonempty stack S
    • Right after Create(S) the call IsEmpty(S) returns TRUE
    • Doing Push and then Pop doesn’t change stack

Data Structures

  • User knows the operations, not their implementation
  • Implementation can change over time
  • The quality of the implementation:
    • Time complexity
    • Space complexity
    • Simplicity of programming
  • The running time of an algorithm on input x is measured by the number of machine operations needed to execute it as a function of |x| = n
  • The worst-case running time is worst over all possible inputs of length |x| = n

Time complexity – Worst case, Big O

  • The running time of an algorithm on input x is measured by the number of machine operations needed to execute it as a function of |x| = n
  • The worst-case running time is worst over all possible inputs of length |x| = n
  • f(n)=O(g(n)) iff there are constants c, n 0 such that for

every n > n 0 we have f(n) < c g(n).

Time complexity – Worst case, Big O

Time complexity – constants

Lets examine the time complexity of

lists

Implementing lists using arrays

L

array maxlen length

a 0 a 1 … an− 1

n

M

M

n

We need to know the maximal length M in advance.

Retrieve( L , i ) (of any element) takes O(1) time

Insert-Last( L , b ) and Delete-Last( L ) take O(1) time

Stack operations in O(1) time

L

array maxlen length

a 0 a 1 … an− 1

n

M

M

n

We need to know the maximal length M in advance.

Retrieve( L , i ) (of any element) takes O(1) time

Insert-Last( L , b ) and Delete-Last( L ) take O(1) time

Insert( L , i , b ) and Delete( L , i ) take O( n − i +1) time

Implementing lists using arrays

Delete-Last very

similar

L

array maxlen length (^) n

M

M

0 1 n− 1^ n

Implementing lists using arrays