Docsity

Data Structures and Abstract Data Types: Lecture Notes for CSCE310J, Study notes of Algorithms and Programming

Toggle sidebar
bg1
Page 1
1
http://www.cse.unl.edu/~goddard/Courses/CSCE310J
Abstract Data Types
and Basic Data Structures
Dr. Steve Goddard
CSCE 310J: Data Structu res & Algorithms
2
IGiving credit where credit is due:
•Most of slides for this lecture are based on slides created
by Dr. Ben Choi, Louisiana Technical University.
•I have modi fied them and adde d new slides
CSCE 310J: Data Structu res & Algorithms
3
Abstract Data Type
IAbstract Data Type
•Data Structure declaration
•Operations performed on the data structure
–e.g., create, destroy, or manipulate
–These are logical operations that are independent of the actual
implementation!
IProvides data encapsulation (information hiding)
IAn ADT is implemented as a Class in languages such as
C++ and Java
IAlgorithms can be designed, specified, and proven correct
using the logical properties of ADTs
IPerformance analysis depends on the implementation!
4
ADT Specification
IThe specification of an ADT describes how the
operations (functions, proce dures, or methods) behave
in terms of Inputs and Outputs
IA specification of an operation consists of:
•Calling prototype
•Preconditions
•Postconditions
IThe calling prototype includes
•name of the operation
•parameters and their types
•return value and its types
IThe preconditions are statements
•assumed to be true when the operation is called.
IThe postconditions are statements
•assumed to be true when the operation returns.
5
Operations for ADT
IConstructors
•create a new object and return a reference to it
IAccess functions
•return information about an object, but do not modify it
IManipulat ion procedures
•modify an object, but do not return information
IDestructors
•deallocate an object
6
More on ADTs
IState of an object
•current value of its data
ISome books claim that constructors and manipulation
procedures should be describe d in terms of Acces s
functions
•Maybe…
IRecursive ADT
•if any of its access functions returns the same class as the ADT
pf3
pf4

Partial preview of the text

Download Data Structures and Abstract Data Types: Lecture Notes for CSCE310J and more Study notes Algorithms and Programming in PDF only on Docsity!

1

http://www.cse.unl.edu/~goddard/Courses/CSCE310J

Abstract Data Types

and Basic Data Structures

Dr. Steve Goddard

[email protected]

CSCE 310J: Data Structures & Algorithms

2

I Giving credit where credit is due:

  • Most of slides for this lecture are based on slides created

by Dr. Ben Choi, Louisiana Technical University.

  • I have modified them and added new slides

CSCE 310J: Data Structures & Algorithms

3

Abstract Data Type

I Abstract Data Type

  • Data Structure declaration
  • Operations performed on the data structure
    • e.g., create, destroy, or manipulate
    • These are logical operations that are independent of the actual implementation!

I Provides data encapsulation (information hiding)

I An ADT is implemented as a Class in languages such as

C++ and Java

I Algorithms can be designed, specified, and proven correct

using the logical properties of ADTs

I Performance analysis depends on the implementation!

4

ADT Specification

I The specification of an ADT describes how the

operations (functions, procedures, or methods) behave

in terms of Inputs and Outputs

I A specification of an operation consists of:

  • Calling prototype
  • Preconditions
  • Postconditions

I The calling prototype includes

  • name of the operation
  • parameters and their types
  • return value and its types

I The preconditions are statements

  • assumed to be true when the operation is called.

I The postconditions are statements

  • assumed to be true when the operation returns.

5

Operations for ADT

I Constructors

  • create a new object and return a reference to it

I Access functions

  • return information about an object, but do not modify it

I Manipulation procedures

  • modify an object, but do not return information

I Destructors

  • deallocate an object

6

More on ADTs

I State of an object

  • current value of its data

I Some books claim that constructors and manipulation

procedures should be described in terms of Access

functions

  • Maybe…

