Top Banner
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type
62
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

Chapter 12

AbstractData Type

Page 2: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

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

Understand the concept of a linear list as well as its operations Understand the concept of a linear list as well as its operations and applications.and applications.

After reading this chapter, the reader should After reading this chapter, the reader should be able to:be able to:

OOBJECTIVESBJECTIVES

Understand the concept of a stack as well as its operations Understand the concept of a stack as well as its operations and applications. and applications.

Understand the concept of a queue as well as its operations Understand the concept of a queue as well as its operations and applications.and applications.

Understand the concept of a tree as well as its operations Understand the concept of a tree as well as its operations and applications.and applications.Understand the concept of a graph as well as its operations Understand the concept of a graph as well as its operations and applications.and applications.

Page 3: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

BACKGROUNDBACKGROUNDBACKGROUNDBACKGROUND12.112.1

Page 4: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

The concept of The concept of abstractionabstraction means:means:1. You know what a data type 1. You know what a data type can do.can do.2. How it is done is hidden. 2. How it is done is hidden.

Note:Note:

Page 5: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

Abstract Data TypeAbstract Data Type

1.1.Declaration of dataDeclaration of data

2.2.Declaration of operationsDeclaration of operations

3.3.Encapsulation Encapsulation ( 膠囊化 ) of of data and operationsdata and operations

Note:Note:

Page 6: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

Figure 12-1

Model for ADT

Page 7: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

LINEARLINEARLISTSLISTS

LINEARLINEARLISTSLISTS

12.212.2

Page 8: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

Figure 12-2

Linear list

A linear list is a list in which each A linear list is a list in which each element has a unique successor.element has a unique successor.

Linear lists can be divided into two Linear lists can be divided into two categories: general and restrictedcategories: general and restricted General list: random list and ordered General list: random list and ordered

listlist Restricted list: queue and stackRestricted list: queue and stack

Page 9: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

Figure 12-3

Categories of linear lists

Page 10: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

Operations on linear listsOperations on linear lists Insertion:Insertion: Fig. 12.4 Fig. 12.4

Most of the time, data are inserted Most of the time, data are inserted somewhere in the middle of the list.somewhere in the middle of the list.

Deletion:Deletion: Fig. 12.5 Fig. 12.5 Deletion from a general list requires that Deletion from a general list requires that

the list be searched to located the data the list be searched to located the data being deleted.being deleted.

Retrieval:Retrieval: Fig. 12.6 Fig. 12.6 A copy of the data should be retrieved A copy of the data should be retrieved

without changing the contents of the list.without changing the contents of the list. Traversal:Traversal: Fig. 12.7 Fig. 12.7

List traversal is an operation in which all List traversal is an operation in which all elements in the list are processed elements in the list are processed sequentially, one by one.sequentially, one by one.

Page 11: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

Figure 12-4

Insertion in a linear list

Page 12: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

Figure 12-5

Deletion from a linear list

Page 13: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

Figure 12-6

Retrieval from a linear list

Page 14: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

Figure 12-7

Traversal of a linear list

Page 15: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

ImplementationImplementation

Implementation of a general Implementation of a general linear list:linear list: ArrayArray Linked listLinked list

Page 16: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

STACKSSTACKSSTACKSSTACKS

12.312.3

Page 17: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

Figure 12-8

Three representations of a stack

Page 18: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

Operations on stacksOperations on stacks

Push: Fig. 12.9Push: Fig. 12.9 PushPush adds an item at the top of the stack. adds an item at the top of the stack. If there is not enough room, the stack is in If there is not enough room, the stack is in

an an overflowoverflow state and the item cannot be state and the item cannot be added.added.

Pop: Fig. 12.10Pop: Fig. 12.10 When you When you poppop a stack, you remove the a stack, you remove the

item at the top of the stack and return it item at the top of the stack and return it to the user.to the user.

If pop is called when the stack is empty, it If pop is called when the stack is empty, it is in an is in an underflow underflow state.state.

Page 19: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

Figure 12-9

Push operation in a stack

Page 20: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

Figure 12-10

Pop operation in a stack

Page 21: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

Implementation and Implementation and applicationapplication

Implementation of a stack:Implementation of a stack: ArrayArray Linked listLinked list

Stack applicationsStack applications Reversing dataReversing data ParsingParsing Postponement (Postponement (延期延期 )) Backtracking Backtracking

Page 22: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

Example 1Example 1

Show the result of the following operations on a stack S.push (S , 10)push (S , 12)push (S , 8)if not empty (S), then pop (S)push (S , 2)

SolutionSolution

Page 23: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

QUEUESQUEUESQUEUESQUEUES12.412.4

Page 24: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

Figure 12-12

Queue representation

Page 25: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

Operations on queuesOperations on queues Enqueue: Fig. 12.13Enqueue: Fig. 12.13

