Midterm Review 22C:21 Computer Science II. Problem 1 A Set ADT represents some subset of {1,2,..., n} for some natural number n. It supports the operations.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Inserting a Node into a Specified Position of a Linked List To create a node for the new item newNode = new Node(item); To insert a node between two nodes.
Stacks, Queues, and Linked Lists
Data Structures ADT List
AITI Lecture 19 Linked List Adapted from MIT Course 1.00 Spring 2003 Lecture 26 and Tutorial Note 9 (Teachers: Please do not erase the above note)
Computer Science 112 Fundamentals of Programming II Queues and Priority Queues.
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
Circular Linked List. Agenda  What is a Circularly linked list  Why a Circular Linked List  Adding node in a new list  Adding node to list with nodes.
Queue Overview Queue ADT Basic operations of queue
Graphs CS3240, L. grewe.
Priority Queues and Heaps. Overview Our last ADT: PriorityQueueADT A new data structure: heaps One more sorting algorithm: heapsort Priority Queues and.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
CS-2851 Dr. Mark L. Hornick 1 Tree Maps and Tree Sets The JCF Binary Tree classes.
Chapter 7 Stacks II CS Data Structures I COSC 2006
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
SAK 3117 Data Structures Chapter 6: LINKED LISTS.
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Chapter 12 Collections. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Collections A collection is an object that helps us organize and manage.
Data Structures Using Java1 Chapter 7 Queues. Data Structures Using Java2 Chapter Objectives Learn about queues Examine various queue operations Learn.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Data Structures Using C++
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
13-1 Sets, Bags, and Tables Exam 1 due Friday, March 16 Wellesley College CS230 Lecture 13 Thursday, March 15 Handout #23.
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
Graphs – Part II CS 367 – Introduction to Data Structures.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Introduction to Data Structures and Algorithms
Sets and Maps Computer Science 4 Mr. Gerb Reference: Objective: Understand the two basic applications of searching.
Chapter 5 Linked Lists II
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 Java linked list. Java linked list - definition ▪ Often in programming we are required to systematically store some type of information. A prime example.
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
– Graphs 1 Graph Categories Strong Components Example of Digraph
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
Java linked list.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Queues Another Linear ADT Copyright © 2009 Curt Hill.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone();
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
CS2005 Week 7 Lectures Set Abstract Data Type.
Linked Data Structures
Java Programming: Program Design Including Data Structures
Priority Queue.
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 13 Collections.
Data Structures ADT List
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
COMPUTER 2430 Object Oriented Programming and Data Structures I
ADT list.
COMPUTER 2430 Object Oriented Programming and Data Structures I
by Sriram Pemmaraju Unoversity of Iowa
Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus
Queues Definition of a Queue Examples of Queues
Lists CMSC 202, Version 4/02.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Presentation transcript:

Midterm Review 22C:21 Computer Science II

Problem 1 A Set ADT represents some subset of {1,2,..., n} for some natural number n. It supports the operations insert, delete, isMember, isEmpty, union, and intersection, which are described below.

Problem 1(methods) S.insert(x) takes a number x in {1, 2,..., n} and inserts x it into S. S.delete(x) takes a number x in {1, 2,..., n} and deletes it from S. S.isMember(x) takes a number x in {1, 2,..., n} and returns true if x is in S and returns false, otherwise. S.isEmpty() returns true if S is empty and returns false otherwise. S.union(A) takes a subset A of {1, 2,..., n} and changes S to S union A. S.intersection(A) takes a subset A of {1, 2,..., n} and changes S to S intersection A.

Problem 1(What to do?) To implement the Set ADT as a boolean array of size n. If an element x in {1, 2,..., n} belongs to the set, then slot x-1 in the boolean array should be true; otherwise it should be false. e.g. {1,3,4,7,…} → {T,F,T,T,F,F,T,…}

