SlideShare a Scribd company logo
IN2243
Foundations of Computer
Science
Instructor
Dr. Muhammad Waqar
Chapter 12
Abstract Data Types
3
Background
Tests
• 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.
• For example, to find the sum of a list of numbers, we should select the type
for the number (integer or real) and define the operation (addition).
• The definition of the data type and the definition of the operation 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 word, the user of an ADT needs only to know that a set of
operations are available for the data type but does not need to know how
they are applied.
4
Simple ADTs
Tests
• Many programming languages already define some simple ADTs as integral
parts of the language. For example, the C language defines a simple ADT
called an integer. The type of this ADT is integer with predefined ranges. C
also defines several operations that can be applied on this data type
(addition, subtraction, multiplication, division, and so on).
• The programmer, however, does not need to know how these operations
are actually implemented. For example, the programmer uses the
expression z ← x + y and expects the value of x (an integer) to be added to
the value of y (an integer) and the result to be named z (an integer). The
programmer does not need to know how the addition is performed.
5
Complex ADTs
Tests
• 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.
• To be efficient, these ADTs should be created and stored in the library of the
computer to be used. The user of a list, for example, should only need to
know what operations are available for the list, not how these operations
are performed.
• Therefore, with an ADT, users are not concerned with how the task is done,
but rather with what it can do. This generalization of operations with
unspecified implementations is known as abstraction. We abstract the
essence of the process and leave the implementation details hidden.
6
Definition
Tests
• 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
7
Model for an abstract data type
Tests
• The ADT model is shown in Figure 12.1. The colored area with an irregular
outline represents the ADT.
• Inside the ADT are two different parts of the model: data structure and
operations (public and private). The application program can only access the
public operations through the interface.
• An interface is a list of public operations and data to be passed to or
returned from those operations.
• The private operations are for internal use by the ADT.
• The data structures, such as arrays and linked lists, are inside the ADT and
are used by the public and private operations.
8
Model for an abstract data type
Tests
9
STACKS
Tests
• A stack is a restricted linear list in which all additions and deletions are
made at one end, the top.
• If we insert a series of data into a stack and then remove it, the order of the
data is reversed.
• Data input as 5, 10, 15, 20, for example, would be removed as 20, 15, 10,
and 5. This reversing attribute is why stacks are known as a last in, first out
(LIFO) data structure.
10
Operations on stacks
Tests
• Although we can define many operations for a stack, there are four basic
operations, stack, push, pop, and empty, which we define in this lecture.
The stack operation
• The stack operation creates an empty stack. The following shows the
format: stack (stackName)
11
Operations on stacks
Tests
The push operation
• The push operation inserts an item at the top of the stack. The following
shows the format: push (stackName, dataItem)
12
Operations on stacks
Tests
The pop operation
• The pop operation deletes the item at the top of the stack. The following
shows the format: pop (stackName, dataItem)
13
Operations on stacks
Tests
The empty operation
• The empty operation checks the status of the stack. The following shows
the format: empty (stackName)
• The stackName is the name of the stack. This operation returns true if the
stack is empty and false if the stack is not empty.
14
Stack ADT
Tests
15
Example 12.1
Tests
16
Stack applications
Tests
Reversing data items
• Reversing data items requires that a given set of data items be reordered so
that the first and last items are exchanged, with all of the positions between
the first and last being relatively exchanged also.
• For example, the list (2, 4, 7, 1, 6, 8) becomes (8, 6, 1, 7, 4, 2).
17
Example 12.2
Tests
18
Stack applications
Tests
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 two expressions are evaluated differently because of the
parentheses in the second expression: 3 x 6 + 2 = 20, 3 x (6 + 2) = 24
• In the first expression, the multiplication operator has precedence over the
addition operator—it is calculated first. In the second expression, the
parentheses ignore the precedence, so the addition is calculated first.
• When we type an expression with a lot of parentheses, we often forget to
pair the parentheses. One 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.
19
Stack applications
Tests
20
Stack applications
Tests
21
Stack implementation
Tests
• Stack ADT 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.
22
QUEUES
Tests
• A queue is a linear list in which data can only be inserted at one end, called
the rear, and deleted from the other end, called the front.
• These restrictions ensure that the data are processed through the queue in
the order in which it is received. In other words, a queue is a first in, first
out (FIFO) structure.
23
Operations on queues
Tests
• 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 (queueName)
• queueName is the name of the queue to be created.
24
Operations on queues
Tests
The enqueue operation
• The enqueue operation inserts an item at the rear of the queue. The
following shows the format: enqueue (queueName, dataItem)
• queueName is the name of the queue and dataItem is the data to be
inserted at the rear of the queue.
25
Operations on queues
Tests
The dequeue operation
• The dequeue operation deletes the item at the front of the queue. The
following shows the format: dequeue (queueName, dataItem)
• queueName is the name of the queue and dataItem is the data that is
deleted from the queue. The deleted item can be used by the application
program or can be just discarded.
26
Operations on queues
Tests
The empty operation
• The empty operation checks the status of the queue. The following shows
the format: empty (queueName)
• queueName is the name of the queue. This operation returns true if the
queue is empty and false if the queue is not empty.
27
Queue ADT
Tests
28
Example 12.4
Tests
29
Queue applications
Tests
• 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.
30
Example 12.5
Tests
31
Example 12.5
Tests
32
Example 12.6
Tests
• Another common application of a queue is to adjust and create a balance
between a fast producer of data and a slow consumer of data.
• For example, assume that a CPU is connected to a printer. The speed of a
printer is not comparable with the speed of a CPU. If the CPU waits for the
printer to print some data created by the CPU, the CPU would be idle for a
long time.
• The solution is a queue. The CPU creates as many chunks of data as the
queue can hold and sends them to the queue. The CPU is now free to do
other jobs.
• The chunks are dequeued slowly and printed by the printer. The queue used
for this purpose is normally referred to as a spool queue.
33
Queue implementation
Tests
• At the ADT level, we use the queue and its four operations (queue,
enqueue, dequeue, and empty): 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.
• In the array implementation we have a record with three fields. The first
field can be used to store information about the queue: we have used this
as a count field that shows the current number of data items in the queue.
The second field is an integer that holds the index of the front element. The
third field is also an integer, which holds the index of the rear element.
• The linked list implementation is similar: we have an extra node that has the
name of the queue. 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.
34
Queue implementation
Tests
35
GENERAL LINEAR LISTS
Tests
• Stacks and queues defined in the two previous sections are restricted linear
lists.
• A general 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.
36
GENERAL LINEAR LISTS PROPERTIES
Tests
We define a general linear list as a collection of elements with the following
properties:
• The elements are of the same type.
• The elements are arranged sequentially, which means that there is a first
element and a last element.
• Each element except the first has a unique predecessor, each element
except the last has a unique successor.
• Each element is a record with a key field.
• The elements are sorted based on the key value.
37
Operations on general linear lists
Tests
• Although we can define many operations on a general linear list, we discuss
only six common operations are discussed here: list, insert, delete, retrieve,
traverse, and empty.
The list operation
• The list operation creates an empty list. The following shows the format:
• List (listName)
• listName is the name of the general linear list to be created. This operation
returns an empty list.
38
Operations on general linear lists
Tests
The insert operation
• Since 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 elements are to be placed, searching is needed. The
following shows the format: insert (listName, element)
39
Operations on general linear lists
Tests
The delete operation
• Deletion from a general list 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: delete (listName, target,
element)
• target is a data value of the same type as the key of the elements in the list.
If an element with the key value equal to the target is found, that element is
deleted.
40
Operations on general linear lists
Tests
The retrieve operation
• By retrieval, we mean access of a single element. Like insertion and
deletion, the general list should be first searched, and if the data is found, it
can be retrieved. The format of the retrieve operation is: retrieve (listName,
target, element)
• target is a data value of the same type as the key of the elements in the list.
41
Operations on general linear lists
Tests
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: traverse
(listName, action)
The empty operation
The empty operation checks the status of the list. The following shows the
format: empty (listName)
listName is the name of the list. This operation returns true if the list is empty,
or false if the list is not empty.
42
General linear list ADT
Tests
43
Example 12.7
Tests
44
Example 12.8
Tests
45
Example 12.9
Tests
46
General linear list implementation
Tests
• At the ADT level, we use the list and its six operations (list, insert, delete,
retrieve, traverse, and empty), 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.
47
TREES
Tests
• A tree consists of a finite set of elements, called nodes (or vertices), and a
finite set of directed lines, called arcs, that connect pairs of the nodes.
• If the tree is not empty, one of the nodes, called the root, has no incoming
arcs. The other nodes in a tree can be reached from the root by following a
unique path, which is a sequence of consecutive arcs.
• Tree structures are normally drawn upside down with the root at the top
48
Node Types
Tests
• We can divide the vertices in a tree into three categories: the root, leaves,
and the internal nodes.
• A node that is directly accessible (through a single arc) from a given node is
called the child: the node from which the child is directly accessible is called
a parent. Nodes with a common parent are called siblings.
• Descendants of a node are all nodes that can be reached by that node, and
a node from which all descendants can be reached is called an ancestor.
49
Sub Trees
Tests
• The subtree of each node includes one of its children and all descendants of
that child.
50
Binary trees
Tests
• A binary tree is a tree in which no node can have more than two subtrees.
In other words, a node can have zero, one, or two subtrees.
• These subtrees are designated as the left subtree and the right subtree.
51
Recursive definition of binary trees
Tests
• A binary tree is either empty or consists of a node, root, with two subtrees,
in which each subtree is also a binary tree.
52
Operations on binary trees
Tests
• The six most common operations defined for a binary tree are tree (creates
an empty tree), insert, delete, retrieve, empty and traversal. The first five
are complex and beyond the scope of this course. We will discuss binary
tree traversal only.
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.
53
Operations on binary trees
Tests
Depth-first traversals
• Given that a binary tree consists of a root, a left subtree, and a right
subtree, we can define six different depth-first traversal sequences.
• Computer scientists have assigned standard names to three of these
sequences in the literature: the other three are unnamed but are easily
derived.
54
Operations on binary trees
Tests
Depth-first traversals
• Preorder traversal. In preorder traversal the root node is processed first,
followed by the left subtree and then the right subtree. The prefix pre
indicates that the root node is processed before the subtrees.
• Inorder traversal: In inorder traversal the left subtree is processed first, then
the root node, and finally the right subtree. The prefix in indicates that the
root node is processed between the subtrees.
• Postorder traversal: In postorder traversal the root node is processed after
the left and right subtrees have been processed. The prefix post indicates
that the root is processed after the subtrees.
55
Example 12.10
Tests
• Figure 12.25 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, B, C, D, E, F.
56
Breadth-first traversals
Tests
• In breadth-first traversal of a binary tree we process all the children of a
node before proceeding with the next generation. As with depth-first
traversals, we can trace the traversal with a walk.
57
Binary tree applications
Tests
• Binary trees have many applications in computer science. We will discuss
only two of them: Huffman coding and expression trees.
Huffman coding
• Huffman coding is a compression technique that uses binary trees to
generate a variable length binary code from a string of symbols.
58
Binary tree applications
Tests
Expression trees
• An arithmetic expression can be represented in three different formats:
infix, postfix, and prefix.
59
Binary search trees
Tests
• A binary search tree (BST) is a binary tree with one extra property: the key
value of each node is greater than the key values of all nodes in each left
subtree and smaller than the value of all nodes in each right subtree.
60
Binary search trees
Tests
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.
61
In order traversal of Binary tree
Tests
• A very interesting property of a BST is that if we apply the inorder traversal
of a binary search tree, the elements that are visited are sorted in
ascending order.
62
BST implementation
Tests
• BSTs can be implemented using either arrays or linked lists. However, linked
list structures are more common and more efficient.
• A linear implementation uses nodes with two pointers, left and right. The
left pointer points to the left subtree and the right pointer points to the
right subtree. If the left subtree is empty, the left pointer is null: if the right
subtree is empty, the right pointer is null.
63
GRAPHS
Tests
• A graph is an ADT made of a set of nodes, called vertices, and set of lines
connecting the vertices, called edges or arcs. Whereas 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.
• Graphs may be either directed or undirected. In a directed graph, or
digraph, each edge, which connects two vertices, has a direction (shown in
the figure by an arrowhead) from one vertex to the other. In an undirected
graph, there is no direction.
64
GRAPHS Examples
Tests
Example 12.13
• 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 connect them. If we want to show the
distances between the cities, we can use weighted graphs, in which each
edge has a weight that represent the distance between two cities
connected by that edge.
Example 12.14
• Another application of graphs is in computer networks. The vertices can
represent the nodes or hubs; the edges can represent the route. Each edge
can have a weight that defines the cost of reaching from one hub to the
adjacent hub. A router can use graph algorithms to find the shortest path
between itself and the final destination of a packet.
Thank You

