COS1521-foundation of Computer Science - Chapter 12
COS1521-foundation of Computer Science - Chapter 12
Abstract
Data Types
12.1
Source: Foundations of Computer Science © Cengage Learning
Objectives
After studying this chapter, the student should be able to:
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 general linear list, the basic operations on lists, their
applications and how they can be implemented.
Define a general tree and its application.
Define a binary tree—a special kind of tree—and its
applications.
Define a binary search tree (BST) and its applications.
Define a graph and its applications.
12.2
1
12-1 BACKGROUND
12.3
Simple ADTs
Many programming languages already define some simple
ADTs as integral parts of the language. For example, the C
language defines a simple ADT as an integer. The type of
this ADT is an integer with predefined ranges. C also
defines several operations that can be applied to this data
type (addition, subtraction, multiplication, division and so
on). C explicitly defines these operations on integers and
what we expect as the results. A programmer who writes a C
program to add two integers should know about the integer
ADT and the operations that can be applied to it.
12.4
2
Complex 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 complex
ADTs are not. As we will see in this chapter, we need a list
ADT, a stack ADT, a queue ADT and so on. To be
efficient, these ADTs should be created and stored in the
library of the computer to be used.
12.5
Definition
Let us now define an ADT. An abstract data type is a data
type packaged with the operations that are meaningful
for the data type. We then encapsulate the data and the
operations on the data and hide them from the user.
12.6
3
Model for an abstract data type
The ADT model is shown in Figure 12.1. Inside the ADT are
two different parts of the model: data structure and
operations (public and private).
Implementation
Computer languages do not provide complex ADT packages.
To create a complex ADT, it is first implemented and kept in
a library. The main purpose of this chapter is to introduce
some common user-defined ADTs and their applications.
However, we also give a brief discussion of each ADT
implementation for the interested reader. We offer the
pseudocode algorithms of the implementations as
challenging exercises.
12.8
4
12-2 STACKS
Operations on stacks
There are four basic operations, stack, push, pop and
empty, that we define in this chapter.
stack(S)
5
The push operation
The push operation inserts an item at the top of the stack.
The following shows the format.
push(S,20)
push (S,num) push(S,78)
{ top = top +1; push(S,30)
S[top] = num;
}
pop (S)
{ num = S[top];
pop(S,x); // x=30 top = top -1;
return num;
}
6
The empty operation
The empty operation checks the status of the stack. The
following shows the format.
If empty(S)
This operation returns true if the stack is empty and false if
the stack is not empty.
12.13
Stack ADT
We define a stack as an ADT as shown below:
12.14
7
Give a stack S as below,after Pop(S)、
Push(S,25)、Push(S,33)、Pop(S)、Pop(S) and
Pop(S) ,Top = 及S[top]=
1 2 3 4 5 6 7 8 9
S 75 56 43 68 92 14 28
Top
12.15
Example 12.1
8
Stack applications
Stack applications can be classified into four broad
categories: reversing data, pairing data, postponing data
usage and backtracking steps. We discuss the first two in
the sections that follow.
12.17
Example 12.2
Convert a decimal integer to binary and print the results
12.18
9
Example 12.2 (Continued)
12.19
12.20
10
Pairing data items
We often need to pair some characters in an expression.
For example, when we write a mathematical expression in a
computer language, we often need to use parentheses to
change the precedence of operators. The following
expression is evaluated because of the parentheses:
12.22
11
Stack implementation
At the ADT level, we use the stack and its four operations; at
the implementation level, we need to choose a data
structure to implement it. Stack ADTs can be implemented
using either an array or a linked list. Figure 12.7 shows an
example of a stack ADT with five items. The figure also
shows how we can implement the stack.
12.23
12
12-3 QUEUES
Operations on queues
Although we can define many operations for a queue, four
are basic: queue, enqueue, dequeue and empty, as defined
below.
The queue operation
The queue operation creates an empty queue. The
following shows the format.
queue(Q)
13
The enqueue operation
The enqueue operation inserts an item at the rear of the
queue. The following shows the format.
enqueue (Q,num)
enqueue(Q,20) { rear = rear +1;
enqueue(Q,78) if (front ==-1) front = rear;
enqueue(Q,34) Q[rear] = num;
}
12.27
12.28
14
The empty operation
The empty operation checks the status of the queue. The
following shows the format.
if empty(Q)
12.29
Queue ADT
We define a queue as an ADT as shown below:
12.30
15
Example 12.4
0 1 2 3 4 5 6 7 8 9
Q 42 69 57 35
Front Rear
12.32
16
Queue applications
Queues 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. For
example, queues are used in online business applications
such as processing customer requests, jobs and orders. In
a computer system, a queue is needed to process jobs and for
system services such as print spools.
Example 12.5
12.34
17
Example 12.6
12.35
Queue implementation
At the ADT level, we use the queue and its four operations at
the implementation level. We need to choose a data structure
to implement it. A queue ADT can be implemented using
either an array or a linked list. Figure 12.13 on page 329
shows an example of a queue 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 store information about the queue.
12.36
18
Figure 12.13 Queue implementation
12.37
19
Operations on general linear lists
Although we can define many operations on a general linear
list, we discuss only six 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:
12.39
20
The delete operation
Deletion from a general list (Figure 12.16) also requires that
the list be searched to locate the data to be deleted. After the
location of the data is found, deletion can be done. The
following shows the format:
21
The traverse operation
Each of the previous operations involves a single element in
the list, randomly accessing the list. List traversal, on the
other hand, involves sequential access. It is an operation in
which all elements in the list are processed one by one. The
following shows the format:
12.43
12.44
22
General linear list ADT
We define a general linear list as an ADT as shown below:
12.45
Example 12.7
Figure 12.18 shows a segment of an algorithm that applies the
previously defined operations on a list L. Note that the third and
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
value
3 because it is not in the list.
12.46
Figure 12. 18 Example 12.7
23
General linear list applications
General linear lists are used in situations in which the
elements are accessed randomly or sequentially. For
example, in a college a linear list can be used to store
information about students who are enrolled in each
semester.
12.47
Example 12.8
A college has a general linear list that holds information about the
students and that each data element is a record with three fields: fields: ID,
Name and Grade. Algorithm 12.4 shows an algorithm that helps a
professor to change the grade for a student.
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 the
changed element back into the list. The element holds the whole record
for the student, and the target is the ID used to search the list.
list.
12.48
24
Example 12.8 (Continued)
12.49
Example 12.9
Continuing with Example 12.8, assume that the tutor wants to
semester.
print the record of all students at the end of the semester.
Algorithm 12.5 can do this job. We assume that there is an
algorithm called Print that prints the contents of the record. For
For
each node, the list traverse calls the Print algorithm and passes
passes
the data to be printed to it.
12.50
25
General linear list implementation
At the ADT level, we use the list and its six operations but at
the implementation level we need to choose a data structure
to implement it. A general list ADT can be implemented
using either an array or a linked list. Figure 12.19 shows
an example of a list ADT with five items. The figure also
shows how we can implement it.
12.51
26
12-5 TREES
12.54
27
Each node in a tree may have a subtree. The subtree of each
node includes one of its children and all descendents of that
child. Figure 12.21 shows all subtrees for the tree in Figure
12.20.
John
Mary Rom
12.56
28
12-6 BINARY TREES
12.58
29
Figure 12.23 shows eight trees, the first of which is an empty
binary tree (sometimes called a null binary tree).
- 6
35 9
12.60
30
Binary tree traversals
A binary tree traversal requires that each node of the tree
be processed once and only once in a predetermined
sequence. The two general approaches to the traversal
sequence are depth-first and breadth-first traversal.
Example 12.10
Figure 12.25 shows how we visit each node in a tree using preorder
traversal.
traversal. The figure also shows the walking order. In preorder
traversal we visit a node when we pass from its left side. The nodes
nodes
are visited in this order: A, B, C, D, E, F.
31
Example 12.11
Figure 12.26 shows how we visit each node in a tree using breadth-
breadth-
first traversal.
traversal. The figure also shows the walking order. The traversal
order is A, B, E, C, D, F.
Huffman coding
Huffman coding is a compression technique that uses
binary trees to generate a variable length binary code from
a string of symbols. We discuss Huffman coding in detail in
Chapter 15.
12.64
32
Expression trees
An arithmetic expression can be represented in three
different formats: infix, postfix and prefix. In an infix
notation, the operator comes between the two operands. In
postfix notation, the operator comes after its two operands,
and in prefix notation it comes before the two operands.
Prefix: +AB
Infix: A+B
Postfix: AB+
12.65
Root
- 6
35 9
12.66
33
Figure 12.27 Expression tree
12.67
Root
78
55 92
42 60 119
12.68
34
12-7 BINARY SEARCH TREES
Example 12.12
Figure 12.29 shows some binary trees that are BSTs and some that
are not. Note that a tree is a BST if all its subtrees are BSTs and
the whole tree is also a BST.
BST.
35
A very interesting property of a BST is that if we apply the
inorder traversal of a binary tree, the elements that are visited
are sorted in ascending order. For example, the three BSTs in
Figure 12.29, when traversed in order, give the lists
(3, 6, 17), (17, 19) and (3, 6, 14, 17, 19).
12.71
36
Binary search tree ADTs
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 BST lists than general linear lists
today. The reason is that searching a BST is more efficient
than searching a linear list: a general linear list uses
sequential searching, but BSTs use a version of binary
search.
12.73
BST implementation
BSTs can be implemented using either arrays or linked lists.
However, linked list structures are more common and
more efficient. The implementation uses nodes with two
pointers, left and right.
37
12-8 GRAPHS
38
Example 12.13
Example 12.14
Another application of graphs is in computer networks (Chapter 6).
The vertices can represent the nodes or hubs, the edges can
represent the route.
route. Each edge can have a weight that defines the
cost of reaching from one hub to an adjacent hub. A router can use
use
graph algorithms to find the shortest path between itself and the
the
final destination of a packet.
12.77
39