Solution 1 /** * Implementation of the Set ADT as a boolean array * Michael Edwards and Sriram Pemmaraju */ import java.util.*; public class Set { private int n; private boolean[] set; … contd.

Solution 1 (contd.) // This is the constructor for the Set. public Set(int size) { n = size; set = new boolean[n]; for(int i = 0; i < n; i++) set[i] = false; } // Copy constructor for duplicating a set. public Set(Set s) { n = s.n; set = new boolean[n]; for(int i = 0; i < n; i++) set[i] = s.set[i]; } … contd.

Solution 1 (contd.) /** * This is the default constructor for the Set. * If no argument is provided, it will construct * an empty set that has the potential to hold 100 elements. */ public Set() { this(100); } … contd.

Solution 1 (contd.) // This inserts an element into the set. public void insert(int x) { if(x > 0 && x <= n) set[x-1] = true; } // This removes an element from the set. public void delete(int x) { if(x > 0 && x <= n) set[x-1] = false; } … contd.

Solution 1 (contd.) // Test an element for membership public boolean isMember(int x) { if(x > 0 && x <= n) return set[x-1]; else return false; } // Test the set to see if it is empty. public boolean isEmpty() { boolean empty = true; for(int i = 0; i < n; i++) empty = empty && !set[i]; return empty; } … contd.

Solution 1 (contd.) // Combine another set with this one using the union public void union(Set s) { for(int i = 0; i < n; i++) set[i] = set[i] || s.set[i]; } … contd. {1,3,4} ∪ {2,4} = {1,2,3,4} {T,F,T,T,F} || {F,T,F,T,F} = {T,T,T,T,F}

Solution 1 (contd.) // Combine another set with this one using the intersection public void intersection(Set s) { for(int i = 0; i < n; i++) set[i] = set[i] && s.set[i]; } … contd. {1,3,4} ∩ {3,4,5} = {3,4} {T,F,T,T,F} && {F,F,T,T,T} = {F,F,T,T,F}

Solution 1 (contd.) // Return the number of elements in the set public int size() { int size = 0; for(int i = 0; i < n; i++) if(set[i]) size++; return size; } // Print out all the elements in this set public void print() { System.out.print("{"); for(int i = 0; i < n; i++) if(set[i]) System.out.print((i+1)+", "); System.out.println("}"); }

Problem 6 Write a method of the LinkList class that deletes and returns the last Link of the linked list. Use the following function header: public Link deleteLast() If the linked list is empty, then the function just returns null.

Solution 6 public Link deleteLast() { // if list is empty if(first == null) return null; else{ Link current = first; Link previous = null; while(current.next != null) { previous = current; current = current.next; } // if list has one element, first needs to be updated if(previous == null) first = null; else previous.next = null; return current; }

Problem 7 Write a method of the myGraph class that determines if a given pair of vertices have a common neighbor. The function returns true if the given vertices have a common neighbor; otherwise it returns false. Two vertices A and B have a common neighbor if for some vertex C, the graph contains the edge between A and C and the edge between B and C. For simplicity, you may assume that the two given vertices are present in the graph. Solve this problem using both the adjacency matrix implemenation (myGraph class) and the adjacency list representation (myListGraph class).

Solution 7 (using myGraph) public boolean haveCommonNeighbor(String vertex1, String vertex2) { int id1 = getIndex(vertex1); int id2 = getIndex(vertex2); for(int i = 0; i < numVertices; i++) { // Check if i is a neighbor of both id1 and id2 boolean check1 = false; boolean check2 = false; if(i <= id1) check1 = Edges[id1][i]; else check1 = Edges[i][id1]; if(i <= id2) check2 = Edges[id2][i]; else check2 = Edges[i][id2]; if (check1 && check2) return true; } return false; }

Problem 8 Consider the STRANGE_QUEUE ADT that is similar to the QUEUE ADT, except that each item has an associated priority that can be 0 or 1. Items with priority 0 are considered very important and are "served" before items of priority 1. Like the QUEUE ADT, the STRANGE_QUEUE ADT also supports the operations insert and delete, but these operations for the STRANGE_QUEUE ADT pay attention to the priority of the items.

Problem 8 (contd.) Here is a description of how insert and delete work for the STRANGE_QUEUE ADT. Insert: Add the given item, with the specified priority, to the collection. Delete: if there is an item with priority 0 in the collection, delete the oldest item in the collection with priority 0 and return it. Otherwise, if there is an item with priority 1 in the collection, delete the oldest item in the collection with priority 1 and return it. Otherwise, the collection is empty and there is nothing to return.

Problem 8 (contd.) Write a brief description (5-6 lines) of how you would implement the STRANGE_QUEUE ADT so that the insert and delete functions, both run in O(1) time. Your description would have two parts: (1) describing the data structures you will use for your implementation and, (2) how the two operations are implemented, using these data structures.

Solution 8 We should keep two separate regular queues, one exclusively for items with priority 0 and another for items with priority 1. So the data members could just be: private Queue q0; private Queue q1; Any implementation of the Queue class that performs insert, delete, and isEmpty in O(1) time will suffice for our purposes. The insert operation inserts the given item into q0 if it has priority 0 and into q1 if it has priority 1. The delete operation checks if q0 is empty first. If it is non-empty, then it deletes an item from q0 and returns it. If q0 is empty, it deletes an item from q1 and returns it, assuming that q1 is non-empty.