Chapter 9 Abstract Data Types and Algorithms. 2 Abstract Data Types Abstract data type A data type whose properties (data and operations) are specified.

Slides:



Advertisements
Similar presentations
Abstract Data Types and Algorithms
Advertisements

Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
Problem Solving and Algorithms
Abstract Data Types and Subprograms
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Abstract Data Types and Subprograms
Chapter 8 Abstract Data Types and Subprograms. 2 Chapter Goals Distinguish between an array-based visualization and a linked visualization Distinguish.
Connecting with Computer Science, 2e
CS 206 Introduction to Computer Science II 10 / 31 / 2008 Happy Halloween!!! Instructor: Michael Eckmann.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
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.
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.
CHAPTER 11 Searching. 2 Introduction Searching is the process of finding a target element among a group of items (the search pool), or determining that.
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.
Important Problem Types and Fundamental Data Structures
Slides modified by Erin Chambers Problem Solving and Algorithm Design, part 2.
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.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Chapter 9 Abstract Data Types and Algorithms. 2 Chapter Goals Define an abstract data type and discuss its role in algorithm development Distinguish between.
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.
Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2.
Connecting with Computer Science, 2e Chapter 8 Data Structures.
Information and Computer Sciences University of Hawaii, Manoa
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Chapter 9 Priority Queues, Heaps, Graphs, and Sets.
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
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.
Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)
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.
ACM/JETT Workshop - August 4-5, 2005 Using Visualization Tools To Teach Data Structures and Algorithms Java applets by Dr. R. Mukundan, University of Canterbury,
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).
1 C++ Plus Data Structures Nell Dale Chapter 10 Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
The List ADT A sequence of zero or more elements A 1, A 2, A 3, … A N-1 N: length of the list A 1 : first element A N-1 : last element A i : position i.
The ADT Table The ADT table, or dictionary Uses a search key to identify its items Its items are records that contain several pieces of data 2 Figure.
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.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 15 Other Data Structures Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Chapter 8 Abstract Data Types and Subprograms. 2 Chapter Goals Distinguish between an array-based visualization and a linked visualization Distinguish.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
Data Structure and Algorithms
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To introduce the basic concepts of linked lists ❏ To introduce the basic concepts.
CPS120: Introduction to Computer Science Sorting.
Chapter 9 Abstract Data Types and Algorithms Nell Dale John Lewis.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
Chapter 16: Searching, Sorting, and the vector Type.
CPS120: Introduction to Computer Science Nell Dale John Lewis Abstract Data Types.
CPS120: Introduction to Computer Science Sorting.
UNIT-V ABSTRACT DATA TYPE 1.LIST 2.STACK 3.QUEUE EC6301-II-ECE-C.
CSE373: Data Structures & Algorithms
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
Chapter 15 Lists Objectives
Hashing Exercises.
Abstract Data Types and Subprograms
Chapter 17 Object-Oriented Data Structures
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.
Chapter 9 Priority Queues, Heaps, Graphs, and Sets
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Chapter 8: Data Abstractions
ITEC 2620M Introduction to Data Structures
CS6045: Advanced Algorithms
Important Problem Types and Fundamental Data Structures
Presentation transcript:

Chapter 9 Abstract Data Types and Algorithms

2 Abstract Data Types Abstract data type A data type whose properties (data and operations) are specified independently of any particular implementation

3 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

4 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

5 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. This level is concerned with data structures.

6 Three Views of Data Composite data type A data type in which a name is given to a collection of data value 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

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

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

9 Unsorted and Sorted Containers Unsorted container The items in the container are not ordered in any way Sorted container The items in the container are ordered by the value of some field within the items

10 Array-Based Implementations Figure 9.1 A list The array goes from [0] to [ MAX-LENGTH-1 ] The items in the container (the list) go from [0] to [length-1] 1]

11 Array-Based Implementations Figure 9.2 An unsorted list of integers What is the array? What is the list?

12 Array-Based Implementations Figure 9.3 A sorted list of integers What is the array? What is the list?

13 Array-Based Implementations How do we implement the operations? Add itemgiven an index, shift following items down and store item at index Remove itemgiven an index, shift following items up one Get next itemincrement value of index and return value at that position more itemsvalue of index < length-1

14 Linked Implementation Linked implementation An implementation based on the concept of a node Node A holder for two pieces of information –the item that the user wants in the list (item) –a pointer to the next node in the list (next)

15 Linked Implementation Figure 9.4 Anatomy of a linked list

16 Linked Implementation Figure 9.5 An unsorted linked list

17 Linked Implementation Figure 9.6 A sorted linked list

18 Linked Implementation How do we implement the operations? Add itemgiven current, insert a new node with item in the info part between current and next(current) Remove itemgiven current, remove next(current) Get next itemset current to next(current) more itemscurrent does not contain null

19 Linked Implementation Figure 9.7 Store a node with info of 67 after current

