Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the...

67
Chapter 12 Abstract Data Type

Transcript of Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the...

Page 1: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Chapter 12

AbstractData Type

Page 2: Chapter 12 Abstract Data Type. 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 an Understand the concept of an abstract data type (ADT).abstract data type (ADT).

Understand the concept of a Understand the concept of a linear listlinear list as well as its operations 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 Understand the concept of a stackstack as well as its operations as well as its operations and applications. and applications.

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

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

Page 3: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

BACKGROUNDBACKGROUNDBACKGROUNDBACKGROUND12.112.1

Page 4: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Abstract Data Type (ADT) An Abstract Data Type (ADT)

is a data structure and a collection of functions which operate on the data structure.

Stack – The operations new(), push(v, S), top(S), and pop(S) may be defined with axiomatic semantics as following. new() returns a stack pop(push(v, S)) = S top(push(v, S)) = v where S is a stack and v is a value.

Page 5: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

The concept of The concept of abstractionabstraction means:means:1. You know 1. You know whatwhat a data type a data type can do.can do.2. 2. HowHow it is done is it is done is hiddenhidden. .

Note:Note:

Page 6: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Queue problem Queue simulation

queueenqueuing dequeuing

Page 7: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Abstract Data TypeAbstract Data Type

1.1.Declaration of Declaration of datadata

2.2.Declaration of Declaration of operationsoperations

3.3.EncapsulationEncapsulation of of datadata and and operationsoperations

Note:Note:

Page 8: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Stack problem Stack –

The operations new(), push(S, v), top(S), and popoff(S) may be defined with axiomatic semantics as following. new() returns a stack popoff(push(S, v)) = S top(push(S, v)) = v

where S is a stack and v is a value.

The pop operation is a combination of top, to return the top value, and popoff, to remove the top value.

Page 9: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Figure 12-1 Model for ADT

Page 10: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

LINEARLINEARLISTSLISTS

LINEARLINEARLISTSLISTS

12.212.2

Page 11: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Figure 12-2 Linear list

Linear List is a list in which each element has a unique successor.

Page 12: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Figure 12-3 Categories of linear lists

Data can be inserted and deleted anywhere.

Data can only be inserted and deleted at the ends of the structure.

Page 13: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Figure 12-4 Insertion in a general linear list

with ordered data

Page 14: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Figure 12-5 Deletion from a linear list

Page 15: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Figure 12-6 Retrieval from a linear list

Page 16: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Figure 12-7 Traversal of a linear list

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

Page 17: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Implementation

Two common methods:ArrayLinked list

Page 18: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

STACKSSTACKSSTACKSSTACKS

12.312.3

Page 19: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Figure 12-8 Three representations of a stack

Stacks A restricted linear list in which all additions and

deletions are made at one end, called the top. A LIFO (Last In, First Out) data structure.

Page 20: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Figure 12-9 Push operation in a stack

Page 21: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Figure 12-10 Pop operation in a stack

Page 22: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

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: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Implementation Two common methods:

Array Linked list (more common)

Applications Reversing data

1,2,3,4 → 4,3,2,1

Parsing Unmatched parentheses in an algebraic expression

Postponing data Postfix: ab+c* → (a+b)*c,

abc+* → a*(b+c)

Backtracking steps Computer gaming

Page 24: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

QUEUESQUEUESQUEUESQUEUES12.412.4

Page 25: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Figure 12-12 Queue representation

Queues A restricted linear list in which data can only be

inserted at one end (rear) and deleted from the other end (front).

A FIFO (First In, First Out) data structure.

Page 26: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Figure 12-13 Enqueue operation

Page 27: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Figure 12-14 Dequeue operation

Page 28: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Example 2Example 2Show 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: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Implementation Two common methods:

Array Linked list (more common)

Applicationsfound in virtually every OS and network and in countless other areas.

Page 30: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

TREESTREESTREESTREES12.512.5

Page 31: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Trees A Tree consists of

a finite set of elements called Nodes a finite set of directed lines, called Branches, that connect the nodes

Degree of a node – number of branches associated with the node. Indegree branch Outdegree branch

Root – The first node of a tree Indegree : 0

Other nodes – Indegree : 1

All nodes – outdegree : 0, 1 or more.

Page 32: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Figure 12-16

Representation of a tree

Page 33: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Trees Leaf –

Outdegree : 0 Internal nodes –

a node that is not a root or a leaf. Parent –

A node with successors outdegree : > 0

Child – A node with a predecessor indegree : 1

Siblings – two or more nodes with the same parent.

Ancestor – any node in the path from the root to the node.

Descendant – any node in the path from the node to a leaf.

Page 34: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Trees Path –

a sequence of nodes in which each node is adjacent to the next one.

Level of a node – its distance from the root. Root : 0

