Abstract Data Types and Subprograms

Slides:



Advertisements
Similar presentations
Abstract Data Types and Algorithms
Advertisements

Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
Abstract Data Types and Subprograms
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both.
Chapter 8 Abstract Data Types and Subprograms. 2 Chapter Goals Distinguish between an array-based visualization and a linked visualization Distinguish.
Graphs CS3240, L. grewe.
Advanced Data Structures
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Graphs. Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs Birmingham Rugby London Cambridge.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 09 / 2009 Instructor: Michael Eckmann.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
1 Section 9.2 Tree Applications. 2 Binary Search Trees Goal is implementation of an efficient searching algorithm Binary Search Tree: –binary tree in.
Marc Smith and Jim Ten Eyck
12 Abstract Data Types Foundations of Computer Science ã Cengage Learning.
Trees & Graphs Nell Dale & John Lewis (adaptation by Michael Goldwasser and Erin Chambers)
9 Priority Queues, Heaps, and Graphs. 9-2 What is a Heap? A heap is a binary tree that satisfies these special SHAPE and ORDER properties: –Its shape.
Chapter 9 Abstract Data Types and Algorithms. 2 Abstract Data Types Abstract data type A data type whose properties (data and operations) are specified.
Trees. Tree Terminology Chapter 8: Trees 2 A tree consists of a collection of elements or nodes, with each node linked to its successors The node at the.
Chapter 9 Abstract Data Types and Algorithms. 2 Chapter Goals Define an abstract data type and discuss its role in algorithm development Distinguish between.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
Chapters 7, 8, & 9 Quiz 3 Review 1. 2 Algorithms Algorithm A set of unambiguous instructions for solving a problem or subproblem in a finite amount of.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Chapter 9 Priority Queues, Heaps, Graphs, and Sets.
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
Chapter 9 (modified) Abstract Data Types and Algorithms Nell Dale John Lewis.
Chapter 9 Abstract Data Types and Algorithms Nell Dale John Lewis.
9-1 Abstract Data Types Abstract data type A data type whose properties (data and operations) are specified independently of any particular implementation.
1 Chapter 17 Object-Oriented Data Structures. 2 Objectives F To describe what a data structure is (§17.1). F To explain the limitations of arrays (§17.1).
Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.
Cousin of the Stack.  An abstract data type (container class) in which items are entered at one end and removed from the other end  First In First.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.
Computer Science and Software Engineering University of Wisconsin - Platteville 10. Binary Search Tree Yan Shi CS/SE 2630 Lecture Notes Partially adopted.
Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
Chapter 8 Abstract Data Types and Subprograms. 2 Chapter Goals Distinguish between an array-based visualization and a linked visualization Distinguish.
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
COSC 2007 Data Structures II
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Graphs Upon completion you will be able to:
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
Chapter 9 Abstract Data Types and Algorithms Nell Dale John Lewis.
DATA STRUCURES II CSC QUIZ 1. What is Data Structure ? 2. Mention the classifications of data structure giving example of each. 3. Briefly explain.
CPS120: Introduction to Computer Science Nell Dale John Lewis Abstract Data Types.
CPS120: Introduction to Computer Science Sorting.
Section 9.6 Graph Applications. 9.6 Graph Applications Our graph specification does not include traversal operations. We treat traversal as a graph application/algorithm.
Chapter 12 Abstract Data Type.
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
Chapter 12 – Data Structures
Abstract Data Types and Subprograms
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Graphs Chapter 11 Objectives Upon completion you will be able to:
CSC 143 Java Trees.
Yan Shi CS/SE 2630 Lecture Notes
Important Problem Types and Fundamental Data Structures
Heaps Chapter 6 Section 6.9.
Presentation transcript:

Abstract Data Types and Subprograms Chapter 8 Abstract Data Types and Subprograms

Abstract Data Types Abstract data type A data type whose properties (data and operations) are specified independently of any particular implementation Remember what the most powerful tool there is for managing complexity?

Three Views of Data Application (user) level View of the data within a particular problem View sees data objects in terms of properties and behaviors

Three Views of Data Logical (abstract) level Abstract view of the data and the set of operations to manipulate them View sees data objects as groups of objects with similar properties and behaviors

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 View sees the properties represented as specific data fields and behaviors represented as methods implemented in code

