Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

Similar documents
First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

12 Abstract Data Types

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

R13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC)

Stacks, Queues and Hierarchical Collections

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

TREES. Trees - Introduction

1 P a g e A r y a n C o l l e g e \ B S c _ I T \ C \

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College!

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305

Binary Trees, Binary Search Trees

INSTITUTE OF AERONAUTICAL ENGINEERING

Name CPTR246 Spring '17 (100 total points) Exam 3

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

CS301 - Data Structures Glossary By

Friday, March 30. Last time we were talking about traversal of a rooted ordered tree, having defined preorder traversal. We will continue from there.

1) What is the primary purpose of template functions? 2) Suppose bag is a template class, what is the syntax for declaring a bag b of integers?

7.1 Introduction. A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w

Code No: R Set No. 1

Formal Languages and Automata Theory, SS Project (due Week 14)

End-Term Examination Second Semester [MCA] MAY-JUNE 2006

3137 Data Structures and Algorithms in C++

March 20/2003 Jayakanth Srinivasan,

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

APJ ABDUL KALAM TECHNOLOGICAL UNIVERSITY THIRD SEMESTER B.TECH DEGREE EXAMINATION, JULY 2017 CS205: DATA STRUCTURES (CS, IT)

Data Structure Advanced

Data Structures Question Bank Multiple Choice

Department of Computer Science and Technology

1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking

UNIT 6A Organizing Data: Lists. Last Two Weeks

Table of Contents. Chapter 1: Introduction to Data Structures... 1

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

CSE 373 APRIL 17 TH TREE BALANCE AND AVL

Computer Science Foundation Exam. Dec. 19, 2003 COMPUTER SCIENCE I. Section I A. No Calculators! KEY

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

Course goals. exposure to another language. knowledge of specific data structures. impact of DS design & implementation on program performance

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.

IX. Binary Trees (Chapter 10)

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

CSE 373 OCTOBER 11 TH TRAVERSALS AND AVL

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

Data Structures and Algorithms for Engineers

Data Structure. IBPS SO (IT- Officer) Exam 2017

Summer Final Exam Review Session August 5, 2009

Data Structures. Trees. By Dr. Mohammad Ali H. Eljinini. M.A. Eljinini, PhD

CS 8391 DATA STRUCTURES

EE 368. Week 6 (Notes)

Prepared By: Ms. Nidhi Solanki (Assist. Prof.) Page 1

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

MLR Institute of Technology

Visit ::: Original Website For Placement Papers. ::: Data Structure

DATA STRUCTURE UNIT I

The Stack and Queue Types

ECE250: Algorithms and Data Structures Midterm Review

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

Chapter 20: Binary Trees

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

Friday Four Square! 4:15PM, Outside Gates

UNIT 6A Organizing Data: Lists Principles of Computing, Carnegie Mellon University

Topics. Trees Vojislav Kecman. Which graphs are trees? Terminology. Terminology Trees as Models Some Tree Theorems Applications of Trees CMSC 302

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE

CSI33 Data Structures

STACKS AND QUEUES. Problem Solving with Computers-II

DS ata Structures Aptitude

Largest Online Community of VU Students

ASSIGNMENTS. Progra m Outcom e. Chapter Q. No. Outcom e (CO) I 1 If f(n) = Θ(g(n)) and g(n)= Θ(h(n)), then proof that h(n) = Θ(f(n))

Linked List. April 2, 2007 Programming and Data Structure 1

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU

8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F))

CS8391-DATA STRUCTURES

E.G.S. PILLAY ENGINEERING COLLEGE (An Autonomous Institution, Affiliated to Anna University, Chennai) Nagore Post, Nagapattinam , Tamilnadu.

Sample Question Paper

a) State the need of data structure. Write the operations performed using data structures.

Objective Questions for Online Practical Exams under CBCS Scheme Subject: Data Structure-I (CS-113)

Postfix (and prefix) notation

