Top Banner
Chapter 8 Abstract Data Types and Subprograms
47

Chapter 8 Abstract Data Types and Subprograms. What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation.

Dec 16, 2015

Download

Documents

Merry Fox
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 8 Abstract Data Types and Subprograms. What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation.

Chapter 8

Abstract Data Types and Subprograms

Page 2: Chapter 8 Abstract Data Types and Subprograms. What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation.

What is Computer Science?

• One More Definition of Computer Science:

“Computer Science is the Automation of Abstractions” – anonymous

9-2

Page 3: Chapter 8 Abstract Data Types and Subprograms. What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation.

More Simply Stated…

• Data• Instructions

• Data organization and Algorithm affect each other

9-3

Page 4: Chapter 8 Abstract Data Types and Subprograms. What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation.

Chapter Goals

• What is an Abstract Data Type?

• Concept of “The Separation of Interface from Implementation”

• array-based implementation • linked implementation

4

Page 5: Chapter 8 Abstract Data Types and Subprograms. What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation.

More Chapter Goals

Some Specific Common Abstract Data Types• arrays and lists• stacks and queues• binary trees and binary search trees• Graphs

Common Algorithms that Operate on these ADT’s• Tree Searches• Traveling Salesman Problem, etc

5

Page 6: Chapter 8 Abstract Data Types and Subprograms. What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation.

Abstract Data Types

Abstract data type A composite data type containing:•Data in a particular organization•Operations (algorithms) to operate on that data

Remember the most powerful tool for managing complexity?

6

Page 7: Chapter 8 Abstract Data Types and Subprograms. What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation.

Abstract Data Types

Abstract data type The goals are to:1)Reduce complexity thru abstraction2)Organize our data into various kinds of containers3)Think about our problem in terms of data and the operations (algorithms) that are done to them

7

Page 8: Chapter 8 Abstract Data Types and Subprograms. What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation.

Why do We Need ADT’s?

8Very DIFFICULT to write this without ADT’s

Page 9: Chapter 8 Abstract Data Types and Subprograms. What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation.

Why do We Need ADT’s?

9Almost IMPOSSIBLE to write this without ADT’s

Page 10: Chapter 8 Abstract Data Types and Subprograms. What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation.

Stacks

All operations occur at the

top

Page 11: Chapter 8 Abstract Data Types and Subprograms. What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation.

Stacks

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

11

Name some everydaystructures that are stacks

Page 12: Chapter 8 Abstract Data Types and Subprograms. What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation.

12

Stacks

WHILE (more data)

Read value

Push(myStack, value)

WHILE (NOT IsEmpty(myStack))

Pop(myStack, value)

Write value

Hand simulate this algorithm

Page 13: Chapter 8 Abstract Data Types and Subprograms. What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation.

Queues

All operations occur at the

front and back

Page 14: Chapter 8 Abstract Data Types and Subprograms. What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation.

QueuesQueue An abstract data type in which items are entered at one end and removed from the other end

– FIFO, for “First In First Out”

•EnQue: Get in line at rear

•Deque: Get served at front

14

Page 15: Chapter 8 Abstract Data Types and Subprograms. What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation.

Queues

WHILE (more data)

Read value

Enque(myQueue, value)

WHILE (NOT IsEmpty(myQueue))

Deque(myQueue, value)

Write value

Hand simulate this algorithm

Page 16: Chapter 8 Abstract Data Types and Subprograms. What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation.

Stacks and Queues(using a Linked Implementation)

16

Stack and queue visualized as linked structures

Page 17: Chapter 8 Abstract Data Types and Subprograms. What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation.

Implementation: What’s Inside?

There are several ways to implement any ADT

2 Common implementations use:1)An Array2)Linked Nodes

Here, we are concerned with the details inside the ADT

(Building the car instead of Driving the car)

17

Page 18: Chapter 8 Abstract Data Types and Subprograms. What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation.

ADT Implementations

Array-based implementationItems are in an array, physically next to each other in memory

Linked-based implementationItems are not next to each other in memory, instead each item points to the next item

18

Did you ever play treasure hunt, a game in which each clue

told you where to go to get the next clue?

Page 19: Chapter 8 Abstract Data Types and Subprograms. What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation.

Array-Based Implementations

19

Page 20: Chapter 8 Abstract Data Types and Subprograms. What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation.

Linked Implementations

20

Page 21: Chapter 8 Abstract Data Types and Subprograms. What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation.

Algorithm for Playing Solitaire

21

WHILE (deck not empty)

Pop the deckStack

Check for Aces

While (There are playStacks to check)

If(can place card)

Push card onto playStack

Else

push card onto usedStack

Does implementation matter at this point?

Page 22: Chapter 8 Abstract Data Types and Subprograms. What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation.

22

“Logical Level”

The algorithm that uses the list does not need to

know how it is implemented

We have written algorithms using a stack, a

queue, and a list without ever knowing the

internal workings of the operations on these

containers

Page 23: Chapter 8 Abstract Data Types and Subprograms. What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation.

Trees

Can represent more complex relationships between data

Page 24: Chapter 8 Abstract Data Types and Subprograms. What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation.

Trees

24

Root node

Node with two children

Node with right child

Leaf node

Node with left childWhat is the unique path to the node containing

5? 9? 7? …

Page 25: Chapter 8 Abstract Data Types and Subprograms. What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation.

