Data Structures -1 st test- March 30, 2015 授課教授:李錫智.

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

DATA STRUCTURES USING C++ Chapter 5
Recursion, Divide and Conquer Lam Chi Kit (George) HKOI2007.
授課教授:李錫智 Data Structures -1 st exam-. 1.[10] We define f(n) = O(g(n)) if and only if there is a real constant c>0 and an integer constant n 0 >0, such.
授課教授:李錫智 Data Structures -3 rd exam-. 1.[5] Refer to the two trees in Fig. 1. Let’s define the balance factor BF of a node to be the absolute value of.
Chapter 3 Stacks.
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.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Lecture 4 Abstract Data Types (ADT). Usual Data Types Integer, Double, Boolean You do not know the implementation of these. There are some operations.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Applications of Stacks (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Lecture 4 Abstract Data Types (ADT). Usual Data Types Integer, Double, Boolean You do not know the implementation of these. There are some operations.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 20: Binary Trees.
1 Project 5: Median. 2 The median of a collection of numbers is the member for which there are an equal number less than or equal and greater than or.
Data Structures Using C++ 2E Chapter 6 Recursion.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Data Structures Using C++ 2E Chapter 6 Recursion.
Recursion. Basic problem solving technique is to divide a problem into smaller subproblems These subproblems may also be divided into smaller subproblems.
Recursion. Basic problem solving technique is to divide a problem into smaller sub problems These sub problems may also be divided into smaller sub problems.
Stack. Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations on the data.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.
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.
Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 6 Recursion.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 3.
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
Data Structure & Algorithm
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues (part 2)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
Data Structures -3 rd test- 2015/06/01 授課教授:李錫智. Question 1 Suppose we use an array to implement a sorted list. Please answer the following questions.
Data Structures -3 st exam- 授課教師 : 李錫智 教授. 1. [5] Assume we have a binary tree which is implemented in a pointer-based scheme. Describe how to know the.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Recursion.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Array-Based Implementations Chapter 3 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Data Structures -3 rd exam- 授課教授:李錫智. 1.[10] Suppose we want to implement a queue by using the following List operations: getLength(); remove(position);
Data Structures -1 st exam- 授課教師 : 李錫智 教授. 1.[10] Suppose we have the ADT Bag which has the following operations: bool isEmpty(): tests whether Bag is.
Queues Another Linear ADT Copyright © 2009 Curt Hill.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
1 Stacks Abstract Data Types (ADTs) Stacks Application to the analysis of a time series Java implementation of a stack Interfaces and exceptions.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
LINKED LISTS.
Data Structures Intro2CS – week Stack ADT (Abstract Data Type) A container with 3 basic actions: – push(item) – pop() – is_empty() Semantics: –
DATA STRUCTURES 1st exam solution. Question 1(1/3) Suppose we have the ADT Bag which has the following operations: bool isEmpty(): tests whether Bag is.
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.
Topic 2: binary Trees COMP2003J: Data Structures and Algorithms 2
Chapter 12 – Data Structures
Recursion DRILL: Please take out your notes on Recursion
5.13 Recursion Recursive functions Functions that call themselves
Data Structure Interview Question and Answers
Stacks.
Linked Lists Damian Gordon.
Stacks and Queues.
Queues Queues Queues.
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.
CMSC 341 Lecture 5 Stacks, Queues
A Bag Implementation that Links Data
A Robust Data Structure
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Data Structures & Algorithms
CS210- Lecture 3 Jun 6, 2005 Announcements
Presentation transcript:

Data Structures -1 st test- March 30, 2015 授課教授:李錫智

Question 1 Suppose you want to open a grocery store. Please design an abstract data type (ADT) for your store. Please give at least five operations.

Solution of Q.1 Grocery +getTotalItem(): integer +isEmpty(): boolean +stockItem(newEntry: ItemType): boolean +sellItem(anEntry: ItemType): boolean +findItem(anEntry: ItemType): ItemType

Question 2 Suppose we use an integer variable itemcount and an array A of size 12 to implement the bag structure. Note that objects are stored in no particular order and objects may be duplicated. We would like to store integers in a bag.

Q.2-1 We have to initialize the bag. Please show the content of itemcount and A in the initial state.

Solution of Q.2-1 itemcount = 0

Q.2-2 Suppose we add six integers 5, 10, 5, 5, 15, and 10 successively in the bag. Please show the content of itemcount and A after these additions.

Solution of Q.2-2 itemcount = 6 A[] = {5, 10, 5, 5, 15, 10,,,, }

Q.2-3 We want to delete the second 5 from the bag. How do you do it? Please describe. Also, please show the content of itemcount and A after execution.

Solution of Q.2-3 Q.2-3: num5count = 0 For i from 0 to 12: If A[i] == 5: If num5count == 1: For j from i+1 to 12: Copy A[j] to A[j-1] itemcount = itemcount – 1 Else: num5count = num5count + 1 End itemcount = 5 A[] = { 5, 10, 5, 15, 10,,,,, }

Q.2-4 We want to see whether the bag is empty. How do you do it? Please describe.

Solution of Q.2-4 isEmpty: If itemcount == 0: print “The bag is empty.” Else: print “There is something in bag.” End

Q.2-5 We want to clear the bag, i.e., making the bag empty. How do you do it? Please describe.

Solution of Q.2-5 Clear: itemcount = 0 for i from 0 to 12: A[i] = NULL End

Question 3 Suppose we use a pointer variable headPtr and a chain to implement the bag structure. We would like to store integers in a bag.

Q.3-1 We have to initialize the bag. Please show the content of headPtr and the chain in the initial state.

Solution of Q.3-1 headPtr nullptr

Q.3-2 Suppose we add six integers 5, 10, 5, 5, 15, and 10 successively in the bag. Please show the headPtr and the chain after these additions.

Solution of Q.3-2 headPtr nextPtr5 10 nextPtr5 5 15nextPtr10 nullptr

Q.3-3 We want to delete the second 5 from the bag. How do you do it? Please describe. Also, please show headPtr and the chain after execution.

Solution of Q.3-3 headPtr nextPtr5 10 nextPtr5 5 15nextPtr10 nullptr

Q.3-4 We want to see whether the bag is empty. How do you do it? Please describe.

Solution of Q.3-4 Check whether the headPtr points to nullptr. headPtr nullptr ?

Q.3-5 We want to clear the bag, i.e., making the bag empty. How do you do it? Please describe.

Solution of Q.3-5 Find next node and free current node from headPtr until that the next node is nullptr.

Question 4 Suppose we have the following recursive program: Let’s execute the function call kkk(5,3).

Q.4 How many times kkk(2,1) is executed before termination? 3 How many times kkk(1,1) is executed before termination? 3 What messages are shown on the screen? Please show them in correct order. What is the final return value? 10 Is this function efficient? Why or why not?

Solution of Q.4

Q.4-3 What messages are shown on the screen? Please show them in correct order.

Q.4-5 Is this function efficient? Why or why not? Usually, recursion is not efficient in terms of stack and system overhead. Think about a recursion to calculate the factorial of a number. You will have n- 1 recursion calls.

Question 5 Suppose we have a program: where max takes the larger of its two arguments. Let’s execute the function call aaa(A,0,6) with A = [20, 80, 30, 40, 10, 70, 50].

Q.5 Please list all the base cases encountered during execution and show what values of n and i in each case. What messages are shown on the screen? Please show them in correct order. How many times the function max is executed? 6 Please show its two arguments each time. What is the final return value? 80

Solution of Q.5

Q.5-1 Please list all the base cases encountered during execution and show what values of n and i in each case. aaa(A, 0, 0) 、 aaa(A, 1, 1) 、 aaa(A, 2, 2) aaa(A, 3, 3) 、 aaa(A, 4, 4) 、 aaa(A, 5, 5)

Q.5-2 About to compute for i, n = 0, 6 About to compute for i, n = 0, 3 About to compute for i, n = 0, 1 About to compute for i, n = 0, 0 About to compute for i, n = 1, 1 About to compute for i, n = 2, 3 About to compute for i, n = 2, 2 About to compute for i, n = 3, 3 About to compute for i, n = 4, 6 About to compute for i, n = 4, 5 About to compute for i, n = 4, 4 About to compute for i, n = 5, 5 About to compute for i, n = 6, 6

Question 6 Suppose we have a recursive program: where Swap exchanges the two values involved. Let’s execute the function call qqq(A,0,7) with A = [20, 80, 30, 40, 10, 70, 50].

Q.6 How many times the function qqq is called before termination, including qqq(A,0,7) itself? 4 What messages are shown on the screen? Please show them in correct order. How many times the Swap operation is performed? 3 What is the content of A after execution? [50, 70, 10, 40, 30, 80, 20]

Solution of Q.6 0, 7  0  6 1, 5  1  5 2, 3  2  4 3, 1  End

Q.6-2 What messages are shown on the screen? Please show them in correct order. About to compute for i, n = 0, 7 To swap the indices 0 and 6 About to compute for i, n = 1, 5 To swap the indices 1 and 5 About to compute for i, n = 2, 3 To swap the indices 2 and 4 About to compute for i, n = 3, 1 End computing for i, n = 3, 1