20 Linked Implementation Figure 9.8 Remove node next(current)

21 Lists List operations –Create itself (Initialize) –Insert an item –Delete an item –Print itself –Know the number of items it contains Generic data type (or class) A data type or class in which the operations are specified but the type or class of the objects being manipulated is not

22 Unsorted Lists Create (initialize) Set length to 0 Insert(item) Find where the item belongs Put the item there Increment length Remove(item) Find the item Remove the item Decrement length

23 Unsorted Lists Print While (more items) Get next item Print Item Insert(item) Find where the item belongs Put the item there Increment length Know Length return length

24 Sorted Lists From the application view, how do the sorted an unsorted list differ? The decomposition of which algorithm steps must be different?

25 Unfinished Algorithm Steps Find where the items belongs (unsorted) Item belongs at the length position Find where the items belongs (sorted) Set tempItem to the first item While (item.compareTo(tempItem) > 0) Set tempItem to next item Item belongs at tempItem Find the item Set tempItem to first item While (item.compareTo(tempItem) not equal 0 Set tempItem to next item e.g. insert 998 to the list 23, 46, 75, 1066, 1492

26 Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields) on which the ordering is based Sorting algorithms Algorithms that order the items in the collection based on the sort key Why is sorting important?

27 Selection Sort Given a list of names, put them in alphabetical order –Find the name that comes first in the alphabet, and write it on a second sheet of paper –Cross out the name off the original list –Continue this cycle until all the names on the original list have been crossed out and written onto the second list, at which point the second list contains the same items but in sorted order

28 Selection Sort A slight adjustment to this manual approach does away with the need to duplicate space –As you cross a name off the original list, a free space opens up –Instead of writing the value found on a second list, exchange it with the value currently in the position where the crossed-off item should go

29 Selection Sort Figure 9.9 Example of a selection sort (sorted elements are shaded)

30 Bubble Sort Bubble Sort uses the same strategy: Find the next item Put it into its proper place But uses a different scheme for finding the next item Starting with the last list element, compare successive pairs of elements, swapping whenever the bottom element of the pair is smaller than the one above it

31 Bubble Sort Figure 9.10 Example of a bubble sort

32 Quicksort Figure 9.12 Ordering a list using the Quicksort algorithm It is easier to sort a smaller number of items: Sort A…F, G…L, M…R, and S…Z and A…Z is sorted

33 Quicksort If (there is more than one item in list[first]..list[last]) Select splitVal Split the list so that list[first]..list[splitPoint-1] <= splitVal list[splitPoint] = splitVal list[splitPoint+1]..list[last] > splitVal Quicksort the left half Quicksort the right half

34 Quicksort

35 Quicksort Split Set left to first + 1 Set right to last Do Increment left until list[left] > splitVal OR left > right Decrement right until list[right] right If (left < right) Swap list[left] and list[right] While (left <= right) Set splitPoint to right Swap list[first] and last[right]

36 Quicksort Figure 9.13 Splitting algorithm

37 Binary Search Sequential search Search begins at the beginning of the list and continues until the item is found or the entire list has been searched Binary search (list must be sorted) Search begins at the middle and finds the item or eliminates half of the unexamined items; process is repeated on the half where the item might be Say that again…

38 Binary Search Boolean Binary Search (first, last) If (first > last) return false Else Set middle to (first + last)/2 Set result to item.compareTo(list[middle]) If (result is equal to 0) return true Else If (result < 0) Binary Search (first, middle - 1) Else Binary Search (middle + 1, last)

39 Binary Search Figure 9.14 Trace of the binary search

40 Binary Search Table 9.1 Average Number of Comparisons

41 Stacks and Queues 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 e.g. stack of plates, cans, etc. Cannot pop if stack is empty

42 Stacks and 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

43 Stacks and Queues Figure 9.15 Stack and queue visualized as linked structures

44 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 e.g. family relationships

45 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…

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

47 Binary Search Trees Binary search tree (BST) A binary tree (shape property) that has the (semantic) property that a 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

48 Binary Search Tree Figure 9.18 A 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

49 Binary Search Tree

50 Binary Search Tree Boolean IsThere(current, item) If (current is null) return false Else Set result to item.compareTo(info(current)) If (result is equal to 0) return true Else If (result < 0) IsThere(item, left(current)) Else IsThere(item, right(current))

51 Binary Search Tree Trace the nodes passed as you search for 18, 8, 5, 4, 9, and 15

52 Binary Search Tree Insert (current, item) If (tree is null) Put item in tree Else If (item.compareTo(info(current)) < 0) Insert (item, left(current)) Else Insert (item, right(current))

53 Binary Search Tree Print(tree) If (tree is not null) Print (left(tree)) Write info(tree) Print (right(tree))

54 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

55 Graphs Figure 9.21 Examples of graphs

56 Graphs Figure 9.21 Examples of graphs

57 Graphs Figure 9.21 Examples of graphs