Why Trees?

• Some real-world data is tree-like– Geneology Family trees– Management Hierarchies– File Systems (Folders etc)

• Treeses are easy to search

25

Page 26: Chapter 8 Abstract Data Types and Subprograms. What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation.

Binary Search Trees

Binary search treeEach “sub-tree” has the following property(s):

1.All sub-trees on one side are greater2.All sub-tress on the other side are smaller

26

Page 27: Chapter 8 Abstract Data Types and Subprograms. What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation.

Binary Search Tree

27Figure 8.7 A binary search tree

Each nodeis the root

of a subtreemade up ofits left and

right children

Prove that thistree is a BST

Page 28: Chapter 8 Abstract Data Types and Subprograms. What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation.

Binary Search Tree(A Look at Implementation Details)

28

Page 29: Chapter 8 Abstract Data Types and Subprograms. What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation.

Trees and Recursion

Like Mona Lisa,Trees have repeating

patterns at smaller levels

Page 30: Chapter 8 Abstract Data Types and Subprograms. What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation.

Recursive Binary Search Algorithm

30

Boolean BinSearch(node, item) If (node is null)

item does not existElse

If (item < node)BinSearch(node.leftchild, item)

Else BinSearch(node.rightchild, item)

Page 31: Chapter 8 Abstract Data Types and Subprograms. What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation.

Binary Search Tree

31

Page 32: Chapter 8 Abstract Data Types and Subprograms. What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation.

32

Another Binary Search Tree

Page 33: Chapter 8 Abstract Data Types and Subprograms. What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation.

33

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)

Page 34: Chapter 8 Abstract Data Types and Subprograms. What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation.

Graphs

Can represent more complex relationships between data

Page 35: Chapter 8 Abstract Data Types and Subprograms. What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation.

Graphs

Graph A set of nodes and a set of edges that relate the nodes to each otherUndirected graph Edges have no directionDirected graph (Digraph) Each edge has a direction (arrowhead)Weighted GraphEdges have values

35

Page 36: Chapter 8 Abstract Data Types and Subprograms. What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation.

Graphs

36

Figure 8.10Examples of graphs

Page 37: Chapter 8 Abstract Data Types and Subprograms. What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation.

Graphs

37

Figure 8.10Examples of graphs

Page 38: Chapter 8 Abstract Data Types and Subprograms. What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation.

Graphs

38

Figure 8.10Examples of graphs

Page 39: Chapter 8 Abstract Data Types and Subprograms. What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation.

Common Graph Algorithms

39

•Traveling salesman problemFinding the cheapest or shortest path through several cities

•Internet data routing algorithms•Family tree software•Neural Nets (An Artificial Intelligence technique)

Page 40: Chapter 8 Abstract Data Types and Subprograms. What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation.

Graph Algorithms

A Depth-First Searching Algorithm--Given a starting vertex and an ending vertex, we can develop an algorithm that finds a path from startVertex to endVertex

This is called a depth-first search because we start at a given vertex and go to the deepest branch and exploring as far down one path before taking alternative choices at earlier branches

40

Page 41: Chapter 8 Abstract Data Types and Subprograms. What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation.

Depth First Search(startVertex, endVertex)Set found to FALSEPush(myStack, startVertex)WHILE (NOT IsEmpty(myStack) AND NOT found)

Pop(myStack, tempVertex)IF (tempVertex equals endVertex)

Write endVertexSet found to TRUE

ELSE IF (tempVertex not visited)Write tempVertexPush all unvisited vertexes adjacent with tempVertexMark tempVertex as visited

IF (found)Write "Path has been printed"

ELSEWrite "Path does not exist")

41

Page 42: Chapter 8 Abstract Data Types and Subprograms. What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation.

Can we get from Austin to Washington?

Figure 8.11 Using a stack to store the routes

42

Page 43: Chapter 8 Abstract Data Types and Subprograms. What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation.

Can we get from Austin to Washington?

Figure 8.12, The depth-first search

43

Page 44: Chapter 8 Abstract Data Types and Subprograms. What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation.

Breadth-First Search

What if we want to answer the question of how to get from City X to City Y with the fewest number of airline stops? A Breadth-First Search answers this question A Breadth-First Search examines all of the vertices adjacent with startVertex before looking at those adjacent with those adjacent to these verticesA Breadth-First Search uses a queue, not a stack, to answer this above question Why??

44

Page 45: Chapter 8 Abstract Data Types and Subprograms. What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation.

Breadth First Search(startVertex, endVertex)

Set found to FALSE

Enque(myQueue, startVertex)

WHILE (NOT IsEmpty(myQueue) AND NOT found)

Deque(myQueue, tempVertex)

IF (tempVertex equals endVertex)

Write endVertex

Set found to TRUE

ELSE IF (tempVertex not visited)

Write tempVertex

Enque all unvisited vertexes adjacent with tempVertex

Mark tempVertex as visited

IF (found)

Write "Path has been printed"

ELSE

Write "Path does not exist"

Page 46: Chapter 8 Abstract Data Types and Subprograms. What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation.

How can I get from Austin to Washington in the fewest number of stops?

Figure 8.13 Using a queue to store the routes

46

Page 47: Chapter 8 Abstract Data Types and Subprograms. What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation.

Breadth-First Search Traveling from Austin to Washington, DC

Figure 8.14, The Breadth-First Search

47