data structures course 1

Upload: dan-heisenberg

Post on 03-Apr-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/28/2019 Data structures course 1

    1/26

    Data Structures

    [email protected]

    mailto:[email protected]:[email protected]
  • 7/28/2019 Data structures course 1

    2/26

    Organizational

    Course objectives: present main data structures,improve programming.

    Final grade = sum of all: 1 p appreciation, 4 p

    homework, 2 p - laboratory test, 3 p - problemimplementation, 2 p - final exam

    if you have >10 p the final grade is 10, if you have

  • 7/28/2019 Data structures course 1

    3/26

    Important

    Academic honesty: cheating leads to failing classand reporting.

    OK/encouraged: speak up in class. Two-way, ratherthan one-way communication. Request: be concise,to the point, respect time spent together in class.

    Disclaimer: I can make mistakes/be wrong. Let meknow (in person, email) how I can improve things.

  • 7/28/2019 Data structures course 1

    4/26

    Further reference material

    Introduction to Algorithms, Second Edition by: Thomas H.Cormen, Charles E. Leiserson, Ronald L. Rivest, CliffordStein http://mitpress.mit.edu/algorithms/

    Adam Drozdek Data structures and algorithms in C++,third edition, Thomson Course Technology, 2005.

    Michael T. Goodrich, Roberto Tamassia, David M. MountData Structures and Algorithms in C++.Second Edition

    Gabriel Istrate last year lectures:http://gabrielistrate.weebly.com/pentru-studenti.html

    other prefered Data Structures books

    http://mitpress.mit.edu/algorithms/http://mitpress.mit.edu/algorithms/
  • 7/28/2019 Data structures course 1

    5/26

    The plan

    Linked Lists. Singly linked lists, Doubly linkedlists. Stacks, queues, dequeues.

    Binary Search Trees AVL trees

    .

    Heaps. Red-black trees. Hash tables. Augmenting Data Structures

    .

  • 7/28/2019 Data structures course 1

    6/26

    Why data structures ?

    Data structures: a way to define data types we want.

    Many tasks can be specified in terms of input and output.

    Input/output: data. Divided into types. Predefined (in C: int,long, short, double, etc), or user defined.

    Data types can involve packaging multiple elements of

    simpler types. Examples: complex numbers (real,

    imaginary), enums.

    Performance requirements.

    Operations abstracted from requirements.

    Frequent operations should be fast.

  • 7/28/2019 Data structures course 1

    7/26

    Examples

    Problem 1

    Read a stream of words from an input. Maintainset of words, withfrequencies. Want: quickly test if we have seen a word. Quicklyupdate frequency.

    Problem 2

    Evaluating postfix arithmetic expressions

    Problem 3

    Computation of shortest paths from a Weighted Graph.

  • 7/28/2019 Data structures course 1

    8/26

    Example (operations from requirements)

    1 2 3 6 7 4 8 5 10 14 13 11 12...

    TCP: basis for much of Internet traffic.

    Data requirement: We need to buffera packetthat is out-of order.

    We need to pop elements that become in-order.

    We need to test emptyness of buffer.

    We need to produce first missing element

    Operation performance O(1)?.

  • 7/28/2019 Data structures course 1

    9/26

    What are data structures after all ?

    (Abstract) sets ofdata ...

    E.g. complex numbers: two floats.

    ... together with operationsone can perform on the

    data ...

    E.g. integer + (addition), - (subtraction), (multiplication)

    ... and performance guarantees.

    How to precisely implement operations is not a part ofdata structure specification. Concepts, not code.

  • 7/28/2019 Data structures course 1

    10/26

    Data types

    All DS that share a common structure and expose thesame set of operations.

    Predefined data types: array, structures, files.

    Scalar data type: ordering relation exists among elements. More complicated, dynamic DS: Lists, circular lists, trees,

    hash tables, graphs.

    Standard template library (STL): library of container

    classes, algorithms, and iterators; provides many of thebasic algorithms and data structures of computer science

  • 7/28/2019 Data structures course 1

    11/26

    E.g. Standard data types: integers

    Available on most hardware. Often subject tostandardization.

    Integer data type: subset of integers.

    Operations: exact. In case of overflow computationstopped.

    Unsigned/signed integers, twos complement.Value Unsigned Signed Two's complement

    -128 - - 10000000-127 - 11111111 10000001

    -1 - 10000001 11111111

    0 00000000 00000000 00000000

    127 01111111 01111111 01111111

    128 10000000 - -

    255 11111111 - -

  • 7/28/2019 Data structures course 1

    12/26

    Real numbers: data types

    floating point representation.

    simple (32 bits), double (64 bits), extended precision (80bits ).

    representation: three parts. sign, exponent,significand(mantissa).

    mantissa: normalized (by adjusting exponent) so that mostsignificant bit is 1.

    e.g. double: 64 bits, one for the sign, 52 bits for mantissa(normalized), 11 bits exponent (base 2).

    arithmetic operations: +,-,*, /

    also % and bitwise operations.

  • 7/28/2019 Data structures course 1

    13/26

    Array data type/Vector

    Ensures random access to its elements.

    Composed of objects of the same type.

    int myarray[10]; One dimensional arrays.

    Multidimensional arrays.

    type name[lim1]...[limn];

    alternative in C++: vector.

  • 7/28/2019 Data structures course 1

    14/26

    Example using vectorclass

    #include

    using namespace std;

    int main(){

    static const int SIZE = 10000;

    vector arr( SIZE );

    ...

    }

  • 7/28/2019 Data structures course 1

    15/26

    some OOP

    OOP: revolves around concept ofobject

    Objects: created using class definition

    Combining of data and related operations iscalled encapsulation.

    Allow us to conceal details of object operationsfrom other objects (information-hiding).

  • 7/28/2019 Data structures course 1

    16/26

    Classes

    class C{

    public:

    ... (functions and data members)

    private:

    ... (functions and data members)

    }

  • 7/28/2019 Data structures course 1

    17/26

    Classes: Constructors/Destructors

    class C{

    public:

    C(); // constructor

    C(const C &); // copy constructor

    ~C(); // destructor

    ...

    }

  • 7/28/2019 Data structures course 1

    18/26

    Operations: message passing to objects

    class person{public:

    person(char *s="", int i=0, double d=1.0){strcpy(name,s);age = i;

    credit = d;}void memberFunction1(){ ...}void memberFunction2(int i, char *s = "unknown") { }

    protected:

    char name[20];int age;double credit;

    };

    C object1("object1", 100,2000), object2("object2"), object3;

    object1.memberFunction1();object1.memberFunction2(123);

  • 7/28/2019 Data structures course 1

    19/26

    Namespaces & Streams

    using namespace std;

    string a;

    cout a;

    or

    std::cout

  • 7/28/2019 Data structures course 1

    20/26

    Pointers

    Variables that hold addresses of other variables.

    int i=15, j, *p, *q;

    Dynamic memory allocation:

    p = new int; p = (int *)malloc(sizeof(int));

    Assignment: *p=20;

    Deallocation: delete p; free(p);

    Delete reference: upon deallocation should assign p = 0;

  • 7/28/2019 Data structures course 1

    21/26

    Pointers and arrays

    int a[5],*p;

    for(sum=a[0],i=1;i

  • 7/28/2019 Data structures course 1

    22/26

    Pointers and reference variables

    int n =5, *p = &n, &r =n;

    ris a reference variable. Must be initialized in definitionas reference to a particular variable.

    reference: different name for/constant pointer to variable.cout

  • 7/28/2019 Data structures course 1

    23/26

    Generic Classes/Functions: Motivation

    swap two integers

    void swapint(int *a, int *b){

    int temp = *a;

    *a = *b;

    *b = temp;

    } swap two reals

    void swapfloat(float *a, float *b){float temp = *a;

    *a = *b;

    *b = temp;

    }

  • 7/28/2019 Data structures course 1

    24/26

    Generic Functions

    template (genType &el1, genType &el2){

    genType tmp = el1;

    el1 = el2;

    el2 = tmp;

    }

    more general code

    reusing code

  • 7/28/2019 Data structures course 1

    25/26

    Generic Classes

    template class genClass{

    genType storage[50];

    ...

    }

    template class genClass2{

    genType storage[size];

    ...

    }

    genClass intObject1;

    genClass2 intObject2;

    E.g. see map from C++

  • 7/28/2019 Data structures course 1

    26/26

    Conclusions

    some syntaxes of C++

    For more details: read, practice!

    Any questions?