Module 1: Asymptotic Time Complexity and Intro to Abstract Data Types

ECE Spring 2018 Problem Set #0 Due: 1/30/18

Programming II (CS300)

Basic Data Structures (Version 7) Name:

F453 Module 7: Programming Techniques. 7.2: Methods for defining syntax

Introduction p. 1 Pseudocode p. 2 Algorithm Header p. 2 Purpose, Conditions, and Return p. 3 Statement Numbers p. 4 Variables p. 4 Algorithm Analysis

CS61BL. Lecture 3: Asymptotic Analysis Trees & Tree Traversals Stacks and Queues Binary Search Trees (and other trees)

University of Waterloo Department of Electrical and Computer Engineering ECE250 Algorithms and Data Structures Fall 2014

March 13/2003 Jayakanth Srinivasan,

There are many other applications like constructing the expression tree from the postorder expression. I leave you with an idea as how to do it.

Course Review for Finals. Cpt S 223 Fall 2008

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

CS171 Midterm Exam. October 29, Name:

Analysis of Algorithms

DYNAMIC MEMORY ALLOCATION AND DEALLOCATION

Trees. T.U. Cluj-Napoca -DSA Lecture 2 - M. Joldos 1

Chapter 9. Priority Queue

Data Structures and Algorithms Notes

CS8391-DATA STRUCTURES QUESTION BANK UNIT I

Transcription:

[1] Big-O Analysis AVERAGE(n) 1. sum 0 2. i 0. while i < n 4. number input_number(). sum sum + number 6. i i + 1 7. mean sum / n 8. return mean Revision Statement no. of times executed 1 1 2 1 n+1 4 n n 6 n 7 1 8 1 T(n) = 4n +, growth rate of n. We use asymptotic notation, such as big-oh. So,T(n) = O(n), i.e., it is of O(n) complexity. Compare algorithms: Linear search vs Binary search Linear search Set found to false. Set position to -1. Set index to 0. While found is false and index < number of elements If list [Index] is equal to search value found = true. position = index. End If Add 1 to index. End While. Return position Binary search Preconditions: a is an array sorted in ascending order, first is the index of the first element to search, last is the index of the last element to search, target is the item to search for. If first > last return failure mid (first+last)/2 if a[mid] is equal to target return mid else if target < a[mid] return result of recursive search of a from first up to mid-1 else return result of recursive search of a from mid+1 up to last Postcondition: value returned is position of target in a, otherwise return failure

Example1- For an array a = {22, 11, 1, 81, }, trace linearsearch (a,,11). a = {22, 11, 1, 81, } linearsearch (a,,11) n == ; target == 11. Since a[4]!= target, return linearsearch(a, 4, 11) linearsearch (a, 4,11) n == 4; target == 11. Since a[]!= target, return linearsearch(a,, 11) linearsearch (a,, 11) n == ; target == 11. Since a[2]!= target, return linearsearch(a, 2, 11) linearsearch(a, 2, 11) n == 2; target == 11. Since a[l] == target, return 1 Example2-For an array a = {22, 11, 1, 81, }, trace linearsearch (a,,0). a = {22, 11, 1, 81, } linearsearch (a,, 0) n == ; target == 0. Since a[4]!= target, return linearsearch(a, 4, 0) linearsearch (a, 4,0) n == 4; target == 0. Since a[]!= target, return linearsearch(a,, 0) linearsearch (a,, 0) n == ; target == 0. Since a[2]!= target, return linearsearch(a, 2, 0) linearsearch (a, 2,0) n == 2; target == 0. Since a[1]!= target, return linearsearch(a, 1, 0) linearsearch (a, 1, 0) n == 1; target == 0. Since a[0]!= target, return linearsearch(a, 0, 0) 1. linearsearch (a, 0, 0) n <= 0; return -1 Example- For an array a = {1, 6, 1, 22, 7, 4, 2}, trace binarysearch(a, 0, 6, 7). binarysearch (a, 0, 6, 7) first=0; last=6; target=7; mid=; target > a[], return binarysearch(a,4,6, 7) binarysearch (a, 4, 6, 7) first=4; last=6; target=7; mid=; target < a[], return binarysearch(a,4,4, 7) binarysearch (a, 4, 4, 7) first=4; last=4; target=7; mid=4; target == a[], return 4 Example4- For an array a = {1, 6, 1, 22, 7, 4, 2}, trace binarysearch(a, 0, 6, 40). binarysearch (a, 0, 6, 40) first=0; last=6; target=40; mid=; target > a[], return binarysearch(a,4,6, 40) binarysearch (a, 4, 6, 40) first=4; last=6; target=40; mid=; target < a[], return binarysearch(a,4,4, 40) binarysearch (a, 4, 4, 40) first=4; last=4; target=40; mid=4; target > a[4], return binarysearch(a,,4, 40) binarysearch (a,, 4, 40) first=4; last=4; target=40; first > last, return -1

