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



Similar documents
Binary Search Trees (BST)

Binary Search Trees CMPSC 122

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

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

Ordered Lists and Binary Trees

Data Structures and Algorithms

Data Structure [Question Bank]

Converting a Number from Decimal to Binary

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

Analysis of Algorithms I: Binary Search Trees

DATA STRUCTURES USING C

Introduction to: Computers & Programming: Review for Midterm 2

Class Notes CS Creating and Using a Huffman Code. Ref: Weiss, page 433

First programming project: Key-Word indexer

Symbol Tables. Introduction

ER E P M A S S I CONSTRUCTING A BINARY TREE EFFICIENTLYFROM ITS TRAVERSALS DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF TAMPERE REPORT A

PES Institute of Technology-BSC QUESTION BANK

Lecture Notes on Binary Search Trees

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

Full and Complete Binary Trees

10CS35: Data Structures Using C

Section IV.1: Recursive Algorithms and Recursion Trees

Binary Trees and Huffman Encoding Binary Search Trees

From Last Time: Remove (Delete) Operation

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

TREE BASIC TERMINOLOGIES

Data Structures Fibonacci Heaps, Amortized Analysis

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

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

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

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

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

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

Algorithms Chapter 12 Binary Search Trees

The ADT Binary Search Tree

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

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

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

Algorithms and Data Structures Written Exam Proposed SOLUTION

APP INVENTOR. Test Review

Introduction to Programming System Design. CSCI 455x (4 Units)

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

Heaps & Priority Queues in the C++ STL 2-3 Trees

GRAPH THEORY LECTURE 4: TREES

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

csci 210: Data Structures Recursion

Algorithms and Data Structures

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

Sample Questions Csci 1112 A. Bellaachia

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other 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:

Analysis of Binary Search algorithm and Selection Sort algorithm

Binary Heap Algorithms

C H A P T E R Regular Expressions regular expression

Lecture Notes on Binary Search Trees

Java Software Structures

Data Structures in Java. Session 15 Instructor: Bert Huang

Chapter 14 The Binary Search Tree

Haberdashers Adams Federation Schools

Dynamic Programming Problem Set Partial Solution CMPSC 465

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

3.2. Solving quadratic equations. Introduction. Prerequisites. Learning Outcomes. Learning Style

Binary Search Trees 3/20/14

FOPR-I1O23 - Fundamentals of Programming

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

Welcome to Introduction to programming in Python

Machine Learning. CUNY Graduate Center, Spring Professor Liang Huang.

CSE373: Data Structures & Algorithms Lecture 14: Hash Collisions. Linda Shapiro Spring 2016

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

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

WRITING PROOFS. Christopher Heil Georgia Institute of Technology

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

2. (a) Explain the strassen s matrix multiplication. (b) Write deletion algorithm, of Binary search tree. [8+8]

How To Create A Tree From A Tree In Runtime (For A Tree)

Lecture 1: Data Storage & Index

Time needed. Before the lesson Assessment task:

International Journal of Advanced Research in Computer Science and Software Engineering

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

Hash Tables. Computer Science E-119 Harvard Extension School Fall 2012 David G. Sullivan, Ph.D. Data Dictionary Revisited

INFORMATION AND REGULATIONS FOR SELF-PACED PROGRAMMING COURSES CS 3S, CS 9ABCDEFGH, CS 47ABC. Spring Semester 2008

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

Data Structures and Algorithms Written Examination

Sample Syllabus (C++) CSCI 1301 Introduction to Programming Principles

Data Structure with C

ML for the Working Programmer

How to create a blog or website

ALLIED PAPER : DISCRETE MATHEMATICS (for B.Sc. Computer Technology & B.Sc. Multimedia and Web Technology)

A Comparison of Dictionary Implementations

Chapter 13: Query Processing. Basic Steps in Query Processing

Data Structures. Level 6 C Module Descriptor

Notepad++ The COMPSCI 101 Text Editor for Windows. What is a text editor? Install Python 3

Lab 1: Introduction to C, ASCII ART and the Linux Command Line Environment

Mathematical Induction. Lecture 10-11

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

Lecture Notes on Linear Search

CS 2302 Data Structures Spring 2015

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

Sequential Data Structures

Transcription:

