chapter 15 lists - bumsoo kim · 15-2 general linear lists a general linear list is a list in which...

128
Computer Science: A Structured Programming Approach Using C 1 Objectives To introduce the basic concepts of linked lists To introduce the basic concepts of stacks To introduce the basic concepts of queues To introduce the basic concepts of tree structures To introduce the basic concepts of graph structures Chapter 15 Lists

Upload: others

Post on 15-Oct-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 1

Objectives

❏ To introduce the basic concepts of linked lists

❏ To introduce the basic concepts of stacks

❏ To introduce the basic concepts of queues

❏ To introduce the basic concepts of tree structures

❏ To introduce the basic concepts of graph structures

Chapter 15

Lists

Page 2: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 2

FIGURE 15-1 Lists

Page 3: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 3

15-1 List Implementations

The C language does not provide any list structures or

implementations. When we need them, we must

provide the structures and functions for them.

Traditionally, two data types, arrays and pointers, are

used for their implementation.

Array Implementation

Linked List Implementation

Pointers to Linked Lists

Topics discussed in this section:

Page 4: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Array Implementation

How?

Pros and cons?

Computer Science: A Structured Programming Approach Using C 4

Page 5: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Linked List Implementation

Linked list

An ordered collection of data in which each element contains the location of the next element or elements

A node contains data and links

Linear and non-linear structures

Pros and cons over the array?

Computer Science: A Structured Programming Approach Using C 5

Page 6: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 6

FIGURE 15-2 Linked Lists

Page 7: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 7

FIGURE 15-3 Nodes

Page 8: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 8

FIGURE 15-4 Linked List Node Structures

Page 9: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 9

15-2 General Linear Lists

A general linear list is a list in which operations, such

as retrievals, insertions, changes, and deletions, can be

done anywhere in the list, that is, at the beginning, in

the middle, or at the end of the list..

Insert a Node

Delete a Node

Locating Data in Linear Lists

Traversing Linear Lists

Building a Linear List

Topics discussed in this section:

Page 10: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 10

FIGURE 15-5 Pointer Combinations for Insert

Page 11: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 11

FIGURE 15-6 Insert Node to Empty List

Page 12: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 12

FIGURE 15-7 Insert Node at Beginning

Page 13: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 13

FIGURE 15-8 Insert Node in Middle

Page 14: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 14

FIGURE 15-9 Insert Node at End

Page 15: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 15

PROGRAM 15-1 Insert a Node

Page 16: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 16

PROGRAM 15-1 Insert a Node

Page 17: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 17

FIGURE 15-10 Delete First Node

Page 18: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 18

FIGURE 15-11 Delete—General Case

Page 19: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 19

PROGRAM 15-2 Delete a Node

Page 20: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 20

Table 15-1 Linear List Search Results

Page 21: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 21

FIGURE 15-12 Search Results

Page 22: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 22

PROGRAM 15-3 Search Linear List

Page 23: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 23

PROGRAM 15-3 Search Linear List

Page 24: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 24

FIGURE 15-13 Linear List Traversal

Page 25: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 25

PROGRAM 15-4 Print Linear List

Page 26: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 26

PROGRAM 15-5 Average Linear List

Page 27: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 27

FIGURE 15-14 Design for Inserting a Node in a List

Page 28: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 28

PROGRAM 15-6 Build List

Page 29: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 29

PROGRAM 15-6 Build List

Page 30: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 30

FIGURE 15-15 Design for Remove Node

Page 31: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 31

PROGRAM 15-7 Delete Key

Page 32: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 32

PROGRAM 15-7 Delete Key

Page 33: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 33

FIGURE 15-16 Link List Test Driver

Page 34: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 34

PROGRAM 15-8 Test Driver for Link List

Page 35: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 35

PROGRAM 15-8 Test Driver for Link List

Page 36: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 36

PROGRAM 15-8 Test Driver for Link List

Page 37: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

PROGRAM 15-8 Test Driver for Link List

Computer Science: A Structured Programming Approach Using C 37

Page 38: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

PROGRAM 15-8 Test Driver for Link List

Computer Science: A Structured Programming Approach Using C 38

Page 39: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 39

15-3 Stacks

A stack is a linear list in which all additions and

deletions are restricted to one end, called the top.

Stacks are known as the last in–first out (LIFO) data

structure.

Stack Structures

Stack Algorithms

Stack Demonstration

Topics discussed in this section:

