Name: Login: Signature: ECE 368 Spring Homework 3

Similar documents
Binary Search Trees (BST)

Ordered Lists and Binary Trees

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

Data Structures. Level 6 C Module Descriptor

DATA STRUCTURES USING C

Data Structures and Algorithms

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

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

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

10CS35: Data Structures Using C

Binary Search Trees CMPSC 122

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

Data Structure [Question Bank]

Data Structure with C

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

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

Algorithms and Data Structures Written Exam Proposed SOLUTION

Exercises Software Development I. 11 Recursion, Binary (Search) Trees. Towers of Hanoi // Tree Traversal. January 16, 2013

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

A binary search tree is a binary tree with a special property called the BST-property, which is given as follows:

Chapter 14 The Binary Search Tree

TREE BASIC TERMINOLOGIES

Lecture Notes on Binary Search Trees

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:

Lecture 12 Doubly Linked Lists (with Recursion)

Converting a Number from Decimal to Binary

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

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Binary Trees and Huffman Encoding Binary Search Trees

PES Institute of Technology-BSC QUESTION BANK

Lecture Notes on Binary Search Trees

Binary Heap Algorithms

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

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

WORKSPACE WEB DEVELOPMENT & OUTSOURCING TRAINING CENTER

How To Teach C++ Data Structure Programming

Java Software Structures

Parallelization: Binary Tree Traversal

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

CompuScholar, Inc. Alignment to Utah's Computer Programming II Standards

Analysis of a Search Algorithm

Graduate Assessment Test (Sample)

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

Data Structures Using C++ 2E. Chapter 5 Linked Lists

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

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

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

COMPSCI 105 S2 C - Assignment 2 Due date: Friday, 23 rd October 7pm

Algorithms and Data Structures

Sample Questions Csci 1112 A. Bellaachia

7.1 Our Current Model

Cpt S 223. School of EECS, WSU

From Last Time: Remove (Delete) Operation

API for java.util.iterator. ! hasnext() Are there more items in the list? ! next() Return the next item in the list.

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

C++ INTERVIEW QUESTIONS

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

Binary Search Trees. Ric Glassey

The ADT Binary Search Tree

Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

Algorithms Chapter 12 Binary Search Trees

COMPUTER SCIENCE. Paper 1 (THEORY)

Alex. Adam Agnes Allen Arthur

Stacks. Linear data structures

OPTIMAL BINARY SEARCH TREES

LINKED DATA STRUCTURES

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION

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.

Data Structures and Algorithms(5)

Unordered Linked Lists

02-201: Programming for Scientists

Lecture 6: Binary Search Trees CSCI Algorithms I. Andrew Rosenberg

Analysis of Algorithms I: Binary Search Trees

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

Illustration 1: Diagram of program function and data flow

Binary Search Trees. basic implementations randomized BSTs deletion in BSTs

2. FINDING A SOLUTION

Specific Simple Network Management Tools

Lecture 11 Array of Linked Lists

Atmiya Infotech Pvt. Ltd. Data Structure. By Ajay Raiyani. Yogidham, Kalawad Road, Rajkot. Ph : ,

CS 241 Data Organization Coding Standards

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

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

Keys and records. Binary Search Trees. Data structures for storing data. Example. Motivation. Binary Search Trees

C Dynamic Data Structures. University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell

C++ Support for Abstract Data Types

Linked Lists: Implementation Sequences in the C++ STL

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

Glossary of Object Oriented Terms

University of Pune Revised Structure for the B. Sc. (Computer Science) Course (Second Year to be implemented from Academic Year )

Integrating the C++ Standard Template Library Into the Undergraduate Computer Science Curriculum

DNS LOOKUP SYSTEM DATA STRUCTURES AND ALGORITHMS PROJECT REPORT

Linked List as an ADT (cont d.)

CS 2112 Spring Instructions. Assignment 3 Data Structures and Web Filtering. 0.1 Grading. 0.2 Partners. 0.3 Restrictions

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

CS 2412 Data Structures. Chapter 2 Stacks and recursion

Transcription:

Name: Login: Signature: ECE 368 Spring 2016. Homework 3 1) Linked Lists Assignment in C++ (15pts). Consider the two linked lists explained in lecture 10, slide 4: lst1 and lst2. If we try to assign lst2 to lst1, we will run into problems because lst2 will point only to the head of lst1 (because of the way the linked list class is defined.) lst2 = lst1; We have 2 problems, First: if we call lst1.pop_front(); lst2 will point to null. Second, the elements 72 and 94 are wasted (shown in slide 5 of lecture 10). (Continues in the next page)

1) Given a class defined in an object oriented programming language like C++ or Python, explain what is Operator Overloading. Operator overloading is a technique by which operators used in a programming language are implemented in user-defined types with customized logic that is based on the types of arguments passed. Operator overloading facilitates the specification of user-defined implementation for operations wherein one or both operands are of user-defined class or structure type. This helps user-defined types to behave much like the fundamental primitive data types. Operator overloading is helpful in cases where the operators used for certain types provide semantics related to the domain context and syntactic support as found in the programming language. It is used for syntactical convenience, readability and maintainability. 2) Write a piece of C++ code to fix these problems when performing the following operation: lst2 = lst1 (Hint: Use Operator Overloading). (Sample Solution 1) List &List::operator = ( List rhs ) std::swap( *this, rhs ); return *this;

