Chapter 3 data structure introductionn

  • Upload
    gaby

  • View
    219

  • Download
    0

Embed Size (px)

Citation preview

  • 8/18/2019 Chapter 3 data structure introductionn

    1/75

    McGraw-Hill   ©  The McGraw-Hill Companies, Inc., 2000Dr. Fares Saab

    Data Structures - CSC313C H A P T E R 3 – A B S T R A C T D A T A T Y P E S ( A D T )

  • 8/18/2019 Chapter 3 data structure introductionn

    2/75

      Define the concept of an abstract data type (ADT).

      Define a stack, the basic operations on stacks, their applications and how

      they can be implemented.

      Define a queue, the basic operations on queues, their applications and how

      they can be implemented.

      Define a eneral linear list, the basic operations on lists, their applications

      and how they can be implemented.

      Define a eneral tree and its application.

      Define a binary tree!a special kind of tree!and its applications.

      Define a binary search tree ("ST) and its applications.

      Define a raph and its applications.

    #b$ecti%esAfter studyin this chapter, the student should be able to&

    Dr. Fares Saab

  • 8/18/2019 Chapter 3 data structure introductionn

    3/75

      BACKGROUND

    Problem solving with a computer means processing

    data. To process data, we need to define the data type

    and the operation to be performed on the data. The

    definition of the data type and the definition of theoperation to be applied to the data is part of the idea

     behind an abstract data type (ADT) —to hide how the

    operation is performed on the data. In other words, the

    user of an ADT needs only to know that a set ofoperations are available for the data type, but does not

    need to know how they are applied.

    Dr. Fares Saab

  • 8/18/2019 Chapter 3 data structure introductionn

    4/75

    Simple ADTs

    any programming languages already define some simple

    ADTs as integral parts of the language. !or e"ample, the #

    language defines a simple ADT as an integer. The type of

    this ADT is an integer with predefined ranges. # also defines

    several operations that can be applied to this data type

    $addition, subtraction, multiplication, division and so on%. #e"plicitly defines these operations on integers and what we

    e"pect as the results. A programmer who writes a # program

    to add two integers should know about the integer ADT and

    the operations that can be applied to it.

    Dr. Fares Saab

  • 8/18/2019 Chapter 3 data structure introductionn

    5/75

    'omple ADTs

    Although several simple ADTs, such as integer, real,

    character, pointer and so on, have been implemented and are

    available for use in most languages, many useful comple"

    ADTs are not. As we will see in this chapter, we need a list

    ADT, a stack ADT, a &ueue ADT and so on. To be efficient,

    these ADTs should be created and stored in the library of thecomputer to be used.

    The concept of abstraction means&

    . *e know what a data type can do.

    +. ow it is done is hidden.

     i

    Dr. Fares Saab

  • 8/18/2019 Chapter 3 data structure introductionn

    6/75

    Definition

    'et us now define an ADT. An abstract data type is a data

    type packaged with the operations that are meaningful for the

    data type. (e then encapsulate the data and the operations on

    the data and hide them from the user.

    Abstract data type&

    . Definition of data.

    +. Definition of operations.-. ncapsulation of data and operation.

     i

    Dr. Fares Saab

  • 8/18/2019 Chapter 3 data structure introductionn

    7/75

    /odel for an abstract data type

    The ADT model is shown in !igure ).*. Inside the ADT are

    two different parts of the model+ data structure and

    operations $public and private%.

    Fiure -. The model for an ADTDr. Fares Saab

  • 8/18/2019 Chapter 3 data structure introductionn

    8/75

    0mplementation

    #omputer languages do not provide comple" ADT packages.

    To create a comple" ADT, it is first implemented and kept in

    a library. The main purpose of this chapter is to introduce

    some common userdefined ADTs and their applications.

    -owever, we also give a brief discussion of each ADT

    implementation for the interested reader. (e offer the pseudocode algorithms of the implementations as

    challenging e"ercises.

    Dr. Fares Saab

  • 8/18/2019 Chapter 3 data structure introductionn

    9/75

    -2 STACKS

    A stack is a restricted linear list in which all additionsand deletions are made at one end, the top. If we insert a

    series of data items into a stack and then remove them,

    the order of the data is reversed. This reversing attribute

    is why stacks are known as last in, first out $'I!% data

    structures.

    Fiure -.+ Three representations of stacksDr. Fares Saab

  • 8/18/2019 Chapter 3 data structure introductionn

    10/75

    #perations on stacks

    There are four basic operations,  stack ,  push,  pop and empty,

    that we define in this chapter.

    The stack  operation

    The  stack   operation creates an empty stack. The following

    shows the format.

    Fiure -.- Stack operationDr. Fares Saab

  • 8/18/2019 Chapter 3 data structure introductionn

    11/75

    The push operation

    The  push  operation inserts an item at the top of the stack.

    The following shows the format.

    Fiure -.1 2ush operationDr. Fares Saab

  • 8/18/2019 Chapter 3 data structure introductionn

    12/75

    The pop operation

    The  pop  operation deletes the item at the top of the stack.

    The following shows the format.

    Fiure -.3 2op operationDr. Fares Saab

  • 8/18/2019 Chapter 3 data structure introductionn

    13/75

    The empty operation

    The empty  operation checks the status of the stack. The

    following shows the format.

    This operation returns true if the stack is empty and false ifthe stack is not empty.

    Dr. Fares Saab

  • 8/18/2019 Chapter 3 data structure introductionn

    14/75

    Stack ADT

    (e define a stack as an ADT as shown below+

    Dr. Fares Saab

  • 8/18/2019 Chapter 3 data structure introductionn

    15/75

  • 8/18/2019 Chapter 3 data structure introductionn

    16/75

    Stack applications

    0tack applications can be classified into four broad

    categories+ reversing data,  pairing data,  postponing data 

    usage and backtracking steps. (e discuss the first two inthe sections that follow.

    5e%ersin data items

    1eversing data items re&uires that a given set of data items

     be reordered so that the first and last items are e"changed,

    with all of the positions between the first and last also being

    relatively e"changed. !or e"ample, the list $2, 3, 4, *, /, 5%

     becomes $5, /, *, 4, 3, 2%.

    Dr. Fares Saab

  • 8/18/2019 Chapter 3 data structure introductionn

    17/75

    ample -.+

    Dr. Fares Saab

  • 8/18/2019 Chapter 3 data structure introductionn

    18/75

    ample -.+ $#ontinued%

    Dr. Fares Saab

  • 8/18/2019 Chapter 3 data structure introductionn

    19/75

    2airin data items

    (e often need to pair some characters in an e"pression. !or

    e"ample, when we write a mathematical e"pression in a

    computer language, we often need to use parentheses to

    change the precedence of operators. The following two

    e"pressions are evaluated differently because of the

     parentheses in the second e"pression+

    (hen we type an e"pression with a lot of parentheses, we

    often forget to pair the parentheses. ne of the duties of a

    compiler is to do the checking for us. The compiler uses a

    stack to check that all opening parentheses are paired with a

    closing parentheses.Dr. Fares Saab

  • 8/18/2019 Chapter 3 data structure introductionn

    20/75

    ample -.-

    The below algorithm shows how we can check if all opening

     parentheses are paired with a closing parenthesis.

    Dr. Fares Saab

  • 8/18/2019 Chapter 3 data structure introductionn

    21/75

    ample -.- $#ontinued%

    Dr. Fares Saab

  • 8/18/2019 Chapter 3 data structure introductionn

    22/75

    Stack implementation

    At the ADT level, we use the stack and its four operations6 at

    the implementation level, we need to choose a data structure

    to implement it. 0tack ADTs can be implemented usingeither an array or a linked list. !igure ).4 shows an e"ample

    of a stack ADT with five items. The figure also shows how

    we can implement the stack.

    In our array implementation, we have a record that has two

    fields. The first field can be used to store information about

    the array. The linked list implementation is similar+ we have

    an e"tra node that has the name of the stack. This node also

    has two fields+ a counter and a pointer that points to the top

    element.

    Dr. Fares Saab

  • 8/18/2019 Chapter 3 data structure introductionn

    23/75

    Fiure -.6 Stack implementations

    Dr. Fares Saab

  • 8/18/2019 Chapter 3 data structure introductionn

    24/75

    -3 QUEUES

    A queue  is a linear list in which data can only beinserted at one end, called the rear , and deleted from the

    other end, called the front . These restrictions ensure that

    the data is processed through the &ueue in the order in

    which it is received. In other words, a &ueue is a first

    in, first out (F0F#) structure.

    Fiure -.7 Two representation of queuesDr. Fares Saab

    # i

  • 8/18/2019 Chapter 3 data structure introductionn

    25/75

    #perations on queues

    Although we can define many operations for a &ueue, four

    are basic+ &ueue, en&ueue, de&ueue and empty, as defined

     below.The queue operation

    The &ueue operation creates an empty &ueue. The following

    shows the format.

    Fiure -.8 The queue operationDr. Fares Saab

    Th ti

  • 8/18/2019 Chapter 3 data structure introductionn

    26/75

    The enqueue operation

    The en&ueue operation inserts an item at the rear of the

    &ueue. The following shows the format.

    Fiure -.9 The enqueue operationDr. Fares Saab

    Th d ti

  • 8/18/2019 Chapter 3 data structure introductionn

    27/75

    The dequeue operation

    The de&ueue operation deletes the item at the front of the

    &ueue. The following shows the format.

    Fiure -. The dequeue operationDr. Fares Saab

    Th t ti

  • 8/18/2019 Chapter 3 data structure introductionn

    28/75

    The empty operation

    The empty operation checks the status of the &ueue. The

    following shows the format.

    This operation returns true if the &ueue is empty and false ifthe &ueue is not empty.

    Dr. Fares Saab

    : ADT

  • 8/18/2019 Chapter 3 data structure introductionn

    29/75

    :ueue ADT

    (e define a &ueue as an ADT as shown below+

    Dr. Fares Saab

  • 8/18/2019 Chapter 3 data structure introductionn

    30/75

    ample -.1

    !igure ).*2 shows a segment of an algorithm that applies the

     previously defined operations on a &ueue 7.

    Fiure -.+ ample -.1Dr. Fares Saab

    : li ti

  • 8/18/2019 Chapter 3 data structure introductionn

    31/75

    :ueue applications

    7ueues are one of the most common of all data processing

    structures. They are found in virtually every operating

    system and network and in countless other areas. !ore"ample, &ueues are used in online business applications

    such as processing customer re&uests, 8obs and orders. In a

    computer system, a &ueue is needed to process 8obs and for

    system services such as print spools.

    Dr. Fares Saab

  • 8/18/2019 Chapter 3 data structure introductionn

    32/75

    ample -.3

    7ueues can be used to organi9e databases by some characteristic

    of the data. !or e"ample, imagine we have a list of sorted data

    stored in the computer belonging to two categories+ less than

    *:::, and greater than *:::. (e can use two &ueues to separate

    the categories and at the same time maintain the order of data in

    their own category. Algorithm ).) shows the pseudocode for this

    operation.

    Dr. Fares Saab

  • 8/18/2019 Chapter 3 data structure introductionn

    33/75

    ample -.3 $#ontinued%

    Dr. Fares Saab

    Alorithm -.- 

  • 8/18/2019 Chapter 3 data structure introductionn

    34/75

    ample -.3 $#ontinued%

    Alorithm -.- #ontinued

    Dr. Fares Saab

  • 8/18/2019 Chapter 3 data structure introductionn

    35/75

    ample -.4

    Another common application of a &ueue is to ad8ust and create a

     balance between a fast producer of data and a slow consumer of

    data. !or e"ample, assume that a #P; is connected to a printer.

    The speed of a printer is not comparable with the speed of a #P;.

    If the #P; waits for the printer to print some data created by the

    #P;, the #P; would be idle for a long time. The solution is a

    &ueue. The #P; creates as many chunks of data as the &ueue canhold and sends them to the &ueue. The #P; is now free to do

    other 8obs. The chunks are de&ueued slowly and printed by the

     printer. The &ueue used for this purpose is normally referred to as

    a spool &ueue.

    Dr. Fares Saab

    :ueue implementation

  • 8/18/2019 Chapter 3 data structure introductionn

    36/75

    :ueue implementation

    At the ADT level, we use the &ueue and its four operations at

    the implementation level. (e need to choose a data structure

    to implement it. A &ueue ADT can be implemented usingeither an array or a linked list. !igure ).*) shows an e"ample

    of a &ueue ADT with five items. The figure also shows how

    we can implement it. In the array implementation we have a

    record with three fields. The first field can be used to storeinformation about the &ueue.

    The linked list implementation is similar+ we have an

    e"tra node that has the name of the &ueue. This node also has

    three fields+ a count, a pointer that points to the front element

    and a pointer that points to the rear element.

    Dr. Fares Saab

  • 8/18/2019 Chapter 3 data structure introductionn

    37/75

    Fiure -.- :ueue implementationDr. Fares Saab

  • 8/18/2019 Chapter 3 data structure introductionn

    38/75

    GENERAL LINEAR LISTS

    0tacks and &ueues defined in the two previous sectionsare restricted linear lists. A eneral linear list is a list

    in which operations, such as insertion and deletion, can

     be done anywhere in the list—at the beginning, in the

    middle or at the end. !igure ).*3 shows a general linear

    list.

    Fiure -.1 ;eneral linear listDr. Fares Saab

    #perations on eneral linear lists

  • 8/18/2019 Chapter 3 data structure introductionn

    39/75

    #perations on eneral linear lists

    Although we can define many operations on a general linear

    list, we discuss only si" common operations in this chapter+

    list , insert , delete, retrieve, traverse and empty.

    The list  operation

    The list operation creates an empty list. The following shows

    the format+

    Dr. Fares Saab

    The insert operation

  • 8/18/2019 Chapter 3 data structure introductionn

    40/75

    The insert  operation

    0ince we assume that data in a general linear list is sorted,

    insertion must be done in such a way that the ordering of the

    elements is maintained. To determine where the element is to be placed, searching is needed. -owever, searching is done

    at the implementation level, not at the ADT level.

    Fiure -.3 The insert operationDr. Fares Saab

    The delete operation

  • 8/18/2019 Chapter 3 data structure introductionn

    41/75

    The delete operation

    Deletion from a general list $!igure ).*/% also re&uires that

    the list be searched to locate the data to be deleted. After the

    location of the data is found, deletion can be done. Thefollowing shows the format+

    Fiure -.4 The dequeue operationDr. Fares Saab

    The retrieve operation

  • 8/18/2019 Chapter 3 data structure introductionn

    42/75

    The retrieve operation

  • 8/18/2019 Chapter 3 data structure introductionn

    43/75

    The traverse operation

    =ach of the previous operations involves a single element in

    the list, randomly accessing the list. 'ist traversal, on the

    other hand, involves se&uential access. It is an operation inwhich all elements in the list are processed one by one. The

    following shows the format+

    The empty operation

    The empty operation checks the status of the list. Thefollowing shows the format+

    Dr. Fares Saab

    The empty operation

  • 8/18/2019 Chapter 3 data structure introductionn

    44/75

    The empty operation

    The empty operation checks the status of the list. The

    following shows the format+

    This operation returns true if the list is empty, or false if the

    list is not empty.

    Dr. Fares Saab

    ;eneral linear list ADT

  • 8/18/2019 Chapter 3 data structure introductionn

    45/75

    ;eneral linear list ADT

    (e define a general linear list as an ADT as shown below+

    Dr. Fares Saab

    ample - 6

  • 8/18/2019 Chapter 3 data structure introductionn

    46/75

    ample -.6

    !igure ).*5 shows a segment of an algorithm that applies the

     previously defined operations on a list '. >ote that the third and

    fifth operation inserts the new data at the correct position, because the insert operation calls the search algorithm at the

    implementation level to find where the new data should be

    inserted. The fourth operation does not delete the item with value

    ) because it is not in the list.

    Fiure -. 7 ample -.6Dr. Fares Saab

    ;eneral linear list applications

  • 8/18/2019 Chapter 3 data structure introductionn

    47/75

    ;eneral linear list applications

    ?eneral linear lists are used in situations in which the

    elements are accessed randomly or se&uentially. !or

    e"ample, in a college a linear list can be used to storeinformation about students who are enrolled in each

    semester.

    Dr. Fares Saab

    ample - 7

  • 8/18/2019 Chapter 3 data structure introductionn

    48/75

    ample -.7

    Assume that a college has a general linear list that holds

    information about the students and that each data element is a

    record with three fields+ ID, >ame and ?rade. Algorithm ).3

    shows an algorithm that helps a professor to change the grade for

    a student. The delete operation removes an element from the list,

     but makes it available to the program to allow the grade to be

    changed. The insert operation inserts the changed element backinto the list. The element holds the whole record for the student,

    and the target is the ID used to search the list.

    Dr. Fares Saab

    ample - 7 $#ontinued%

  • 8/18/2019 Chapter 3 data structure introductionn

    49/75

    ample -.7 $#ontinued%

    Dr. Fares Saab

    ample - 8

  • 8/18/2019 Chapter 3 data structure introductionn

    50/75

    ample -.8

    #ontinuing with ="ample ).5, assume that the tutor wants to print

    the record of all students at the end of the semester. Algorithm

    *2.@ can do this 8ob. (e assume that there is an algorithm calledPrint that prints the contents of the record. !or each node, the list

    traverse calls the Print algorithm and passes the data to be printed

    to it.

    Dr. Fares Saab

    ample - 8 $#ontinued%

  • 8/18/2019 Chapter 3 data structure introductionn

    51/75

    ample -.8 $#ontinued%

    Dr. Fares Saab

    ;eneral linear list implementation

  • 8/18/2019 Chapter 3 data structure introductionn

    52/75

    ;eneral linear list implementation

    At the ADT level, we use the list and its si" operations but at

    the implementation level we need to choose a data structure

    to implement it. A general list ADT can be implementedusing either an array or a linked list. !igure ).* shows an

    e"ample of a list ADT with five items. The figure also shows

    how we can implement it.

    The linked list implementation is similar+ we have an e"tra

    node that has the name of the list. This node also has two

    fields, a counter and a pointer that points to the first element.

    Dr. Fares Saab

  • 8/18/2019 Chapter 3 data structure introductionn

    53/75

    Fiure +.8 ;eneral linear list implementationDr. Fares Saab

  • 8/18/2019 Chapter 3 data structure introductionn

    54/75

    12-5 TREES

    A tree consists of a finite set of elements, called nodes 

    $or %ertices% and a finite set of directed lines, called

    arcs, that connect pairs of the nodes.

    Fiure +.+9 Tree representationDr. Fares Saab

    (e can divided the vertices in a tree into three categories+

  • 8/18/2019 Chapter 3 data structure introductionn

    55/75

    (e can divided the vertices in a tree into three categories+

    the root , leaves and the internal nodes. Table *2.* shows the

    number of outgoing and incoming arcs allowed for each

    type of node.

    Dr. Fares Saab

    =ach node in a tree may have a subtree The subtree of each

  • 8/18/2019 Chapter 3 data structure introductionn

    56/75

    =ach node in a tree may have a subtree. The subtree of each

    node includes one of its children and all descendents of that

    child. !igure *2.2* shows all subtrees for the tree in !igure

    *2.2:.

    Fiure +.+ SubtreesDr. Fares Saab

  • 8/18/2019 Chapter 3 data structure introductionn

    57/75

    12-6 BINARY TREES

    A binary tree is a tree in which no node can have morethan two subtrees. In other words, a node can have 9ero,

    one or two subtrees.

    Fiure +.++ A binary treeDr. Fares Saab

    5ecursi%e definition of binary trees

  • 8/18/2019 Chapter 3 data structure introductionn

    58/75

    y

    In #hapter 5 we introduced the recursive definition of an

    algorithm. (e can also define a structure or an ADT

    recursively. The following gives the recursive definition of a binary tree. >ote that, based on this definition, a binary tree

    can have a root, but each subtree can also have a root.

    Dr. Fares Saab

    !igure *2 2) shows eight trees the first of which is an empty

  • 8/18/2019 Chapter 3 data structure introductionn

    59/75

    !igure *2.2) shows eight trees, the first of which is an empty

     binary tree $sometimes called a null binary tree%.

    Fiure +.+- amples of binary treesDr. Fares Saab

    #perations on binary trees

  • 8/18/2019 Chapter 3 data structure introductionn

    60/75

    p y

    The si" most common operations defined for a binary tree

    are tree  $creates an empty tree%, insert , delete, retrieve,

    empty and traversal . The first five are comple" and beyondthe scope of this book. (e discuss binary tree traversal in

    this section.

    Dr. Fares Saab

    "inary tree tra%ersals

  • 8/18/2019 Chapter 3 data structure introductionn

    61/75

    A binary tree traversal re&uires that each node of the tree be

     processed once and only once in a predetermined se&uence.

    The two general approaches to the traversal se&uence aredepth

  • 8/18/2019 Chapter 3 data structure introductionn

    62/75

    p

    !igure *2.2@ shows how we visit each node in a tree using

     preorder traversal. The figure also shows the walking order. In

     preorder traversal we visit a node when we pass from its left side.The nodes are visited in this order+ A,

  • 8/18/2019 Chapter 3 data structure introductionn

    63/75

    p

    !igure *2.2/ shows how we visit each node in a tree using

     breadthfirst traversal. The figure also shows the walking order.

    The traversal order is A,

  • 8/18/2019 Chapter 3 data structure introductionn

    64/75

    y pp

  • 8/18/2019 Chapter 3 data structure introductionn

    65/75

    pression trees

    An arithmetic e"pression can be represented in three

    different formats+ infi, postfi  and prefi. In an infi"

    notation, the operator comes between the two operands. In

     postfi" notation, the operator comes after its two operands,

    and in prefi" notation it comes before the two operands.

    These formats are shown below for addition of two operandsA and

  • 8/18/2019 Chapter 3 data structure introductionn

    66/75

    Fiure +.+6 pression treeDr. Fares Saab

  • 8/18/2019 Chapter 3 data structure introductionn

    67/75

    12-7 BINARY SEARCH TREES

    A binary search tree $

  • 8/18/2019 Chapter 3 data structure introductionn

    68/75

    p

    !igure *2.2 shows some binary trees that are ote that a tree is a

  • 8/18/2019 Chapter 3 data structure introductionn

    69/75

    A very interesting property of a

  • 8/18/2019 Chapter 3 data structure introductionn

    70/75

    use a version of the binary search we used in #hapter 5 for a

     binary search tree. !igure *2.): shows the ;' for a

  • 8/18/2019 Chapter 3 data structure introductionn

    71/75

    The ADT for a binary search tree is similar to the one we

    defined for a general linear list with the same operation. As a

    matter of fact, we see more

  • 8/18/2019 Chapter 3 data structure introductionn

    72/75

  • 8/18/2019 Chapter 3 data structure introductionn

    73/75

    12-8 GRAPHS

    A graph is an ADT made of a set of nodes, called%ertices, and set of lines connecting the vertices, called

    edes  or arcs. (hereas a tree defines a hierarchical

    structure in which a node can have only one single

     parent, each node in a graph can have one or more

     parents. ?raphs may be either directed or undirected. In

    a directed graph, or diraph, each edge, which connects

    two vertices, has a direction from one verte" to theother. In an undirected graph, there is no direction.

    !igure *2.)2 shows an e"ample of both a directed graph

    $a% and an undirected graph $b%.

    Dr. Fares Saab

  • 8/18/2019 Chapter 3 data structure introductionn

    74/75

    Fiure +.-+ ;raphs

    Dr. Fares Saab

    ample +.-

  • 8/18/2019 Chapter 3 data structure introductionn

    75/75

    A map of cities and the roads connecting the cities can be

    represented in a computer using an undirected graph. The cities

    are vertices and the undirected edges are the roads that connectthem. If we want to show the distances between the cities, we can

    use weighted graphs, in which each edge has a weight that

    represents the distance between two cities connected by that

    edge.

    ample +.1

    Another application of graphs is in computer networks $#hapter

    /%. The vertices can represent the nodes or hubs, the edges can

    represent the route. =ach edge can have a weight that defines thecost of reaching from one hub to an ad8acent hub. A router can

    use graph algorithms to find the shortest path between itself and

    the final destination of a packet.