What is the complexity of Linear and Binary Search? Linear (Sequential, or serial) Search, if the size of array is n o Best-Case: O(1) if item in [0] o Worst-Case: O(n) if item in [n-1] or not found o Average-Case O(n/2) Binary Search o average-case O(log2 n) [2*] Abstract data types (ADTs) Example 1 Using Data Structures, write the Abstract Data Type (ADT) for Pair of Dice (Characteristics and Operations) Dice ADT Characteristics The value of the first die, a random integer value from 1 to 6, (dice_1), The value of the second die, a random integer value from 1 to 6 (dice_2), The total sum of the two readings (sum) Operations int roll() randomize the values of the two dice and return the total sum int die_1() return the reading of the first die int die_2() return the reading of the second die [] Linked list ADT What are the basic linked list operations. appending a node, traversing the list, inserting a node, deleting a node, and destroying the list. What is the difference between appending and inserting a node to a list? Append a node means adding it to the end of a list. Create a new node. Store data in the new node. If there are no nodes In the list Make the new node the first node. Else Traverse the list to find the last node. Add the new node to the end of the list. End If. Insert a node means adding it to a list, but not necessarily to the end. Create a new node. Store data In the new node. If there are no nodes In the list Make the new node the first node. Else Find the first node whose value is > or = the new value, or the end of the list Insert the new node before the found node, or at the end of the list if not found. End If.