(Sample Solution 2) List &List::operator = ( List const &rhs ) if ( this == &rhs ) return *this; if ( rhs.empty() ) while (!empty() ) pop_front(); return *this; if ( empty() ) list_head = new Node( rhs.front() ); else head()->element = rhs.front(); Node *this_node = list_head, *rhs_node = rhs.head()->next(); while ( rhs_node!= 0 ) if ( this_node->next() == 0 ) this_node->next_node = new Node( rhs_node->retrieve() ); this_node = this_node->next(); else this_node->next(); this_node->element = rhs_node->retrive(); rhs_node = rhs_node->next(); while ( this_node->next()!= 0 ) Node *tmp = this_node->next(); this_node->next_node = this_node->next()->next(); delete tmp; return *this;

2) Fixing a linked list constructor (15pts). Consider Slide 8 of Lecture 10. We construct a linked list with the command: List vec = initialize( 3, 6 ); Where initialize is defined as follows. However, the function initialize( int a, int b ) will destruct the local variable ls after execution. Therefore, both ls and vec will point at null. Your task is to write the initialize_v2( int a, int b ) constructor so that vec will point to 3 and ls will point to null after the function initialize_v2 is called. (Note: It must be written in C++) (Sample Solution 1) List initialize_v2 ( int a, int b ) List *ls = new List(); for ( int i=b; i>=a; --i ) ls->push_front( i ); return *ls; (Sample Solution 2) List* initialize_v2 ( int a, int b ) List *ls = new List(); for ( int i=b; i>=a; --i ) ls->push_front(i); return ls; List *vec = initialize_v2( 3, 6 );

3) Reversing a Linked List (15pts). Write a function (in C++ or C) to reverse a singly linked list. The function takes a pointer to a list head as input parameter and returns a pointer to the reversed list new head: ( Sample Solution1 Iterative (c) ) typedef struct node char data; struct node* next; Node; Node* reverse (Node* root) Node* new_root = 0; while (root) Node* next = root->next; root->next = new_root; new_root = root; root = next; return new_root; ( Sample Solution2 Recursive (c) ) typedef struct node char data; struct node* next; Node; Node* reverse (Node* root) if(!root!root->next) return root; Node* nextnode = root->next; nextnode = reverse(nextnode); root->next->next = root; root->next = NULL; return nextnode;

4) Binary Search Tree (15pts). The following diagram shows a binary search tree. Perform the following operations: insert 24, insert 19, and delete 50. Use the inorder successor to perform node deletion. (Draw the new BST after every operation) (Insert 24) 50 15 62 5 20 55 91 3 8 37 60 24

(Insert 19) 50 15 62 5 20 55 91 3 8 19 37 60 24 (Delete 50) 55 15 62 5 20 60 91 3 8 19 37 24

5) Programming Assignment (40pts). Implement the following traversal algorithms: Inorder, Preorder and Postorder. The files hw3.c and test.txt will be provided through the assignment on blackboard. Submit only one c file with the name: hw3_sol.c. Given the sample text file test.txt, your code should have the following output: Inorder Traversal: 1 2 3 4 5 6 7 8 9 10 11 12 Preorder Traversal: 4 2 1 3 5 10 6 8 7 9 12 11 Postorder Traversal: 1 3 2 7 9 8 6 11 12 10 5 4 ( Sample Solution 1 Iterative (c) ) void inorder (struct node* r) If (r!=null) inorder(r->left); printf("%d ", r->value); inorder(r->right); void preorder (struct node* r) If (r!=null) printf("%d ", r->value); preorder(r->left); preorder(r->right); void postorder (struct node* r) If (r!=null) postorder(r->left); postorder(r->right); printf("%d ", r->value);

( Sample Solution 2 Iterative (c++) ) : For c, you need to build your own stack functionss. void inorder (struct node* r) struct node *cur = r; stack<struct node *> s; while (!s.empty() cur) if (!cur) cur = s.top(); printf("%d ", cur->value); cur = cur->right; s.pop(); If (cur) s.push(cur); cur = cur->left; return; void preorder (struct node* r) struct node *cur = r; if (!cur) return; stack<struct node *> s; s.push(cur); while(!s.empty()) cur = s.top(); printf("%d ", cur->value); s.pop(); if (cur->right) s.push(cur->right); if (cur->left) s.push(cur->left); return;

void postorder (struct node* r) struct node *cur = r; if (!cur) return; stack<struct node *> s1; stack<struct node *> s2; s1.push(cur); while(!s1.empty()) cur = s1.top(); s2.push(cur); s1.pop(); if (cur->left) s1.push(cur->left); if (cur->right) s1.push(cur->right); while(!s2.empty()) cur = s2.top(); s2.pop(); printf("%d ", cur->value); return;