More Related Content

PPTX
II B.Sc IT DATA STRUCTURES.pptx
PPTX
ADS Introduction
PDF
U nit i data structure-converted
PPTX
UNIT 1.pptx
PPTX
UNIT 1 Memory ManagementMemory Management.pptx
PDF
introduction of Data structure with example
PPT
algo 1.ppt
PPT
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
II B.Sc IT DATA STRUCTURES.pptx
ADS Introduction
U nit i data structure-converted
UNIT 1.pptx
UNIT 1 Memory ManagementMemory Management.pptx
introduction of Data structure with example
algo 1.ppt
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4

Similar to Abstract Data Types (ADTs) in Data Structures (20)

PPT
Intro_2.ppt
PPT
Intro.ppt
PPT
Intro.ppt
PPT
Data structure
PPTX
Data Structures and Algorithms: introduction
PPTX
PPTX
Chapter 1 _edited.pptx.software engineering
PPTX
Introduction to DS.pptx
PPTX
b,Sc it data structure.pptx
PPTX
Data Structures - Lecture 1 - Unit 1.pptx
PPTX
Lecture 1 and 2
PPTX
b,Sc it data structure.pptx
PPTX
datastructuresandalgorithm-module1-230307012644-4c895c84.pptx
PPT
1. Data structures introduction
PDF
DSJ_Unit I & II.pdf
PPTX
Introduction to data_structure
PPTX
Data structure and algorithm using java
PPTX
Data structure Unit-I Part A
PPT
b,Sc it data structure.ppt
PPTX
1-Introduction to Data Structures beginner.pptx
Intro_2.ppt
Intro.ppt
Intro.ppt
Data structure
Data Structures and Algorithms: introduction
Chapter 1 _edited.pptx.software engineering
Introduction to DS.pptx
b,Sc it data structure.pptx
Data Structures - Lecture 1 - Unit 1.pptx
Lecture 1 and 2
b,Sc it data structure.pptx
datastructuresandalgorithm-module1-230307012644-4c895c84.pptx
1. Data structures introduction
DSJ_Unit I & II.pdf
Introduction to data_structure
Data structure and algorithm using java
Data structure Unit-I Part A
b,Sc it data structure.ppt
1-Introduction to Data Structures beginner.pptx
Ad