I Recursive ADT

  • if any of its access functions returns the same class as the ADT

7

ADT Design for Lists

IntList nil //constant denoting the empty list.

IntList constructList(int newElement, IntList oldList) Precondition: None. Postconditions: If newList = constructList(newElement, oldList) then

  1. newList refers to a newly created list object;
  2. newList ≠ nil;
  3. first(newList) = newElement;
  4. rest(newList) = oldList

int first(IntList aList) // access function Precondition: aList ≠ nil Postcondition: if element = first(aList) then

  1. element ≠ nil

IntList rest(IntList aList) // access fcn Precondition: aList ≠ nil

8

Binary Tree

I A binary tree T is a set of elements, called nodes,

that is empty or satisfies:

  1. There is a distinguished node r called the root
  2. The remaining nodes are divided into two disjoint subsets, L and R, each of which is a binary tree. - L is called the left subtree of T and R is called the right subtree of T.

I There are at most 2 d^ nodes at depth d of a binary tree.

I A binary tree with n nodes has height at least

Ceiling[lg(n+1)] – 1.

I A binary tree with height h has at most 2 h+1^ –1 nodes

9

Binary Tree Example

I What node is the root? I What is the depth of each node?

  • How many nodes are there at that depth?
  • Is this the maximum number of nodes at that depth? I How many internal nodes? I How many leaves?

I What is the value of n? I What is the height of the tree? I What is the minimum (maximum) height of a binary tree with this many nodes? I What is the height of each subtree? I What are the preorder, inorder, and postorder traversals?

10

Stacks

I A stack is a linear structure in which insertions and

deletions are always made at one end, called the top.

I This updating policy is called last in, first out (LIFO).

I Operations:

  • Stack create()
  • boolean isEmpty(Stack s )
  • Object top(Stack s ),
  • void push(Stack s , Object e ),
  • void pop(Stack s)

11

Queue

I A queue is a linear structure in which

  • all insertions are done at one end, called the rear or back, and
  • all deletions are done at the other end, called the front.

I This updating policy is called first in, first out (FIFO).

12

Priority Queue

I A priority queue is similar to a FIFO queue but different…

  • element order is related to each element’s priority, rather than its chronological arrival time.

I “As each element is inserted into a priority queue,

conceptually it is inserted in order of its priority.”

I The one element that can be inspected and removed is the

most important element currently in the priority queue.

  • a cost viewpoint: the smallest priority
  • a profit viewpoint: the largest priority

I Priority queue operations are not in Θ(1). Their complexity

varies depending on the implementation, as we shall see.

19

Union-Find ADT for Disjoint Sets

I Through a Union operation, two (disjoint) sets can be

combined.

  • (to insure the disjoint property of all existing sets, the original two sets are removed and the new set is added)
  • Let the set ids of the original two sets be, s and t , s ≠ t
  • Then, the new set has a unique set id that is neither s nor t.

I Through a Find operation, the current set id of an element

can be retrieved.

I Often elements are integers and the set id is some

particular element in the set, called the leader, as in the

example in the book

20

Union-Find ADT Example

Notice how this ADT differs from the book!

I unionFind create(int n)

  • // create a set of n singleton disjoint sets {{1},{2},{3},…,{n}}

I setId find(UnionFind sets, int element)

  • // return the set id for element

I void makeSet(unionFind sets, int element)

  • //union one singleton set {e} (e not already in the sets) // into exiting sets

I void union(unionFind sets, setId s, setId t)

  • // s ≠ t
  • // a new set is created by union of set [s] and set [t]
  • // the new set id is either s or t, in some cases min(s, t)

21

Dictionary ADT

I A dictionary is a general associative storage structure.

I Items in a dictionary

  • have an identifier, and
  • associated information that needs to be stored and retrieved.

I No order is implied for identifiers in a dictionary ADT

I The Dictionary ADT is useful in dynamic

programming, which is covered later in the semester.