Trace following program to append 2., 9.7, and. to draw the form of linked list. void main(void){ FloatList list; list.appendnode(2.); list.appendnode(7.9); list.appendnode(.6); } What is output of the following program and draw the corresponding linked list structure. void main(void) { FloatList list; // Build the list list.appendmode(2.); list.appendnode(7.9); list.appendnode(.6) ; list.insertnode(.); // Insert a node in the middle of the list. list.displaylist();// Dispay the list }

write the algorithms of Traversing and Deleting a Node Traversing Assign List head to node pointer. While node pointer is not NULL Display the value member of the node pointed to by node pointer. Assign node pointer to its own next member End While. Deleting a Node Remove the node from list without breaking the links created by the next pointers Deleting the node from memory (delete nodeptr) And show List.deleteNode(7.9); [4] STACK ADT: A stack S stores items of some type in Last-In, First-Out (LIFO) order. Basic Operations PUSH 1. If stack is not full then : 2. Add 1 to the stack pointer,. Store item at stack pointer location POP 1. If stack is not empty then : 2. read item at stack pointer location,. subtract 1 from the stack pointer Example: Push, push, push 1, push 20, push 2, pop, pop, pop, pop stack size =, [0] top =-1 [1] [2] initially [] [4]

stack size =, top =0 Push stack size =, top = 1 Push stack size =, top = 4 after all Push stack size =, top = pop 1 20 2 1 20 [0] [1] [2] [] [4] [0] [1] [2] [] [4] [0] [1] [2] [] [4] [0] [1] [2] [] [4] Q2. Given an initially empty stack, the following operations are performed: Push T, Push E, Push X, Pop, Push R, Push V, Pop, Push Y, Pop, Pop (a) Write out the composition of the stack after these operations. (b) What sequence of letters was popped off the stack? XVYR Reverse Polish Notation (RPN) Stack ADT is used to evaluate a mathematical expression written in reverse Polish notation (RPN= postfix notation). (A+B)*C AB+C* infix postfix Notice that the postfix expression HAS NO AMBIGUITY, so it does not need parentheses to ensure that A+B is computed before the multiplication of C.

[4.] QUEUE ADT : Operations allowed on a queue: Creating an empty queue. Testing if a queue is empty. Adding data to the tail of the queue (enqueue). Removing data from the head of the queue (dequeue). A Queue Q stores items of some type, with First-In, First-Out (FIFO) access example: Identify the queue structure after execute the following enqueue operations. enqueue(),enqueue(6), enqueue(9), and three consecutive dequeue [] Binary search trees (BST) Basic operations Inserting a new node. Deleting a node. Listing or visiting the nodes of the tree (traversal) How to sort list of integers [, 2, 8, 4, and 1] into ascending order using BST

How to insert in the tree below 17 26 6 14 20 28 11 1 7 The Binary Search Tree after inserted 17 26 6 14 20 28 11 1 7 Traversal The common methods for traversing a binary tree and processing the value of each node: Inorder traversal: 1. The node's left subtree is traversed. 2. The node's data is processed.. The node's right subtree is traversed Preorder traversal: 1. The node's data is processed 2. The node's left subtree is traversed.. The node's right subtree is traversed. Postorder traversal: 1. The node's left subtree is traversed. 2. The node's right subtree is traversed.. The node's data is processed. 1 2 4 6 7 8 9 11

Tree after completed preorder traversal 1 1 Tree visited using postorder traversal 1 2 2 8 6 2 11 4 6 9 6 7 4 9 6 7 4 8 9 7 11 11 1 8 9 2 4 7 11 8 void main(void) { BinaryTree tree; tree.insertnode(); tree.insertnode(8); tree.insertnode(); tree.insertnode(); tree.insertnode(9); } Inorder traversal: 8 9 Preorder traversal: 8 9 Postorder traversal: 9 8 Deleting a node There are 2 possible situations to face when deleting a non-leaf node: A) the node has one child, or B) the node has two children. Q1 Suppose the following values are inserted into a binary tree, in the order given: 2,17,9,,22,24,0,18,,14, 20, Draw a diagram of the resulting binary tree. Q2 Draw a diagram of the resulting binary tree after the following operations : - insert, 8,,, 9, remove 8, insert.

(Prefix) Expression tree for + 7 2 1 = - + 7 2 1 - + 1 X + 1 7 2 14 17 1 16 Q1 Create an expression tree for the following expression: 6 * 4 / 2 + 7 - * [6] Graphs vertex-set V(G) = {u, v, w, z} edge-set E(G) = {e1, e2, e, e4} G = {V(G), E(G)} = {{u, v, w, z}, {e1, e2, e, e4}} Types of graphs Representing graphs a b c d e f g The adjacency matrix: a b c d e f g a 0 0 1 1 0 1 0 b 0 0 0 1 1 0 0 c 1 0 0 0 0 1 0 d 1 1 0 0 1 1 0 e 0 1 0 1 0 0 0 f 1 0 1 1 0 0 0 g 0 0 0 0 0 0 0

The incident matrix: ac ad af bd be cf de df a 1 1 1 0 0 0 0 0 b 0 0 0 1 1 0 0 0 c 1 0 0 0 0 1 0 0 d 0 1 0 1 0 0 1 1 e 0 0 0 0 1 0 1 0 f 0 0 1 0 0 1 0 1 g 0 0 0 0 0 0 0 0 The adjacency list.