COMPSCI 105 S2 C - Assignment Two 1 of 7 Computer Science COMPSCI 105 S2 C - Assignment 2 Due date: Friday, 23 rd October 7pm 100 marks in total = 7.5% of the final grade Assessment Due: Friday, 23 rd October 2015 (7:00 pm) Worth: 7.5% of your final mark Resources and Submission A copy of this handout, as well as relevant source files for questions in this assignment can be obtained from the 105 assignment section of the course web page on the web: https://www.cs.auckland.ac.nz/courses/compsci105s2c/assignments/ Using the web drop box (https://adb.auckland.ac.nz/), please submit the following files for this assignment: o Part 1, Q1: A2Q1abcd001.py where abcd001 is your upi o Part 1, Q2: A2Q2abcd001.py where abcd001 is your upi o Part 2, Q3: ResultTable.py o Part 2, Q4: A2Q4Proof.pdf o Part 2, Q5: ReconstructTree.py Please note: o All submitted files must contain your name and UPI o All programs you submit must display your UPI in the first line of the program output o All your files should be able to be compiled without requiring any editing. o All your files should include a good layout structure, meaningful variable names, and comments explaining the key ideas of your solution o All required resources are found on the assignment section in the course web page Warning The work done on this assignment must be your own work. Think carefully about any problems you come across, and try to solve them yourself before you ask anyone for help. Under no circumstances should you take or pay for an electronic copy of someone else s work, modify it, and submit it as your own. Penalties for copying will be severe to avoid being caught copying, don t do it. To ensure you are not identified as cheating you should follow these points: o Always do individual assignments by yourself. o Never show your code to another person. o Never put your code in a public place (e.g., forum, Facebook, your web site). o Never leave your computer unattended. You are responsible for the security of your account.

COMPSCI 105 S2 C - Assignment Two 2 of 7 Part I of Assignment Aims of Part I of Assignment Solving problems using recursion and hash tables. Q1. Turtle Graphics (35 Marks) Use the built in Python turtle to draw the following shape (you can see the turtle sitting in the middle of the shape, facing up, after the shape is drawn): Actually, your program should not always draw the shape above. The example above is a Level 5 version of the shape. The user should be prompted for which level of the shape they would like to draw (with 1 being the smallest valid input). Examples of several smaller versions of the shape are shown below: Level 1 Level 2 Level 3 The size of the inner I shape, which is the complete Level 1 shape, is 200 pixels wide and 200 pixels high. This inner I is the same size in all versions of the shape, even

COMPSCI 105 S2 C - Assignment Two 3 of 7 though the complete shape grows larger as the level increases. Your program should start by asking the user to enter the level they would like to draw: Draw shape at level: Once the drawing of the shape at the specified level is complete, the turtle should end up in the center of the shape facing north. Program Skeleton import turtle def draw_shape(t,...): def q1(): my_win = turtle.screen() my_turtle = turtle.turtle() my_turtle.speed(0) my_turtle.left(90) my_turtle.up() n = int(input('draw shape at level: ')) draw_shape(my_turtle,...) my_win.exitonclick() Marking Scheme for Question 1 Turtle ends up in centre Draws I figure correctly for level 1 Draws I figure correctly for level 2 Draws I figure correctly for higher levels Put all the code that is needed to run the program in A2Q1abcd001.py, where abcd001 is your upi. Q2. Hash Table (15 Marks) A hashtable has been created, and is using linear probing with the hash function: h(key) = key % size where size is the capacity of the hashtable. A list of keys are then inserted, in order, into the hashtable. Once the keys have been inserted, you need to calculate the worst index position in the hashtable. That is, the index position in the hashtable that will generate the longest probe sequence. You must write a function called q2() which will be passed two inputs: the size of the hashtable the list of keys to be inserted, in order

COMPSCI 105 S2 C - Assignment Two 4 of 7 Your function must return a list which contains the worst index position (or positions, if there are multiple indexes that would lead to equally long probe sequences in this case the index values in the list must be in increasing order). For example, the following code values = [25, 32, 88, 10, 35, 11] worst = q2(11, values) print(worst) should produce the output: [10] As another example, the code: values = [4, 9, 12, 3, 7, 26, 16, 20, 11] worst = q2(13, values) print(worst) should produce the output: [3, 7, 11] Marking Scheme for Question 2 Returns correct worst index t Returns correct worst indices Put all the code that is needed to run the program in A2Q2abcd001.py, where abcd001 is your upi. Part II of Assignment Aims of Part II of Assignment Understanding and solving problems using sorting algorithms and trees Q3. Ordered Structures (15 Marks) In this exercise we develop an algorithm to sort the result table of a soccer competition. Input is a file with comma separated values. Each line of the file contains the name of a team, the number of points of the team and the goals for and against the team. The goal difference is computed as goals for a team minus goals against the team Please write a program ResultTable.py, which reads such a file and outputs it in sorted order. The sorting order is defined as follows: higher points means a higher rank, if the points are equal then a higher goal difference corresponds to a higher rank. If the points and goal difference are equal then a higher number of goals for the team corresponds to a higher rank. If two teams have exactly the same number of points, goal difference, and goals for the team, then the order of these two teams is undefined (i.e. either could be in front).

