COSC066: Data Structure and Algorithms - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

COSC066: Data Structure and Algorithms

Description:

... the unit meanings and a name for easy ... Class name encapsulates them ... Each entry is a pair of (name, score). How are we going to implement ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 20
Provided by: GE96
Category:

less

Transcript and Presenter's Notes

Title: COSC066: Data Structure and Algorithms


1
COSC066 Data Structure and Algorithms
  • Elementary Data Structures
  • Arrays

2
Outline
  • Abstractions
  • Arrays

3
Abstraction and Computation
  • Abstraction
  • High level views of objects or functions and hide
    their low level details and implementations
  • Abstraction is an important tool used in
    computation to process the complexity
  • Procedure of abstraction
  • Glue a collection of data together as a single
    unit
  • Give the unit meanings and a name for easy
    reference and usage
  • Provide methods to manipulate the data

4
Examples of Abstraction
  • Example 1 basic level of abstraction
  • int b
  • For user, a int is a number between -231 and
    231-1 with operations like , -, , /
  • Underlying, a int is a bare bones 32-bit chunk
    of information

8/23/2009
CS066 Fall 2008
4
5
Standard Primitive Types
  • basic level of abstraction, most programming
    languages provide these primitives
  • Boolean values (boolean)
  • Characters (char)
  • 8-bit integers (byte)
  • 16-bit integers (short)
  • 32-bit integers (int)
  • 64-bit integers (long)
  • 32-bit floating-point numbers (float)
  • 64-bit floating-point numbers (double)
  • Operations
  • , -, , /
  • ,

6
Examples of Abstraction
  • Example 2 procedural abstraction
  • Math.abs(-2)
  • For user, it is going to return a number 2
  • Underlying, it consists of a number of
    operations
  • determine the input number is positive, zero, or
    negative
  • return the absolute value accordingly.

8/23/2009
CS066 Fall 2008
6
7
Examples of Abstractions
  • Example 3 -- User level abstraction
  • class Point
  • double x, y
  • Point()
  • x Math.random() y Math.random()
  • Point(double x, double y)
  • this.x x this.y y
  • double r() return Math.sqrt(xx yy)
  • double theta() return Math.atan2(y, x)
  • double distance(Point p)
  • double dx x - p.x, dy y - p.y
  • return Math.sqrt(dxdx dydy)
  • public String toString()
  • return "(" x "," y ")"

Data fields or class members
constructors
Methods
8
High Level Abstractions in Java
  • A high level abstract results in a Java class
  • Data fields(x and y)
  • Methods that manipulate data members (Point(x, y)
    and r())
  • Class name encapsulates them together (class
    Point)
  • ?This resulting class is a data type
  • A data type is defined as
  • A set of values and
  • A collection of operations on those values
  • We will discuss a number of abstract data types,
    where we separate the definitions of data types
    from implementations.

9
The Idea of Abstraction
  • Abstraction uses the idea of a black box
  • Blackbox hides the implementations
  • In this course, we will learn both sides of
    abstract data types
  • Definition or usages
  • Implementations

Input data
Output data
Data type (class)
10
Abstract Data Types
  • We are going to learn the following ADT
  • Stacks
  • Queues
  • Deque
  • Lists
  • Iterators
  • Trees

11
An Example of Stacks
Int size() Boolean isEmpty() E top() Void
push(E element) E pop()
  • A stack is known as LIFO (last-in, first-out)
    data structure
  • In stack 1, first in is yellow, then green, then
    blue, and last red.
  • If we want to access the green box in this stack,
    we need to first remove red (becomes 2), and then
    remove blue (becomes 3). Now, we can access green
    box

12
Building Blocks of Data Types
  • Abstract data types often deal with a large
    collection of data
  • A stack can have thousands of elements
  • A tree can have millions of nodes
  • We learn both the definition and implementation
    of data types
  • Understand the attributes and features
  • The data members and operations
  • Structure and implement the data type in
    programming
  • How to organize the data together?
  • What are the available data structures?

13
Elementary Data Structures
  • Arrays
  • Organizes objects in a fixed sequential fashion
  • Is more suitable for access than for manipulation
  • Linked lists
  • Organize objects in a logical sequential fashion
  • Is more suitable for manipulation than for access

14
Arrays
  • An example array int a10, array size or length
    10
  • When array a10 is declared, a memory space is
    allocated for this array, and the memory address
    of a0 is stored in variable a.
  • a0 20 a1 19 a9 11
  • Question What kind of operations are defined for
    data elements in an array?

a
15
Arrays
  • An array a collection of same-type data that
  • Are stored contiguously in memory
  • Are accessible by an index
  • Are bounded by a fixed array size
  • Primitive in Java and in most other languages

16
Arrays
  • Advantage
  • Quick access to any arbitrary item
  • Efficient computing
  • Disadvantage
  • Fixed capacity
  • Programmers responsibilities (causes of common
    programming mistakes)
  • Store something meaningful
  • Use proper indices (0ltiltarraysize)

8/23/2009
CS066 Fall 2008
16
17
Out of Bounds References
  • ai accesses the ith cell after the entry point
    of a
  • A valid index i of array a 0 lt i lt (a.length
    -1)
  • For example, a0, a1, , a9 access the
    elements of array a
  • Any index outside of this range is out of bounds
  • For example, a10 references to the memory
    address following array a

a
19
18
17
16
15
14
13
12
11
20
1
2
3
4
5
6
7
8
9
0
a10
18
Out of Bounds Errors in Java
  • c doesnt check bounds it is fine to access
    a10
  • ? beloved by hackers
  • Java will throw out an error condition
  • ArrayIndexOutOfBoundsException

19
Uses of Arrays a Video Game Example
  • For a video game, we are asked to store 10 high
    score entries. Each entry is a pair of (name,
    score). How are we going to implement this?
Write a Comment
User Comments (0)
About PowerShow.com