Algorithms and Data Structures Exercise for the Final Exam (17 June 2014) Stack, Queue, Lists, Trees, Heap



Similar documents
Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Binary Heap Algorithms

Analysis of a Search Algorithm

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

Converting a Number from Decimal to Binary

Data Structures, Practice Homework 3, with Solutions (not to be handed in)

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Stacks. Linear data structures

Sample Questions Csci 1112 A. Bellaachia

Lecture 12 Doubly Linked Lists (with Recursion)

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

Lecture Notes on Binary Search Trees

Data Structure [Question Bank]

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Abstract Data Type. EECS 281: Data Structures and Algorithms. The Foundation: Data Structures and Abstract Data Types

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

Data Structures. Level 6 C Module Descriptor

10CS35: Data Structures Using C

Common Data Structures

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

DATA STRUCTURES USING C

Recursion. Slides. Programming in C++ Computer Science Dept Va Tech Aug., Barnette ND, McQuain WD

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

Binary Search Trees (BST)

Introduction to Data Structures and Algorithms

A COMPARATIVE STUDY OF LINKED LIST SORTING ALGORITHMS

Data Structure with C

Unit Write iterative and recursive C functions to find the greatest common divisor of two integers. [6]

Linked Lists Linked Lists, Queues, and Stacks

Lecture Notes on Binary Search Trees

Binary Search Trees. Ric Glassey

Analysis of Binary Search algorithm and Selection Sort algorithm

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

Coding Rules. Encoding the type of a function into the name (so-called Hungarian notation) is forbidden - it only confuses the programmer.

Introduction to Object-Oriented Programming

Algorithms and Data Structures

OPTIMAL BINARY SEARCH TREES

Data Structures Fibonacci Heaps, Amortized Analysis

Ordered Lists and Binary Trees

PES Institute of Technology-BSC QUESTION BANK

Data Structures and Data Manipulation

C++FA 5.1 PRACTICE MID-TERM EXAM

Data Structures and Algorithms

6 March Array Implementation of Binary Trees

Sorting Algorithms. Nelson Padua-Perez Bill Pugh. Department of Computer Science University of Maryland, College Park

Recursion. Definition: o A procedure or function that calls itself, directly or indirectly, is said to be recursive.

Binary search algorithm

Simple sorting algorithms and their complexity. Bubble sort. Complexity of bubble sort. Complexity of bubble sort. Bubble sort of lists

1 Abstract Data Types Information Hiding

Symbol Tables. Introduction

Data Structures and Algorithm Analysis (CSC317) Intro/Review of Data Structures Focus on dynamic sets

EE2204 DATA STRUCTURES AND ALGORITHM (Common to EEE, EIE & ICE)

CSE 211: Data Structures Lecture Notes VII

THIS CHAPTER studies several important methods for sorting lists, both contiguous

Exam study sheet for CS2711. List of topics

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Chapter 14 The Binary Search Tree

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

Abstract Data Types. Chapter 2

ADTs,, Arrays, Linked Lists

1. Relational database accesses data in a sequential form. (Figures 7.1, 7.2)

Lecture 11 Array of Linked Lists

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

Algorithms and Data Structures

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

Section IV.1: Recursive Algorithms and Recursion Trees

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

CS711008Z Algorithm Design and Analysis

Binary Search Trees CMPSC 122

What Is Recursion? 5/12/10 1. CMPSC 24: Lecture 13 Recursion. Lecture Plan. Divyakant Agrawal Department of Computer Science UC Santa Barbara

To My Parents -Laxmi and Modaiah. To My Family Members. To My Friends. To IIT Bombay. To All Hard Workers

Lecture Notes on Linear Search

Basic Programming and PC Skills: Basic Programming and PC Skills:

Chapter 8: Binary Trees

BCS2B02: OOP Concepts and Data Structures Using C++

Singly linked lists (continued...)

Alex. Adam Agnes Allen Arthur