Page 40: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 40

A stack is a last in–first out (LIFO) data structure in which

all insertions and deletions are restricted

to one end, called the top.

Note

Page 41: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 41

FIGURE 15-17 Stack

Page 42: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 42

FIGURE 15-18 Conceptual and Physical Stack Implementations

Page 43: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 43

FIGURE 15-19 Stack Data Structure

Page 44: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 44

FIGURE 15-20 Streams

Page 45: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 45

PROGRAM 15-9 Push Stack

Page 46: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 46

PROGRAM 15-9 Push Stack

Page 47: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 47

FIGURE 15-21 Pop Stack Example

Page 48: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 48

PROGRAM 15-10 Pop Stack

Page 49: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 49

PROGRAM 15-10 Pop Stack

Page 50: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 50

FIGURE 15-22 Design for Basic Stack Program

Page 51: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 51

PROGRAM 15-11 Simple Stack Application Program

Page 52: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 52

PROGRAM 15-11 Simple Stack Application Program

Page 53: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 53

PROGRAM 15-11 Simple Stack Application Program

Page 54: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 54

PROGRAM 15-12 Insert Data

Page 55: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 55

PROGRAM 15-12 Insert Data

Page 56: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 56

PROGRAM 15-13 Print Stack

Page 57: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 57

15-4 Queues

A queue is a linear list in which data can be inserted

only at one end, called the rear, and deleted from the

other end, called the front.

Queue Operations

Queue Linked List Design

Queue Functions

Queue Demonstration

Topics discussed in this section:

Page 58: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 58

A queue is a linear list in which data can be inserted at one

end, called the rear, and deleted from the other end,

called the front. It is a first in–first out

(FIFO) restricted data structure.

Note

Page 59: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 59

FIGURE 15-23 Queue Concept

Page 60: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 60

Enqueue inserts an element at the rear of the queue.

Note

Page 61: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 61

FIGURE 15-24 Enqueue

Page 62: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 62

Dequeue deletes an element at the front of the queue.

Note

Page 63: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 63

FIGURE 15-25 Dequeue

Page 64: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 64

FIGURE 15-26 Conceptual and Physical Queue Implementations

Page 65: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 65

FIGURE 15-27 Queue Data Structure

Page 66: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 66

FIGURE 15-28 Enqueue Example

Page 67: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 67

PROGRAM 15-14 Enqueue

Page 68: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 68

PROGRAM 15-14 Enqueue

Page 69: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 69

FIGURE 15-29 Dequeue Examples

Page 70: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 70

PROGRAM 15-15 Dequeue

Page 71: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 71

PROGRAM 15-15 Dequeue

Page 72: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 72

PROGRAM 15-16 Simple Queue Demonstration

Page 73: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 73

PROGRAM 15-16 Simple Queue Demonstration

Page 74: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 74

PROGRAM 15-16 Simple Queue Demonstration

Page 75: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 75

PROGRAM 15-17 Insert Data

Page 76: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 76

PROGRAM 15-17 Insert Data

Page 77: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 77

PROGRAM 15-18 Print Queue

Page 78: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 78

15-5 Trees

Trees are used extensively in computer science to

represent algebraic formulas; as an efficient method

for searching large, dynamic lists; and for such

diverse applications as artificial intelligence systems

and encoding algorithms.

Basic Tree Concepts

Terminology

Binary Trees

Binary Search Trees

Binary Tree Example

Topics discussed in this section:

Page 79: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 79

A tree consists of a finite set of elements, called nodes, and a

finite set of directed lines, called branches, that

connect the nodes.

Note

Page 80: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 80

FIGURE 15-30 Tree

Page 81: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 81

FIGURE 15-31 Tree Nomenclature

Page 82: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 82

The level of a node is its distance from the root. The height

of a tree is the level of the leaf in the longest path

from the root plus 1.

Note

Page 83: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 83

FIGURE 15-32 Subtrees

Page 84: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 84

A tree is a set of nodes that either:

1. Is empty, or

2. Has a designated node, called the root, from which

hierarchically descend zero or more subtrees, which are

also trees

Note

Page 85: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 85

FIGURE 15-33 Collection of Binary Trees

Page 86: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 86

FIGURE 15-34 Binary Tree Data Structure

Page 87: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 87

FIGURE 15-35 Binary Tree Traversals

Page 88: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 88

FIGURE 15-36 Binary Tree for Traversals