More from mwaslam2303 (7)

PPTX
File Strucutres and Access in Data Structures
PPTX
Abstract Data Types (ADTs) in Data Structures
PPTX
File Structures and Access in Data Structures
PPTX
File Structures and File Access in Data Structures
PPTX
Abstract Data Types (ADTs) in Data Structures
PPTX
Introduction to Microsoft Word Documents
PPTX
Introduction to Microsoft Office Applications
File Strucutres and Access in Data Structures
Abstract Data Types (ADTs) in Data Structures
File Structures and Access in Data Structures
File Structures and File Access in Data Structures
Abstract Data Types (ADTs) in Data Structures
Introduction to Microsoft Word Documents
Introduction to Microsoft Office Applications
Ad

Recently uploaded (20)

PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
Well-logging-methods_new................
PPT
Project quality management in manufacturing
PPTX
web development for engineering and engineering
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
Digital Logic Computer Design lecture notes
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
Sustainable Sites - Green Building Construction
PPTX
Welding lecture in detail for understanding
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Well-logging-methods_new................
Project quality management in manufacturing
web development for engineering and engineering
CYBER-CRIMES AND SECURITY A guide to understanding
OOP with Java - Java Introduction (Basics)
bas. eng. economics group 4 presentation 1.pptx
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Internet of Things (IOT) - A guide to understanding
Digital Logic Computer Design lecture notes
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Sustainable Sites - Green Building Construction
Welding lecture in detail for understanding
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS

Abstract Data Types (ADTs) in Data Structures

  • 3. 3 Background Tests • 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. • For example, to find the sum of a list of numbers, we should select the type for the number (integer or real) and define the operation (addition). • The definition of the data type and the definition of the operation 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 word, the user of an ADT needs only to know that a set of operations are available for the data type but does not need to know how they are applied.
  • 4. 4 Simple ADTs Tests • Many programming languages already define some simple ADTs as integral parts of the language. For example, the C language defines a simple ADT called an integer. The type of this ADT is integer with predefined ranges. C also defines several operations that can be applied on this data type (addition, subtraction, multiplication, division, and so on). • The programmer, however, does not need to know how these operations are actually implemented. For example, the programmer uses the expression z ← x + y and expects the value of x (an integer) to be added to the value of y (an integer) and the result to be named z (an integer). The programmer does not need to know how the addition is performed.
  • 5. 5 Complex ADTs Tests • 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. • To be efficient, these ADTs should be created and stored in the library of the computer to be used. The user of a list, for example, should only need to know what operations are available for the list, not how these operations are performed. • Therefore, with an ADT, users are not concerned with how the task is done, but rather with what it can do. This generalization of operations with unspecified implementations is known as abstraction. We abstract the essence of the process and leave the implementation details hidden.
  • 6. 6 Definition Tests • 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
  • 7. 7 Model for an abstract data type Tests • The ADT model is shown in Figure 12.1. The colored area with an irregular outline represents the ADT. • Inside the ADT are two different parts of the model: data structure and operations (public and private). The application program can only access the public operations through the interface. • An interface is a list of public operations and data to be passed to or returned from those operations. • The private operations are for internal use by the ADT. • The data structures, such as arrays and linked lists, are inside the ADT and are used by the public and private operations.
  • 8. 8 Model for an abstract data type Tests
  • 9. 9 STACKS Tests • A stack is a restricted linear list in which all additions and deletions are made at one end, the top. • If we insert a series of data into a stack and then remove it, the order of the data is reversed. • Data input as 5, 10, 15, 20, for example, would be removed as 20, 15, 10, and 5. This reversing attribute is why stacks are known as a last in, first out (LIFO) data structure.
  • 10. 10 Operations on stacks Tests • Although we can define many operations for a stack, there are four basic operations, stack, push, pop, and empty, which we define in this lecture. The stack operation • The stack operation creates an empty stack. The following shows the format: stack (stackName)
  • 11. 11 Operations on stacks Tests The push operation • The push operation inserts an item at the top of the stack. The following shows the format: push (stackName, dataItem)
  • 12. 12 Operations on stacks Tests The pop operation • The pop operation deletes the item at the top of the stack. The following shows the format: pop (stackName, dataItem)
  • 13. 13 Operations on stacks Tests The empty operation • The empty operation checks the status of the stack. The following shows the format: empty (stackName) • The stackName is the name of the stack. This operation returns true if the stack is empty and false if the stack is not empty.
  • 16. 16 Stack applications Tests Reversing data items • Reversing data items requires that a given set of data items be reordered so that the first and last items are exchanged, with all of the positions between the first and last being relatively exchanged also. • For example, the list (2, 4, 7, 1, 6, 8) becomes (8, 6, 1, 7, 4, 2).
  • 18. 18 Stack applications Tests 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 two expressions are evaluated differently because of the parentheses in the second expression: 3 x 6 + 2 = 20, 3 x (6 + 2) = 24 • In the first expression, the multiplication operator has precedence over the addition operator—it is calculated first. In the second expression, the parentheses ignore the precedence, so the addition is calculated first. • When we type an expression with a lot of parentheses, we often forget to pair the parentheses. One 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.
  • 21. 21 Stack implementation Tests • Stack ADT 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.
  • 22. 22 QUEUES Tests • A queue is a linear list in which data can only be inserted at one end, called the rear, and deleted from the other end, called the front. • These restrictions ensure that the data are processed through the queue in the order in which it is received. In other words, a queue is a first in, first out (FIFO) structure.
  • 23. 23 Operations on queues Tests • 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 (queueName) • queueName is the name of the queue to be created.
  • 24. 24 Operations on queues Tests The enqueue operation • The enqueue operation inserts an item at the rear of the queue. The following shows the format: enqueue (queueName, dataItem) • queueName is the name of the queue and dataItem is the data to be inserted at the rear of the queue.
  • 25. 25 Operations on queues Tests The dequeue operation • The dequeue operation deletes the item at the front of the queue. The following shows the format: dequeue (queueName, dataItem) • queueName is the name of the queue and dataItem is the data that is deleted from the queue. The deleted item can be used by the application program or can be just discarded.
  • 26. 26 Operations on queues Tests The empty operation • The empty operation checks the status of the queue. The following shows the format: empty (queueName) • queueName is the name of the queue. This operation returns true if the queue is empty and false if the queue is not empty.
  • 29. 29 Queue applications Tests • 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.
  • 32. 32 Example 12.6 Tests • Another common application of a queue is to adjust and create a balance between a fast producer of data and a slow consumer of data. • For example, assume that a CPU is connected to a printer. The speed of a printer is not comparable with the speed of a CPU. If the CPU waits for the printer to print some data created by the CPU, the CPU would be idle for a long time. • The solution is a queue. The CPU creates as many chunks of data as the queue can hold and sends them to the queue. The CPU is now free to do other jobs. • The chunks are dequeued slowly and printed by the printer. The queue used for this purpose is normally referred to as a spool queue.
  • 33. 33 Queue implementation Tests • At the ADT level, we use the queue and its four operations (queue, enqueue, dequeue, and empty): 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. • In the array implementation we have a record with three fields. The first field can be used to store information about the queue: we have used this as a count field that shows the current number of data items in the queue. The second field is an integer that holds the index of the front element. The third field is also an integer, which holds the index of the rear element. • The linked list implementation is similar: we have an extra node that has the name of the queue. 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.
  • 35. 35 GENERAL LINEAR LISTS Tests • Stacks and queues defined in the two previous sections are restricted linear lists. • A general 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.
  • 36. 36 GENERAL LINEAR LISTS PROPERTIES Tests We define a general linear list as a collection of elements with the following properties: • The elements are of the same type. • The elements are arranged sequentially, which means that there is a first element and a last element. • Each element except the first has a unique predecessor, each element except the last has a unique successor. • Each element is a record with a key field. • The elements are sorted based on the key value.
  • 37. 37 Operations on general linear lists Tests • Although we can define many operations on a general linear list, we discuss only six common operations are discussed here: list, insert, delete, retrieve, traverse, and empty. The list operation • The list operation creates an empty list. The following shows the format: • List (listName) • listName is the name of the general linear list to be created. This operation returns an empty list.
  • 38. 38 Operations on general linear lists Tests The insert operation • Since 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 elements are to be placed, searching is needed. The following shows the format: insert (listName, element)
  • 39. 39 Operations on general linear lists Tests The delete operation • Deletion from a general list 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: delete (listName, target, element) • target is a data value of the same type as the key of the elements in the list. If an element with the key value equal to the target is found, that element is deleted.
  • 40. 40 Operations on general linear lists Tests The retrieve operation • By retrieval, we mean access of a single element. Like insertion and deletion, the general list should be first searched, and if the data is found, it can be retrieved. The format of the retrieve operation is: retrieve (listName, target, element) • target is a data value of the same type as the key of the elements in the list.
  • 41. 41 Operations on general linear lists Tests 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: traverse (listName, action) The empty operation The empty operation checks the status of the list. The following shows the format: empty (listName) listName is the name of the list. This operation returns true if the list is empty, or false if the list is not empty.
  • 46. 46 General linear list implementation Tests • At the ADT level, we use the list and its six operations (list, insert, delete, retrieve, traverse, and empty), 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.
  • 47. 47 TREES Tests • A tree consists of a finite set of elements, called nodes (or vertices), and a finite set of directed lines, called arcs, that connect pairs of the nodes. • If the tree is not empty, one of the nodes, called the root, has no incoming arcs. The other nodes in a tree can be reached from the root by following a unique path, which is a sequence of consecutive arcs. • Tree structures are normally drawn upside down with the root at the top
  • 48. 48 Node Types Tests • We can divide the vertices in a tree into three categories: the root, leaves, and the internal nodes. • A node that is directly accessible (through a single arc) from a given node is called the child: the node from which the child is directly accessible is called a parent. Nodes with a common parent are called siblings. • Descendants of a node are all nodes that can be reached by that node, and a node from which all descendants can be reached is called an ancestor.
  • 49. 49 Sub Trees Tests • The subtree of each node includes one of its children and all descendants of that child.
  • 50. 50 Binary trees Tests • A binary tree is a tree in which no node can have more than two subtrees. In other words, a node can have zero, one, or two subtrees. • These subtrees are designated as the left subtree and the right subtree.
  • 51. 51 Recursive definition of binary trees Tests • A binary tree is either empty or consists of a node, root, with two subtrees, in which each subtree is also a binary tree.
  • 52. 52 Operations on binary trees Tests • The six most common operations defined for a binary tree are tree (creates an empty tree), insert, delete, retrieve, empty and traversal. The first five are complex and beyond the scope of this course. We will discuss binary tree traversal only. 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.
  • 53. 53 Operations on binary trees Tests Depth-first traversals • Given that a binary tree consists of a root, a left subtree, and a right subtree, we can define six different depth-first traversal sequences. • Computer scientists have assigned standard names to three of these sequences in the literature: the other three are unnamed but are easily derived.
  • 54. 54 Operations on binary trees Tests Depth-first traversals • Preorder traversal. In preorder traversal the root node is processed first, followed by the left subtree and then the right subtree. The prefix pre indicates that the root node is processed before the subtrees. • Inorder traversal: In inorder traversal the left subtree is processed first, then the root node, and finally the right subtree. The prefix in indicates that the root node is processed between the subtrees. • Postorder traversal: In postorder traversal the root node is processed after the left and right subtrees have been processed. The prefix post indicates that the root is processed after the subtrees.
  • 55. 55 Example 12.10 Tests • Figure 12.25 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, B, C, D, E, F.
  • 56. 56 Breadth-first traversals Tests • In breadth-first traversal of a binary tree we process all the children of a node before proceeding with the next generation. As with depth-first traversals, we can trace the traversal with a walk.
  • 57. 57 Binary tree applications Tests • Binary trees have many applications in computer science. We will discuss only two of them: Huffman coding and expression trees. Huffman coding • Huffman coding is a compression technique that uses binary trees to generate a variable length binary code from a string of symbols.
  • 58. 58 Binary tree applications Tests Expression trees • An arithmetic expression can be represented in three different formats: infix, postfix, and prefix.
  • 59. 59 Binary search trees Tests • A binary search tree (BST) is a binary tree with one extra property: the key value of each node is greater than the key values of all nodes in each left subtree and smaller than the value of all nodes in each right subtree.
  • 60. 60 Binary search trees Tests 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.
  • 61. 61 In order traversal of Binary tree Tests • A very interesting property of a BST is that if we apply the inorder traversal of a binary search tree, the elements that are visited are sorted in ascending order.
  • 62. 62 BST implementation Tests • BSTs can be implemented using either arrays or linked lists. However, linked list structures are more common and more efficient. • A linear implementation uses nodes with two pointers, left and right. The left pointer points to the left subtree and the right pointer points to the right subtree. If the left subtree is empty, the left pointer is null: if the right subtree is empty, the right pointer is null.
  • 63. 63 GRAPHS Tests • A graph is an ADT made of a set of nodes, called vertices, and set of lines connecting the vertices, called edges or arcs. Whereas 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. • Graphs may be either directed or undirected. In a directed graph, or digraph, each edge, which connects two vertices, has a direction (shown in the figure by an arrowhead) from one vertex to the other. In an undirected graph, there is no direction.
  • 64. 64 GRAPHS Examples Tests Example 12.13 • 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 connect them. If we want to show the distances between the cities, we can use weighted graphs, in which each edge has a weight that represent the distance between two cities connected by that edge. Example 12.14 • Another application of graphs is in computer networks. The vertices can represent the nodes or hubs; the edges can represent the route. Each edge can have a weight that defines the cost of reaching from one hub to the adjacent hub. A router can use graph algorithms to find the shortest path between itself and the final destination of a packet.