ITI Introduction to Computing II Lab-12

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

AITI Lecture 19 Linked List Adapted from MIT Course 1.00 Spring 2003 Lecture 26 and Tutorial Note 9 (Teachers: Please do not erase the above note)
Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part.
David Weinberg presents Linked Lists: The Background  Linked Lists are similar to ArrayLists in their appearance and method of manipulation  They do.
The Singleton Pattern II Recursive Linked Structures.
Computer Science C++ High School Level By Guillermo Moreno.
Main Index Contents 11 Main Index Contents Tree StructuresTree Structures (3 slides) Tree Structures Tree Node Level and Path Len. Tree Node Level and.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
Introduction to Stacks What is a Stack Stack implementation using array. Stack implementation using linked list. Applications of Stack.
1 Introduction to Stacks What is a Stack? Stack implementation using array. Stack implementation using linked list. Applications of Stacks.
Building Java Programs Binary Search Trees reading: 17.3 – 17.4.
CS 46B: Introduction to Data Structures July 30 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.
Information and Computer Sciences University of Hawaii, Manoa
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
ITI Introduction to Computing II Lab-6 Dewan Tanvir Ahmed University of Ottawa.
09-1 Queues and List-Based ADT Implementations Problem Set: PS3 due Wednesday, March 7 Wellesley College CS230 Lecture 09 Monday, February 26 Handout #18.
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 13 Implementing.
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
List Interface and Linked List Mrs. Furman March 25, 2010.
Iterators ITI 1121 N. El Kadri. Motivation Given a (singly) linked-list implementation of the interface List, defined as follows, public interface List.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Introduction to Collections. Collections Collections provide a way of organizing related data in a model Different types of collections have different.
ITI Introduction to Computing II Lab-5 Dewan Tanvir Ahmed University of Ottawa.
Lab-12 Keerthi Nelaturu. BitList Stores bit values in Linked List Has to be stored in right to left order Example :
CS 46B: Introduction to Data Structures July 21 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
Recursion ITI 1121 N. El Kadri. Reminders about recursion In your 1 st CS course (or its equivalent), you have seen how to use recursion to solve numerical.
CSE 143 Lecture 20 Binary Search Trees read 17.3 slides created by Marty Stepp
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
Recursive Objects Singly Linked List (Part 2) 1. Operations at the head of the list  operations at the head of the list require special handling because.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
8/3/2007CMSC 341 BTrees1 CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
Recursion Powerful Tool
Recursion Version 1.0.
Lecture 19: Binary Trees reading: 17.1 – 17.3
C++ Programming:. Program Design Including
University of Central Florida COP 3330 Object Oriented Programming
Building Java Programs
Recap: Solution of Last Lecture Activity
University of Central Florida COP 3330 Object Oriented Programming
Binary Search one reason that we care about sorting is that it is much faster to search a sorted list compared to sorting an unsorted list the classic.
Algorithm Analysis CSE 2011 Winter September 2018.
Priority Queue.
Binary Trees.
8-1.
Building Java Programs
CMSC 341 Lecture 10 B-Trees Based on slides from Dr. Katherine Gibson.
8-1.
Lecture 5 Stacks King Fahd University of Petroleum & Minerals
CSE 373 Data Structures and Algorithms
Building Java Programs
Building Java Programs
Chapter 10 1 – Binary Trees Tree Structures (3 slides)
Introduction to Stacks
CSC 143 Binary Search Trees.
Building Java Programs
Data Structures & Algorithms
Recursive Objects Singly Linked Lists.
Introduction to Stacks
Podcast Ch18d Title: Binary Search Tree Iterator
Ordered Structures Wellesley College CS230 Lecture 10
Presentation transcript:

ITI 1221. Introduction to Computing II Lab-12 Dewan Tanvir Ahmed University of Ottawa

Today’s Objective Further understanding of iterators Further understanding of recursive list processing Assignment 5

Introduction A class to store an unlimited number of bits: zeros and ones. This class, called BitList, is a linked list to store the bits.