Page 89: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 89

In the preorder traversal, the root is processed first,

before its subtrees.

Note

Page 90: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 90

PROGRAM 15-19 Preorder Traversal of a Binary Tree

Page 91: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 91

FIGURE 15-37 Preorder Traversal—A B C D E F

Page 92: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 92

FIGURE 15-38 Algorithmic Traversal of Binary Tree

Page 93: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 93

PROGRAM 15-20 Inorder Traversal of a Binary Tree

Page 94: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 94

FIGURE 15-39 Inorder Traversal—C B D A E F

Page 95: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 95

In the inorder traversal, the root is processed

between its subtrees.

Note

Page 96: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 96

In a binary search tree, the left subtree contains key values

less than the root, and the right subtree contains

key values greater than or equal to the root.

Note

Page 97: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 97

FIGURE 15-40 Binary Search Tree

Page 98: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 98

FIGURE 15-41 Valid Binary Search Trees

Page 99: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 99

FIGURE 15-42 Invalid Binary Search Trees

Page 100: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 100

All BST insertions take place at a leaf or a leaflike node.

Note

Page 101: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 101

FIGURE 15-43 BST Insertion

Page 102: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 102

PROGRAM 15-21 Binary Tree Insert Function

Page 103: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 103

PROGRAM 15-21 Binary Tree Insert Function

Page 104: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 104

FIGURE 15-44 Binary Tree Example

Page 105: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 105

PROGRAM 15-22 Binary Tree Example

Page 106: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 106

PROGRAM 15-22 Binary Tree Example

Page 107: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 107

PROGRAM 15-22 Binary Tree Example

Page 108: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 108

PROGRAM 15-22 Binary Tree Example

Page 109: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 109

15-6 Graphs

A graph is a collection of nodes, called vertices, and a

collection of segments, called lines, connecting pairs

of vertices. In other words, a graph consists of two sets,

a set of vertices and a set of lines.

Graph Traversal

Topics discussed in this section:

Page 110: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 110

FIGURE 15-45 Directed and Undirected Graphs

Page 111: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 111

Graphs may be directed or undirected. In a directed graph,

each line, called an arc, has a direction indicating

how it may be traversed. In an undirected graph,

the line is known as an edge, and it may

be traversed in either direction.

Note

Page 112: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 112

A file is an external

collection of related data treated as a unit.

Note

Page 113: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 113

FIGURE 15-46 Cycles and Loops

Page 114: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 114

FIGURE 15-47 Connected and Disjoint Graphs

Page 115: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 115

FIGURE 15-48 Depth-first Traversal of a Tree

Page 116: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 116

In the depth-first traversal, all of a node’s descendents are

processed before moving to an adjacent node.

Note

Page 117: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 117

FIGURE 15-49 Depth-first Traversal of a Graph

Page 118: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 118

FIGURE 15-50 Breadth-first Traversal of a Tree

Page 119: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 119

FIGURE 15-51 Breadth-first Traversal of a Graph

Page 120: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 120

In the breadth-first traversal, all adjacent

vertices are processed before processing

the descendents of a vertex.

Note

Page 121: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 121

15-7 Software Engineering

Because lists are useful structures, programmers use

them in many applications. Rather than rewrite their

functions each time we need them, we can write

functions once and put them in a library. The name

given to a complete set of these functions is abstract

data type (ADT).

Atomic and Composite Data

Data Structure and Abstract Data Type

A Model for an Abstract Data Type

ADT Data Structure

Topics discussed in this section:

Page 122: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 122

Atomic Data Type

1. A set of values.

2. A set of operations on the values.

Note

Page 123: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 123

Data Structure

1. A combination of elements, each of which is either a data

type or another data structure.

2. A set of associations or relationships (structure) involving

the combined elements.

Note

Page 124: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 124

Table 15-2 Two Structures

Page 125: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 125

In the concept of abstraction

We know what a data type can do.

How it is done is hidden.

Note

Page 126: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 126

FIGURE 15-52 Structures for Holding a List

Page 127: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 127

Abstract Data Type

1. Declaration of data.

2. Declaration of operations.

Note

Page 128: Chapter 15 Lists - Bumsoo Kim · 15-2 General Linear Lists A general linear list is a list in which operations, such as retrievals, insertions, changes, and deletions, can be done

Computer Science: A Structured Programming Approach Using C 128

FIGURE 15-53 Abstract Data Type Model