COMPSCI 105 S2 C - Assignment Two 5 of 7 The images below show an example of an input file (left) and the corresponding output (right) of your program. Example: In the input file (left) you can see: Schalke 04, 16, 14, 3 => 16 points, 14 goals for and 3 goals against => goal difference is 11 Borrusia Dortmund, 16, 15, 4 => 16 points, 15 goals for and 4 goals against => goal difference is 11 In the output (right) Borrusia Dortmund is ranked higher because both teams have the same number of points and goal difference, but Borrusia Dortmund has scored more goals then Schalke 04. Please note: - Your program should display the rank, team name, points, goal difference, and for and against goals separated by a :. The output should be neatly formatted as shown above. For formatting I use the string format function (Python tutorial section 7.1 See CS105 Resources page), but you can use any solution you like. - You can use any sorting method you like. The simplest solution is probably to use one of the sorting methods from the lecture and modify the comparison operation appropriately. However, it is also possible to use the in-build sort() method and provide a suitable key function, or to overload one of the comparison operators. - In order to see whether your output is correctly formatted you need to use a typewrite front (i.e. where every character has the same width), e.g. Courier New. If you use the Windows console this should be automatically the case. If you use Notepad++ you can change the font using the menu item Plugins->NppExec->Change Console Font. Marking Scheme for Question 3 Reading input file and producing correctly formatted output Teams correctly sorted by points Teams correctly sorted by goal difference (if points equal) Teams correctly sorted by goals scored (if points and goal difference equal) 3 marks 4 marks 4 marks 4 marks Put all the code that is needed to run the program in ResultTable.py

COMPSCI 105 S2 C - Assignment Two 6 of 7 Q4. Binary Tree (10 Marks) Prove that the number of full nodes in a binary tree is one less than the number of leaves. Submit your proof as a pdf-file Ass2Proof.pdf. Please note: I don t expect a formal mathematical proof. However, your proof should demonstrate understanding of the key ideas behind this proof, and an ability to explain technical issues using English language. Hint: It is a good idea to use a process called Structural induction Show that the claim is valid for a tree with one node (i.e. just the root) Assume the claim is valid for a tree with n nodes. Now show that the claim is valid for a tree with (n+1) nodes. In order to show this, consider any leaf node of the tree. Removing the leaf will give a tree with n nodes. With our assumption for that tree the number of full nodes is one less than the number of leaves. Now show that by adding the leaf back the number of full nodes is still one less than the number of leaves. Marking Scheme for Question 4 Correct argument that claim is true for tree with one node (root) Correct argument that claim is true for all trees Acceptable use of English language and terminology 2 marks 3 marks Create a pdf-file Ass2Proof.pdf with your proof. If you write your proof by hand (e.g. in order to add illustrations) make sure that your handwriting in the resulting scanned document is readable. Markers are instructed not to give points for something they cannot read. Q5. Binary Trees (25 Marks) In this exercise we develop a method to reconstruct a binary tree from an inorder and preorder traversal sequence of the unknown tree. Please write a program ReconstructTree.py, which lets the user input the inorder and preorder traversal sequences and from this reconstructs the corresponding binary tree and outputs it using the print() method. Please note: - All nodes of the tree contain a single character and the inorder and preorder traversal sequences are strings formed by these characters in the corresponding order. - Note that in our examples the reconstructed tree is a binary tree, but not a binary search tree - Please construct the tree using a list-of-list representation and use the supplied file ListBinaryTree.py Example: The screenshots below show on the left two examples of the program with input and corresponding output (left). The images on the right show graphical representations of the resulting trees (not required for the assignment)

COMPSCI 105 S2 C - Assignment Two 7 of 7 Marking Scheme for Question 5 Correct root of tree Correct level 1 of tree Correct level 2 of tree All higher levels of tree are correct Put all the code that is needed to run the program in ReconstructTree.py