BitList Because most operations on bits work from right to left Your implementation must store the bits in the “right-to-left” order — with the rightmost bit in the first node of the list. For example, the bits “11010” must be stored in a list such that -> 0 -> 1 -> 0 -> 1 -> 1

BitList (cont..) Complete the implementation of the class BitList. public BitList( String s ); creates a list of bits representing the input string s. The given string, s, must be a string of 0s and 1s, otherwise the constructor must throw an exception of type IllegalArgumentException. This constructor initializes the new BitList instance to represent the value in the string. Each character in the string represents one bit of the list, with the rightmost character in the string being the low order bit.

BitList (cont..) For example, given the string “1010111” the constructor should initialize the new BitList to include this list of bits: -> 1 -> 1 -> 1 -> 0 -> 1 -> 0 -> 1 If given the empty string, the constructor should create an empty list — null is a not a valid argument. The constructor must not remove the leading zeroes. For example, given “0001” the constructor must create a list of size 4: -> 1 -> 0 -> 0 -> 0

Class Node private static class Node { private int bit; // <- NEW private Node next; private Node( int bit, Node next ) { // <- ACCORDINGLY ... this.bit = bit; this.next = next; }

BitList public class BitList { // useful constants public static final int ZERO = 0; public static final int ONE = 1; // instance variables private Node first; // constructors public BitList() { first = null; }

BitList (cont..) public BitList( String s ) { // pre-condition if ( s == null ) { throw new IllegalArgumentException( "null" ); } for ( int i=0; i < s.length() ; i++ ) { int bit = s.charAt(i) - '0'; if ( ( bit != ZERO ) && ( bit != ONE ) ) throw new IllegalArgumentException( s ); addFirst( bit ); // reverses the order!

BitList (cont..) public void addFirst( int bit ) { if ( ( bit != ZERO ) && ( bit != ONE ) ) { throw new IllegalArgumentException( Integer.toString( bit ) ); } first = new Node( bit, first );

BitList (cont..) public int removeFirst() { if ( first == null ) { throw new NoSuchElementException(); } int saved = first.bit; first = first.next; return saved;

BitList (cont..) public String toString() { String str = ""; if ( first == null ) { str += ZERO; } else { Node p = first; while ( p!=null ) { str = p.bit + str; // reverses the order! p = p.next; } return str;

BitList (cont..) public Iterator iterator() { return new BitListIterator(); }

BitListIterator private class BitListIterator implements Iterator { private Node current = null; private BitListIterator() { current = null; } public boolean hasNext() { return ( ( current == null && first != null ) || ( current != null && current.next != null ) );

BitListIterator (cont..) public int next() { if ( current == null ) { current = first ; } else { current = current.next ; // move the cursor forward } throw new NoSuchElementException() ; return current.bit ;

BitListIterator (cont..) public void add( int bit ) { if ( ( bit != ZERO ) && ( bit != ONE ) ) { throw new IllegalArgumentException( Integer.toString( bit ) ); } Node newNode; if ( current == null ) { first = new Node( bit, first ); current = first; } else { current.next = new Node( bit, current.next ); current = current.next;

Iterative Create a class called Iterative. Implement the methods below. Your solutions must be iterative uses iterators

Iterative (cont..) - Complement static BitList complement( BitList in ) Write a static iterative method that returns a new BitList that is the complement of its input. The complement of 0 is 1, and vice-versa. The complement of a list of bits is a new list such that each bit is the complement of the bit at the same position in the original list. Here are 4 examples. 1011   0100     0   1     01   10     0000111   1111000

Iterative (cont..) - Complement public static BitList complement( BitList in ) { BitList result; result = new BitList(); Iterator i = in.iterator(); Iterator j = result.iterator(); if ( ! i.hasNext() ) { j.add( BitList.ONE ); } else { while ( i.hasNext() ) { int bit = i.next(); if ( bit == BitList.ONE ) { j.add( BitList.ZERO ); } return result;

Iterative (cont..) - or static BitList or( BitList a, BitList b ) Write a static iterative method that returns the or of two BitLists. This is a list of the same length as a and b such that each bit is the or of the bits at the equivalent position in the lists a and b. It throws an exception, IllegalArgumentException, if either list is empty or the lists are of different lengths. a = 10001   b = 00011   a or b = 10011

Iterative (cont..) public static BitList or( BitList a, BitList b ) { BitList result; result = new BitList(); Iterator i = a.iterator(); Iterator j = b.iterator(); Iterator k = result.iterator(); if ( ! i.hasNext() ) { throw new IllegalArgumentException( "a is empty" ); } if ( ! j.hasNext() ) { throw new IllegalArgumentException( "b is empty" ); while ( i.hasNext() ) { if ( ! j.hasNext() ) { throw new IllegalArgumentException( "b is shorter than a" ); } int iBit = i.next(); int jBit = j.next(); if ( iBit == BitList.ONE || jBit == BitList.ONE ) { k.add( BitList.ONE ); } else { k.add( BitList.ZERO ); if ( j.hasNext() ) { throw new IllegalArgumentException( "b is longer than a" ); return result;

Recursive list processing For this part of the laboratory you must write recursive implementations of the methods inside the class BitList.

Recursive list processing (cont..) void complement() Write the instance method complement() for/into the class BitList. It transforms this list changing each bit to its complement. a = new BitList( "10001" );   System.out.println( a );   -> 10001     a.complement();   System.out.println( a );   -> 01110

Recursive list processing (cont..) public void complement() { if ( first == null ) { addFirst( ONE ); } else { complement( first ); }

Recursive list processing (cont..) private void complement( Node p ) { // base case if ( p == null ) { return; } // pre-processing (could be post as well) if ( p.bit == ONE ) { p.bit = ZERO; } else { p.bit = ONE; // recursion complement( p.next );

Recursive list processing (cont..) BitList or( BitList other ) Write an instance method that returns a new BitList that is the or of this list and the argument other. This list and other remain unchanged. a = new BitList( "10001" );   b = new BitList( "00011" );     c = a.or( b );     System.out.println( a );   -> 10001   System.out.println( b );   -> 00011   System.out.println( c );   -> 10011

Recursive list processing (cont..) public BitList or( BitList other ) { if ( first == null ) { throw new IllegalArgumentException( "this list is empty" ); } if ( other.first == null ) { throw new IllegalArgumentException( "other is empty" ); BitList result; result = new BitList(); or( result, first, other.first ); return result;

Recursive list processing (cont..) private void or( BitList result, Node p, Node q ) { // pre-conditions if ( p == null && q != null ) { throw new IllegalArgumentException( "this list is shorter" ); } if ( p != null && q == null ) { throw new IllegalArgumentException( "this list is longer" ); // base case if ( p == null && q == null ) { return; // recursion or( result, p.next, q.next ); // post-processing if ( p.bit == ONE || q.bit == ONE ) { result.addFirst( ONE ); } else { result.addFirst( ZERO );

Recursive list processing (cont..) boolean equals( Object other ) Implement the method boolean equals( Object other ) that returns true if other designates a BitList, of the same size, and such that the bits at the same position in both lists are the same, and false otherwise. The implementation must be recursive. Here are examples: a = 11001   b = 11001   a.equals( b ) -> true   a = 11001   b = 11000   a.equals( b ) -> false  

Recursive list processing (cont..) public boolean equals( Object other ) { if ( ! ( other instanceof BitList ) ) { return false; } return equals( first, ( (BitList) other ).first );

Recursive list processing (cont..) private static boolean equals( Node p, Node q ) { // Base cases (stopping recursion) // Same length? if ( p == null && q != null ) { return false; } if ( p != null && q == null ) { // Reached the end of both lists? if ( p == null && q == null ) { return true; // Found a mismatch? if ( p.bit != q.bit ) { // General case return equals( p.next, q.next );

Assignment 5 Objectives Introduction to recursive list processing inside the class Familiarity with recursive list processing outside the class Introduction to binary search trees and traversing a tree

Assignment 5 – Recursive List processing Inside the class LinkedList provided, write the recursive instance method foldl. The method foldl applies the given operator to the first two elements of the list to produce a first intermediate result, it then applies the operator to the intermediate result and the third element to produce a new intermediate result, and so on. The operator is always applied to the intermediate result and the next element of the list to produce a new intermediate result for the next step in the calculation. The smallest valid list is a list of two elements. Here the operator Plus is applied to a list that contains the integers 1 and 2 (in that order). The final result is the integer 3. LinkedList xs = new LinkedList();   xs.addLast( new Integer( 1 ) );   xs.addLast( new Integer( 2 ) );   Integer result = (Integer) xs.foldl( new Plus() );

Assignment 5 – Recursive List processing For the next example, the operator Maximum is applied to a list that contains the integers 10, 0, 15 and 5 (in that order). LinkedList xs = new LinkedList();   xs.addLast( new Integer( 10 ) );   xs.addLast( new Integer( 0 ) );   xs.addLast( new Integer( 15 ) );   xs.addLast( new Integer( 5 ) );   Integer result = (Integer) xs.foldl( new Maximum() );

Assignment 5 – Recursive List processing LinkedList xs = new LinkedList();   xs.addLast( "a" );   xs.addLast( "b" );   xs.addLast( "c" );   xs.addLast( "d" );   xs.addLast( "e" );   xs.addLast( "f" );   xs.addLast( "g" );   xs.addLast( "h" );   System.out.println( xs.foldl( new Concat() ) ); Finally, the above statements are producing the following output: “a,b,c,d,e,f,g,h”.

Assignment 5 – Recursive List processing Write a recursive implementation of the instance method foldl described above. You must use the technique presented in class where a public method initiates the first call to the recursive static private method. Write a class And that implements the interface Operator. Its method apply( Object a, Object b ) assumes that both arguments are Boolean and returns the and of the two arguments (i.e. returns a Boolean representing the value true if both arguments are true and false otherwise). Write a class Product that implements the interface Operator. Its method apply( Object a, Object b ) returns the product of its two arguments. Write a class Join that implements the interface Operator. Its method apply( Object a, Object b ) returns a new Linkedlinked which is the concatenation if its two LinkedList arguments — the two arguments remain unchanged by the call.

Assignment 5 – Recursive list processing revisited The method you write for this question must be recursive, and must be static (class) methods placed in the class (A5P2.java). Therefore, you must use the technique seen in class for implementing recursive methods outside of the class.

Assignment 5 – Recursive list processing revisited add( LinkedList l, Comparable object ) (25 marks) Write a recursive method add( LinkedList l, Comparable obj ). The method first checks that l is non-decreasing. If it is not, the methode does not change l. If l is non-decreasing, the method inserts obj into l in the correct position so that the result is still non-decreasing.

Assignment 5 – Recursive list processing revisited For example, suppose l is 17.2 -> 18.1 -> 18.1 -> 22 add( l, new Double( 99 ) ) would add 99 at the end of l; add( l, new Double( -5 ) ) would add -5 at the beginning of l; add( l, new Double( 19 ) ) would add 19 just before the 22; add( l, new Double( 18.1 ) ) would add another 18.1 so that there would be three consecutively (the exact position of the new one doesn’t matter as long as it is adjacent to one or more of the existing ones). The overall result of these 4 operations would be -5 -> 17.2 -> 18.1 -> 18.1 -> 18.1 -> 19 -> 22 -> 99

Binary search tree This part of the assignment is about the implementation of a binary search tree seen in class. LinkedList toListInOrder() (25 marks) Inside the class BinarySearchTree, write the method LinkedList toListInOrder() that returns a list of all the values stored in the tree. The method must implement an in-order traversal of the tree.

The End