Three Views of Data Composite data type A data type in which a name is given to a collection of data values Data structures The implementation of a composite data fields in an abstract data type Containers Objects whole role is to hold and manipulate other objects

Logical Implementations Two logical implementations of containers Array-based implementation Objects in the container are kept in an array 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 told you where to go to get the next clue?

structures that are stacks 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

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 Enqueue, Enque, Enq, Enter, and Insert are used for the insertion operation Dequeue, Deque, Deq, Delete, and Remove are used for the deletion operation. Name three everyday structures that are queues

Stacks and Queues Stack and queue visualized as linked structures

Lists Think of a list as a container of items Here are the logical operations that can be applied to lists Add item Put an item into the list Remove item Remove an item from the list Get next item Get (look) at the next item more items Are there more items?

Array-Based Implementations

Linked Implementations

Algorithm for Creating and Print Items in a List WHILE (more data) Read value Insert(myList, value) Reset(myList) Write "Items in the list are " WHILE (moreItems(myList)) GetNext(myList, nextItem) Write nextItem, ' ' Which implementation?

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 15 15

Trees Structure such as lists, stacks, and queues are linear in nature; only one relationship is being modeled More complex relationships require more complex structures Can you name three more complex relationships?

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…

Trees What is the unique path to the node containing 5? 9? 7? … Root node Node with two children Node with right child Leaf node What is the unique path to the node containing 5? 9? 7? … Node with left child

Binary Search Trees Binary search tree (BST) A binary tree (shape property) that has the (semantic) property that characterizes the values in a node of a tree: The value in any node is greater than the value in any node in its left subtree and less than the value in any node in its right subtree

Binary Search Tree Each node is the root of a subtree made up of its left and right children Prove that this tree is a BST Figure 8.7 A binary search tree

Binary Search Tree

Binary Search Tree Boolean IsThere(current, item) If (current is null) return false Else If (item is equal to current’s data) return true If (item < current’s data) IsThere(item, left(current)) IsThere(item, right(current))

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?

Binary Search Tree IsThere(tree, item) IF (tree is null) RETURN FALSE ELSE IF (item equals info(tree)) RETURN TRUE IF (item < info(tree)) IsThere(left(tree), item) IsThere(right(tree), item)

Building Binary Search Trees 25 25

Building Binary Search Tree Insert(tree, item) IF (tree is null) Put item in tree ELSE IF (item < info(tree)) Insert (left(tree), item) Insert (right(tree), item) 26 26

Binary Search Tree Print(tree) If (tree is not null) Print (left(tree)) Write info(tree) Print (right(tree)) Is that all there is to it? Yes! Remember we said that recursive algorithms could be very powerful

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

Graphs Figure 8.10 Examples of graphs

Graphs Figure 8.10 Examples of graphs

Graphs Figure 8.10 Examples of graphs

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

An algorithm RecursiveDFS(v): if v is unmarked mark v for each edge vw recursiveDFS(w) Now if you want to search for a node, just check to see if w is equal to the target each time.

An iterative algorithm Use a stack! (Start with x = start vertex) OtherDFS(vertex x): Push(x) while the stack is not empty v <- Pop() if v is not marked mark v for each edge vw push(w)

Can we get from Austin to Washington? Figure 8.11 Using a stack to store the routes

Can we get from Austin to Washington? Figure 8.12, The depth-first search

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 vertices A Breadth-First Search uses a queue, not a stack, to answer this above question Why??

An algorithm Same as DFS, but use a queue! OtherDFS(vertex x): Add x to the queue while the queue is not empty v <- remove front of queue if v is not marked mark v for each edge vw add w to the queue

How can I get from Austin to Washington in the fewest number of stops? Figure 8.13 Using a queue to store the routes

Breadth-First Search Traveling from Austin to Washington, DC Figure 8.14, The Breadth-First Search

Subprogram Statements We can give a section of code a name and use that name as a statement in another part of the program When the name is encountered, the processing in the other part of the program halts while the named code is executed Remember?

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

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

Subprogram Statements Think of arguments as being placed on a message board

Subprogram Statements Insert(list, item) // Subprogram definition Set list.values[length-1] to item Set list.length to list.length + 1 Insert(myList, value) // Calling statement Which parameter must be by reference? 45 45