Chapter 08
Chapter 08
Chapter 08
Abstract Data
Types
and
Subprograms
Chapter Goals
3
Abstract Data Types
4
Three Views of Data
5
Three Views of Data
6
Three Views of Data
Implementation level
A specific representation of the structure that hold
the data items and the coding of the operations in
a programming language
7
Three Views of Data
8
Three Views of Data
Linked-based implementation
Objects in the container are not kept physically
together, but each item tells you where to go to get
the next one in the structure
Did you ever play treasure hunt, a game in which each clue
Stack
An abstract data type in which accesses are
made at only one end
– LIFO, which stands for Last In First Out
– The insert is called Push and the delete is
called Pop
Name three everyday
structures that are stacks
11
Stacks
12
Queues
Queue
An abstract data type in which items are entered at
one end and removed from the other end
– FIFO, for First In First Out
– No standard queue terminology Name
three
• Enqueue, Enque, Enq, Enter, and Insert
are used for the insertion operation everyday
• Dequeue, Deque, Deq, Delete, and Remove structures
are used for the deletion operation. that are
queues
13
Queues
Stack and
queue
visualized
as linked
structures
15
Lists
16
Array-Based Implementations
17
Linked Implementations
18
Algorithm for Creating and
Print Items in a List
20
Trees
Binary tree
A linked container with a unique starting
node called the root, in which each node is
capable of having two child nodes, and in
which a unique path (series of nodes) exists
from the root to every other node
A picture is worth a
thousands words…
22
Trees
Root node
Leaf node
Each node
is the root
of a subtree
made up of
its left and
right children
25
Binary Search Tree
26
Binary Search Tree
Trace the
nodes passed
as you search
for 18, 8, 5,
4, 9, and 15
What is special
about where
you are when
you find null?
28
Binary Search Tree
IsThere(tree, item)
IF (tree is null)
RETURN FALSE
ELSE
IF (item equals info(tree))
RETURN TRUE
ELSE
IF (item < info(tree))
IsThere(left(tree), item)
ELSE
IsThere(right(tree), item)
29
Building Binary Search Tree
30
Building Binary Search Tree
Insert(tree, item)
IF (tree is null)
Put item in tree
ELSE
IF (item < info(tree))
Insert (left(tree), item)
ELSE
Insert (right(tree), item)
31
Binary Search Tree
Print(tree)
If (tree is not null)
Print (left(tree))
Write info(tree)
Print (right(tree))
32
Graphs
Graph
A data structure that consists of a set of nodes
(called vertices) and a set of edges that relate the
nodes to each other
Undirected graph
A graph in which the edges have no direction
Directed graph (Digraph)
A graph in which each edge is directed from one
vertex to another (or the same) vertex
33
Graphs
34
Graphs
35
Graphs
36
Graph Algorithms
37
Depth First Search(startVertex, endVertex)
Set found to FALSE
Push(myStack, startVertex)
WHILE (NOT IsEmpty(myStack) AND NOT found)
Pop(myStack, tempVertex)
IF (tempVertex equals endVertex)
Write endVertex
Set found to TRUE
ELSE IF (tempVertex not visited)
Write tempVertex
Push all unvisited vertexes adjacent with tempVertex
Mark tempVertex as visited
IF (found)
Write "Path has been printed"
ELSE
Write "Path does not exist")
38
Can we get from Austin to
Washington?
39
Can we get from Austin to
Washington?
40
Breadth-First Search
43
Breadth-First Search Traveling
from Austin to Washington, DC
44
Subprogram Statements
Remember?
45
Subprogram Statements
What if the subprogram needs data from the
calling unit?
Parameters
Identifiers listed in parentheses beside the
subprogram declaration; sometimes called formal
parameters
Arguments
Identifiers listed in parentheses on the
subprogram call; sometimes called actual
parameters
46
Subprogram Statements
Value parameter
A parameter that expects a copy of its
argument to be passed by the calling unit
Reference parameter
A parameter that expects the address of its
argument to be passed by the calling unit
47
Subprogram Statements
Think of arguments as being placed on a message board
48
Subprogram Statements
Insert(list, item) // Subprogram definition
Set list.values[length-1] to item
Set list.length to list.length + 1
49
Ethical Issues
Workplace Monitoring
50
Who am I?
In the 1940s, the new
computer architecture
I developed
revolutionized the
design of computers.
Today’s computers are
referred to as [my last
name] machines
because the
architectural principles
I described have proven
very successful.
Courtesy of the U.S. Department of Energy.
51
Do you know?
52