Binary Heaps. CSE 373 Data Structures

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

TREE BASIC TERMINOLOGIES

Data Structures and Algorithms(5)

Persistent Binary Search Trees

GUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD, GUJARAT. Course Curriculum. DATA STRUCTURES (Code: )

Data Structures and Algorithms Written Examination

St S a t ck a ck nd Qu Q eue 1

CmpSci 187: Programming with Data Structures Spring 2015

Introduction to data structures

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

CS211 Spring 2005 Final Exam May 17, Solutions. Instructions

Transcription:

Algorithms and Data Structures Exercise for the Final Exam (17 June 2014) Stack, Queue, Lists, Trees, Heap

Singly linked list (1) Data about exam results are stored into a singly linked list. Each list element consists of: student name(50+1 character) student ID (int) course code (int) grade (int) The list is not sorted. Write the function that removes students with negative grades (those with grade 1) from the list. The function returns the number of removed list members. Write the corresponding struct.

Solution (1/2) struct record { char name[50+1]; int id; int code; int grade; ; struct at { struct record element; struct at *next ; typedef struct at atom;

Solution (2/2) int removenegative(atom **head){ int removed = 0; atom *tmp; while(*head) { if ((*head)->element.grade == 1) { removed++; tmp = *head; *head = (*head)->next; free(tmp); else { head = &((*head)->next); return removed;

Singly linked list (2) Write the function that removes even numbers from the list that contains integers. At the beginning, the head from the function points to the head from the main program, then it points to pointers sljed, all of which are struct members. If the first node contains even number, then the statement: *glava=(*glava)->sljed; changes the pointer head from the main program, otherwise it changes pointer sljed which is the struct member.

Solution 1 with an auxiliary pointer for the previous atom void remove Even(atom **head) { atom *old; atom *tmp = *head; while ((tmp!= NULL) && (tmp->element % 2 == 0)) { tmp = (*head)->next; free (*head); *head = tmp; while (tmp!= NULL) { while ((tmp-> next!= NULL) && ((tmp-> next)->element % 2!= 0)) tmp = tmp-> next; if (tmp-> next!= NULL) { old = tmp-> next; tmp-> next = old-> next; free(old); else tmp = tmp-> next;

Solution 2 without an auxiliary pointer for the previous atom void removeeven(atom **headp){ int removed = 0; atom *tmp; while(*headp) { if (!((*headp)->element % 2)) { tmp = *headp; *headp = (*headp)->next; free(tmp); else { headp = &((*headp)->next);

Singly linked list (3) - homework Given the two lists sorted in an ascending order, write the recursive function that merges the two lists into one list, also sorted in the ascending order. Return that list into the main program. The protptype of the function is: atom *merge(atom *h1, atom *h2);

Doubly linked list

Doubly linked list Doubly linked list contains data about students, sorted in a descending order (from head) according to the average grades. The list is defined with the following structures: struct record { char name[80+1]; float avggrade; ; struct at { struct record element; struct at *next; struct at *prev; ; typedef struct at atom; Write the function that reverses the order of elements in the doubly linked list. The prototype of the function is: void reverselist(atom **head, atom **tail);

Solution void reverselist(atom **head, atom **tail){ atom *aux,*aux1; aux = *head; *head = *tail; *tail = aux; while(aux!= NULL){ aux1 = aux->next; aux->next = aux->prev; aux->prev = aux1; aux = aux->prev;

Trees Tree containing data about products (name and price) is given with the following structures: typedef struct { char name[20]; float price; Element; struct n{ Element element; struct n *left, *right; ; typedef struct n node;

Trees (1) Write the function that will change the given tree into its mirror copy (for each node replace its left and right child). The prototype of the function is: node *mirrortree (node *root);

Solution node *mirrortree(node *root){ if (root){ node *tmp; tmp = root->left; root->left = mirrortree(root->right); root->right = mirrortree(tmp); return root;

Trees (2) A binary search tree is sorted according to the price. Write the function with the prototype: int search(node *root, float price); The function returns 1 if the tree contains the product with the searched price, otherwise it returns 0.

Solution int search(node *root, float price){ if (root){ if (price == root->element.price) return 1; else if (price < root->element.price) return search(root->left, price); else return search(root->right, price); return 0;

Heap (1) Write the function with the prototype int isheap(node *root); The function returns 1 if the complete binary tree (with the given root) is heap, otherwise it returns 0.

Solution int isheap(node *root){ if (root == NULL) return 1; if ((root->left!=null) && return 0; (root>left->element > root->element)) if ((root->right!= NULL) && return 0; (root->right->element > root->element)) return isheap(root->left) && isheap(root->right);

Heap(1) Assuming a heap is created for the following input sequence: 5, 6, 7, 7, 6, 8, 5, 9, using the algorithm with O(n) running time in the worst case, illustrate all the steps of the creation process: Solution: 6 5 7 6 5 7 6 5 8 7 6 8 5 9 6 8 5 9 6 7 5 9 7 7 5 5 9 9 8 9 8 5 8 6 6 7 5 7 6 7 5 7 6 7 5 7 6 6 9 9 7 8 7 8 5 6 7 5 6 6 7 5 6 5

Heap (2) Assuming a heap is created for the following input sequence: 27, 49, 35, 19, 87, 53, 67, 29, using the algorithm with O(n) running time in the worst case, illustrate all the steps of the creation process: Solution: 27 49 35 19 87 53 67 29 27 49 35 29 87 53 67 19 27 49 67 29 87 53 35 19 27 87 67 29 49 53 35 19 87 27 67 29 49 53 35 19 Heap: 87 49 67 29 27 53 35 19

Heap (3) Assuming a heap is created for the following input sequence: 15,, 7, 11, 40, 5, 90, using the algorithm with O(nlog 2 n) runnning time in the worst case, illustrate all the steps of the creation process: Solution: 15 15 15 15 7 15 7 11 40 40 15 7 7 7 11 40 11 15 11 15 5

Heap (3) 40 7 11 15 5 90 90 40 11 15 5 7

Heap (4) The given heap is stored in the array: 93 82 47 31 64 17 27 llustrate the ascending heapsort.

Solution (1/8) Initial heap: 93 93 82 47 31 64 17 27 82 47 31 64 17 27 Replace the element from the top of the heap with the last element from the unsorted array part: 27 82 47 27 82 47 31 64 17 93 31 64 17

Solution 2/8 Adjust the heap: 82 64 47 82 64 47 31 27 17 93 31 27 17 Replace the element from the top of the heap with the last element from the unsorted array part: 17 64 47 17 64 47 31 27 82 93 31 27

Solution 3/8 Adjust the heap: 64 31 47 64 31 47 17 27 82 93 17 27 Replace the element from the top of the heap with the last element from the unsorted array part: 31 47 31 47 17 27 64 82 93 17 27

Solution 4/8 Adjust the heap: 47 31 47 31 17 27 64 82 93 17 27 Replace the element from the top of the heap with the last element from the unsorted array part: 31 27 27 31 17 47 64 82 93 17

Solution 5/8 Adjust the heap: 27 31 31 27 17 47 64 82 93 17 Replace the element from the top of the heap with the last element from the unsorted array part: 17 27 17 27 31 47 64 82 93

Solution 6/8 Adjust the heap: 27 17 27 17 31 47 64 82 93 Replace the element from the top of the heap with the last element from the unsorted array part: 17 27 31 47 64 82 93 17

Solution 7/8 Adjust the heap: 17 17 27 31 47 64 82 93 Replace the element from the top of the heap with the last element from the unsorted array part: 17 17 27 31 47 64 82 93

Solution 8/8 Sorted array: 17 27 31 47 64 82 93