After the data have been inserted into the After the data have been inserted into the queue, the new element becomes the queue, the new element becomes the rearrear..

If there is not enough room for another If there is not enough room for another element in the queue, the queue is in an element in the queue, the queue is in an overflow overflow state.state.

Dequeue: Fig. 12.14Dequeue: Fig. 12.14 The data at theThe data at the front front of the queue are of the queue are

removed from the queue and returned to removed from the queue and returned to the user.the user.

If there are no data in the queue when a If there are no data in the queue when a dequeue is attempted, the queue is in an dequeue is attempted, the queue is in an underflowunderflow state. state.

Page 26: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

Figure 12-13

Enqueue operation

Page 27: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

Figure 12-14

Dequeue operation

Page 28: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

Example 2Example 2

Show the result of the following operations on a queue Q.enqueue (Q , 23)if not empty (Q), dequeue (Q)enqueue (Q , 20)enqueue (Q , 19)if not empty (Q), dequeue (Q)

SolutionSolution

Page 29: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

Implementation and Implementation and applicationapplication

Implementation of a queue:Implementation of a queue: ArrayArray Linked listLinked list

Queue applicationsQueue applications Operating systemOperating system Preserve the order of data …Preserve the order of data …

Page 30: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

TREESTREESTREESTREES12.512.5

Page 31: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

Basic tree conceptsBasic tree concepts A A treetree consists of a finite set of consists of a finite set of

elements (elements (nodesnodes) and a finite set of ) and a finite set of directed lines (directed lines (branchesbranches) that ) that connect the nodes.connect the nodes. Degree of a node: indegree and Degree of a node: indegree and

outdegreeoutdegree Root, internal node, and leafRoot, internal node, and leaf Parent, child, and siblingsParent, child, and siblings Ancestor (Ancestor (祖先祖先 ) and descendant () and descendant (後裔後裔 )) PathPath Level, height, and depthLevel, height, and depth

Page 32: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

Figure 12-16

Representation of a tree

Page 33: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

Figure 12-17

Tree terminology

Page 34: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

Figure 12-18

Subtrees

Page 35: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

BINARYBINARYTREESTREES

BINARYBINARYTREESTREES

12.612.6

Page 36: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

Figure 12-19

Binary tree

A A binary treebinary tree is a tree in which no node is a tree in which no node can have more than two subtrees.can have more than two subtrees.

A A null treenull tree is a tree with no nodes. is a tree with no nodes.

Page 37: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

Figure 12-20

Examples of binary trees

Page 38: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

Properties of binary treesProperties of binary trees

Height of binary tree:Height of binary tree: N N : the number of nodes in a binary : the number of nodes in a binary

treetree

HHmaxmax = = NN

HHminmin = [log = [log22N N ]] + 1+ 1 HH : the height of a binary tree: the height of a binary tree

NNminmin = H = H

NNmaxmax = 2 = 2HH – – 1 1

Page 39: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

Properties of binary treesProperties of binary trees

Balance factor : Balance factor : B B The balance factor of a binary tree The balance factor of a binary tree

is the difference in height between is the difference in height between its left and right subtrees.its left and right subtrees.

BB = = HHLL--HHRR

A binary tree is A binary tree is balancedbalanced if the if the height of its subtrees differs by no height of its subtrees differs by no more than 1 (AVL tree)more than 1 (AVL tree)

Page 40: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

Operations on binary Operations on binary treestrees

Binary tree traversalsBinary tree traversals A A binary tree traversalbinary tree traversal requires that requires that

each node of the tree be processed each node of the tree be processed once and only onceonce and only once in a in a predetermined sequence.predetermined sequence.

Depth-first traversalsDepth-first traversals Preorder, inorder, and postorder Preorder, inorder, and postorder

traversalstraversals Breadth-first traversalsBreadth-first traversals

Page 41: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

Figure 12-21

Depth-first traversal of a binary tree

Page 42: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

Figure 12-22

Preorder traversal of a binary treeA B C D E F

Page 43: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

Figure 12-23

Inorder traversal of a binary treeC B D A E F

Page 44: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

Figure 12-24

Postorder traversal of a binary treeC D B F E A

Page 45: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

Figure 12-25

Breadth-first traversal of a binary tree

Page 46: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

Implementation and Implementation and applicationapplication

Implementation of a binary tree: Implementation of a binary tree: linked listlinked list

ApplicationApplication Expression treeExpression tree

Each leaf is an operandEach leaf is an operand The root and internal nodes are The root and internal nodes are

operatorsoperators Subtrees are subexpressions, with the Subtrees are subexpressions, with the

root being an operatorroot being an operator

Page 47: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

Figure 12-26

Expression treeInfix: a*(b+c)+dPrefix: +*a+bcdPostfix: abc+*d+

Page 48: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