Height (Depth) of the tree – the level of the leaf in the longest path from the root plus 1. Subtree –

any connected structure below the root.

Page 35: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Figure 12-17

Tree terminology

Height : 3

Page 36: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Figure 12-18

Subtrees

Page 37: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

BINARYBINARYTREESTREES

BINARYBINARYTREESTREES

12.612.6

Page 38: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Binary Trees

Binary tree – A tree in which no node can have more than two subtrees.

Null tree - A tree with no node. Height of a Binary Tree – (with N nodes)

Hmax = N Hmin = [log2N] +1

# of nodes of a binary tree – (Given height H) Nmin = H Nmax = 2H - 1

Page 39: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Figure 12-19 Binary tree

Page 40: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Figure 12-20 Examples of binary trees

Page 41: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Balanced Trees

The distance of a node from the root determines how efficiently it can be located.

Balance Factor of a Binary Tree – is the difference in height between its left and right subtrees.B = HL - HR

A binary tree is balanced if the height of its subtrees differs by no more than 1 (B = -1, 0, or +1) and its subtrees are also balanced.

Page 42: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

A

B C

D

F

E

Page 43: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Binary Tree Traversal

Binary Tree Traversal each node of the tree be processed once and only once in a predetermined sequence.

Depth-First Traversal – Preorder Traversal (NLR) – Inorder Traversal (LNR) – Postorder Traversal (LRN) – Need a stack

Breadth-First Traversal – process all of the children of a node before proceeding

with the next level. Need a queue

Page 44: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Figure 12-21 Depth-first traversal of a binary tree

NLR LNR LRN

Page 45: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Figure 12-22 Preorder traversal of a binary tree

A, B, C, D, E, F

Page 46: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Figure 12-23Inorder traversal of a binary tree

C, B, D, A, E, F

Page 47: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Figure 12-24Postorder traversal of a binary tree

C, D, B, F, E, A

Page 48: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Figure 12-25 Breadth-first traversal of a binary tree

Page 49: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Implementation A binary tree is normally implemented as a linked list.

Applications Expression tree –

a binary tree with the following properties: Each leaf is an operand The root and internal nodes are operators. Subtrees are subexpressions, with the root being an operator.

Three different expression formats:1. Infix expression

2. Postfix expression – produced by Postorder traversal

3. Prefix expression – produced by Preorder traversal

Page 50: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Figure 12-26 Expression tree

• Infix expression:

• Prefix expression: + * a + b c d • Postfix expression: a b c + * d +

Page 51: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

GRAPHSGRAPHSGRAPHSGRAPHS12.712.7

Page 52: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Graphs A Graph consists of

A set of vertices (sing. vertex) and A set of lines (connecting pairs of vertices)

Directed graph (digraph) – each line has a direction to its successor - arc

Undirected graph – each line has no direction – edge

Adjacent vertices (neighbors) –two vertices directly connected by a line

Path –a sequence of vertices in which each vertex is adjacent to the next one.

Cycle –a path consisting of at least three vertices that starts and ends with the same vertex.

Page 53: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Graphs

Two vertices are said to be connected if there is a path between them.

A graph is said to be connected (suppressing direction) if there is a path from any vertex to any other vertex.

A directed graph is strongly connected if there is a path from each vertex to every other vertex.

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

Page 54: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Figure 12-27 Directed and undirected graphs

Page 55: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Figure 12-28 Add vertex

Page 56: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Figure 12-29 Delete vertex

Page 57: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Figure 12-30Add edge

Page 58: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Figure 12-31Delete edge

Page 59: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Figure 12-32 Find vertex

Page 60: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Traverse Graph

You must somehow ensure that you process the data in each vertex only once.

Visited flag

Two standard graph traversals Depth-first traversal –

you process a vertex’s descendants before you move to an adjacent vertex.

Breadth-first traversal – you process all adjacent vertices of a vertex before going to the next level.

Page 61: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Figure 12-33 Depth-first traversal of a graph

•在不同的資料結構下,即使起始點相同,產生的順序亦有所不同。

Page 62: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Figure 12-34 Breadth-first traversal of a graph

•在不同的資料結構下,即使起始點相同,產生的順序亦有所不同。

Page 63: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Implementation Two common methods:

Array (adjacency matrix) Linked list (adjacency list)

ApplicationsNetwork

Weighted graph - a graph with weighted lines.

Minimum Spanning Tree A spanning tree

is a tree that contains all of the vertices in the graph. A minimum spanning tree

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

Page 64: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Figure 12-35: Part I Graph implementations

Weight (e.g. distance between networks in an internetwork)

Page 65: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

Figure 12-35: Part 2 Graph implementations

Page 66: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

1 2

3 4 5 6

7

0

Page 67: Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.

10 10

40 20

2030