GRAPHSGRAPHSGRAPHSGRAPHS12.712.7

Page 49: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

Figure 12-27

Directed and undirected graphs

Page 50: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

GraphsGraphs A graph is a collection of nodes (A graph is a collection of nodes (verticesvertices) )

and a collection of line segments (and a collection of line segments (lineslines) ) connecting pairs of vertices.connecting pairs of vertices. Directed graph (digraph): line Directed graph (digraph): line arcarc Undirected graph: line Undirected graph: line edgeedge

Path, cycle, and loopPath, cycle, and loop A A pathpath is a sequence of vertices in which each is a sequence of vertices in which each

vertex is adjacent to the next one.vertex is adjacent to the next one. A A cycle cycle is a path consisting of at least is a path consisting of at least threethree

vertices that starts and ends with the same vertices that starts and ends with the same vertex.vertex.

A A looploop is a is a special casespecial case of a cycle in which a of a cycle in which a single arcsingle arc begins and ends at the same vertex. begins and ends at the same vertex.

Page 51: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

GraphsGraphs

Strongly connected and weakly Strongly connected and weakly connectedconnected A directed graph is A directed graph is strongly connectedstrongly connected if if

there is a there is a pathpath from each vertex to every from each vertex to every other vertex in the digraph.other vertex in the digraph.

A directed graph is A directed graph is weakly connectedweakly connected if if at least two vertices are not connected.at least two vertices are not connected.

A disjoint graph is not connected.A disjoint graph is not connected. Degree, outdegree, and indegree of a Degree, outdegree, and indegree of a

vertexvertex

Page 52: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

Operations on graphsOperations on graphs

Add vertexAdd vertex Delete vertexDelete vertex Add edgeAdd edge Delete edgeDelete edge Find vertexFind vertex Traverse graphTraverse graph

Depth-first traversalDepth-first traversal Breadth-first traversalBreadth-first traversal

Page 53: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

Figure 12-28

Add vertex

Delete vertex

Page 54: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

Figure 12-30

Add edge

Delete edge

Page 55: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

Figure 12-32

Find vertex

Page 56: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

Figure 12-33

Depth-first traversal of a graph

A X G H E M J Y P

3 5

8

6

7

4

9

Page 57: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

Figure 12-34

Breadth-first traversal of a graphA X G H P E M Y J

Page 58: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

Implementation and Implementation and applicationapplication

Implementation of a graph:Implementation of a graph: Array (Fig. 12.35)Array (Fig. 12.35) Linked list (Fig. 12.35)Linked list (Fig. 12.35)

Graph applicationsGraph applications Networks: weighted graphNetworks: weighted graph Minimum spanning treeMinimum spanning tree

A A spanning treespanning tree is a tree that contains is a tree that contains allall of the vertices in the graph.of the vertices in the graph.

A A minimum spanning treeminimum spanning tree is a spanning is a spanning tree such that the sum of its weights is tree such that the sum of its weights is thethe minimum minimum..

Page 59: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

Figure 12-35: Part I

Graph implementations

Page 60: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

Figure 12-35: Part 2

Graph implementations

Page 61: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

Key termsKey terms Abstract data typeAbstract data type AbstractionAbstraction AncestorAncestor ArcArc ArrayArray BacktrackingBacktracking Balance factorBalance factor Binary treeBinary tree Binary tree traversalBinary tree traversal BranchBranch Breadth-first Breadth-first

traversaltraversal Child Child Cycle Cycle

DegreeDegree DepthDepth Depth-first traversalDepth-first traversal DequeueDequeue DescendantDescendant DigraphDigraph Directed graphDirected graph Disjoint graphDisjoint graph EdgeEdge EnqueueEnqueue Expression treeExpression tree FIFOFIFO General listGeneral list

Page 62: ©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.

©Brooks/Cole, 2003

GraphGraph HeightHeight IndegreeIndegree InfixInfix Inorder Inorder

traversaltraversal Internal nodeInternal node LIFOLIFO LeafLeaf LevelLevel LineLine Linear listLinear list LoopLoop Minimum Minimum

spanning treespanning tree Node null Node null

treetree

RootRoot SiblingsSiblings Spanning treeSpanning tree StackStack Strongly Strongly

connected connected graphgraph

SubtreeSubtree TokenToken TraversalTraversal TreeTree UnderflowUnderflow Undirected Undirected

graphgraph VertexVertex Weakly Weakly

connected connected graphgraph

Weighted graphWeighted graph

Ordered listOrdered list OutdegreeOutdegree ParentParent ParsingParsing PathPath PopPop PostfixPostfix Postorder Postorder

traversaltraversal PrefixPrefix Preorder Preorder

traversal traversal PushPush QueueQueue Queue simulationQueue simulation Random listRandom list Restricted listRestricted list RetrievalRetrieval