U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 

Slides:



Advertisements
Similar presentations
Singly Linked Lists What is a singly-linked list? Why linked lists?
Advertisements

Stacks, Queues, and Linked Lists
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)
COSC 1P03 Data Structures and Abstraction 10.1 The List If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping.
Ics202 Data Structures. hh tail head (b) LinkedList head tail Element datum next 3 Integer Element datum next 1 Integer Element datum next 4 Integer.
Introduction to Design Patterns What is Design Pattern? The Container Pattern. The Visitor Pattern. The SearchableContainer Pattern. The Enumeration Pattern.
Ordered Containers Cmput Lecture 21 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Chapter 4 ADT Sorted List.
Queues What are queues? Queue Implementation Using Linked Lists. Applications of Queues.
Introduction to Stacks What is a Stack Stack implementation using arrays. Application of Stack.
1 Hashing Techniques: Implementation Implementing Hash Functions Implementing Hash Tables Implementing Chained Hash Tables Implementing Open Hash Tables.
Introduction to Stacks What are Stacks? Stack implementation using arrays. Stack application.
Binary Heaps What is a Binary Heap? Array representation of a Binary Heap MinHeap implementation Operations on Binary Heaps: enqueue dequeue deleting an.
Introduction to Stacks What is a Stack Stack implementation using array. Stack implementation using linked list. Applications of Stack.
1 Introduction to Design Patterns What is Design Pattern? The Container Pattern. The Iterator Pattern. The Visitor Pattern. The SearchableContainer Pattern.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
Queues What is a queue? Queue Implementations: –As Array –As Circular Array –As Linked List Applications of Queues. Priority queues.
1 Introduction to Stacks What is a Stack? Stack implementation using array. Stack implementation using linked list. Applications of Stacks.
Introduction to Design Patterns What is Design Pattern? The Container Pattern. The Visitor Pattern. The SearchableContainer Pattern. The Iterator Pattern.
Lecture 11 Binary Heap King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Introduction to Design Patterns What is Design Pattern? The Container Pattern. The Iterator Pattern. The Visitor Pattern. The SearchableContainer Pattern.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Ics202 Data Structures. U n i v e r s i t y o f H a i l 1. Stacks top push (8)push (2)
U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 
Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques.
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.
The Java Collections Framework (JCF) Introduction and review 1.
Information and Computer Sciences University of Hawaii, Manoa
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
CS 162 Intro to Programming II Searching 1. Data is stored in various structures – Typically it is organized on the type of data – Optimized for retrieval.
IMPLEMENTING ARRAYLIST – Part 2 COMP 103. RECAP  Abstract Classes – overview, details in 2 nd year  Implementing the ArrayList: size(), get(), set()
Ordered Containers CMPUT Lecture 19 Department of Computing Science University of Alberta ©Duane Szafron 2003 Some code in this lecture is based.
Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 
Ordered Lists and Sorted Lists Chapter 7. Ordered Lists Array version vs. Linked-list version.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
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.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 20 Ordered.
Recursive Objects  an object that holds a reference to its own type is a recursive object  linked lists and trees are classic examples in computer science.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
LINKED LISTS.
U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 
The List ADT.
Queues.
Chapter 5 Ordered List.
Recap: Solution of Last Lecture Activity
Efficiency of in Binary Trees
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.
Java collections library
Queues What is a queue? Queue Implementations: As Array
Chapter 5 Ordered List.
null, true, and false are also reserved.
Introduction to Design Patterns
Lecture 5 Stacks King Fahd University of Petroleum & Minerals
Chapter 5 Ordered List.
Binary Search Trees Chapter 9 2/22/2019 B.Ramamurthy.
Collections Framework
Binary Search Trees Chapter 9 2/24/2019 B.Ramamurthy.
Introduction to Stacks
Recursive Objects Singly Linked Lists.
Introduction to Stacks
8 List ADTs List concepts. List applications.
Introduction to Design Patterns
Queues What is a queue? Queue Implementations: As Array
Introduction to Design Patterns
Presentation transcript:

U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 

U n i v e r s i t y o f H a i l 2 Outline 1. Ordered Lists Array Implementation Linked-List Implementation Applications 2. Sorted Lists Array Implementation Linked-List Implementation Applications ICS 202  2009 Summer  Data Structures and Algorithms  Amani Mahajoub Omer

U n i v e r s i t y o f H a i l Lec 1

U n i v e r s i t y o f H a i l 4 Comparable Container SearchableContainer OrderedList SortedList AbstractObject AbstractContainer OrderedListAsArray OrderedListAsLinkedList SortedListAsArray SortedListAsLinkedList AbstractSearchableContainer

U n i v e r s i t y o f H a i l 5 1. Ordered List An ordered list is a list in which the order of the items is significant. The items in an ordered list are not necessarily sorted. It is possible to change the order of items and still have a valid ordered list. public interface OrderedList extends SearchableContainer { Comparable get (int i); // to access the object at a given position in the ordered list. Cursor findPosition (Comparable object); } // to find the position of an object in the ordered list.

U n i v e r s i t y o f H a i l 6 1. Ordered List A searchable container is a container that supports the following additional operations:  insert, used to put objects into the container;  withdraw, used to remove objects from the container;  find, used to locate objects in the container;  isMember, used to test whether a given object instance is in the container.

U n i v e r s i t y o f H a i l 7 1. Ordered List public interface Cursor { Comparable getDatum (); // to access the object in the ordered list at the current cursor position void insertAfter (Comparable object); // to insert an object into the ordered list after the current cursor position void insertBefore (Comparable object); // to insert an object into the ordered list before the current cursor position void withdraw (); // to remove from the ordered list the object at the current cursor position }

U n i v e r s i t y o f H a i l 8 1. Ordered List: Array Implementation public class OrderedListAsArray extends AbstractSearchableContainer implements OrderedList { protected Comparable[] array; // the OrderedListAsArray inherits the field count from AbstractContainer //... }

U n i v e r s i t y o f H a i l 9 1. Ordered List: Array Implementation public class OrderedListAsArray extends AbstractSearchableContainer implements OrderedList { protected Comparable[] array; public OrderedListAsArray (int size) { array = new Comparable [size]; } public void insert (Comparable object) { if (count == array.length) throw new ContainerFullException (); array [count] = object; ++count; } //... }

U n i v e r s i t y o f H a i l Ordered List: Array Implementation public class OrderedListAsArray extends AbstractSearchableContainer implements OrderedList { protected Comparable[] array; public boolean isMember (Comparable object) { // tests weather object is member of the ordered list. for (int i = 0; i < count; ++i) if (array [i] == object) return true; return false; } public Comparable find (Comparable arg) { // locates an element in the list that matches arg for (int i = 0; i < count; ++i) if (array [i].isEQ (arg)) return array [i]; return null; }

U n i v e r s i t y o f H a i l Ordered List: Array Implementation Comparable object1 = new Int (57); Comparable object2 = new Int (57); List list = new OrderedListAsArray (1); List.insert(object1); List.isMember(object1); // returns true List.isMember(object2); // returns false Comparable object3 = list.find (object1); // object3 = object1

U n i v e r s i t y o f H a i l Ordered List: Array Implementation public class OrderedListAsArray extends AbstractSearchableContainer implements OrderedList { protected Comparable[] array; public void withdraw (Comparable object) { if (count == 0) throw new ContainerEmptyException (); int i = 0; while (i < count && array [i] != object) ++i; if (i == count) throw new IllegalArgumentException ( "object not found"); for ( ; i < count - 1; ++i) array [i] = array [i + 1]; array [i] = null; --count; }

U n i v e r s i t y o f H a i l Ordered List: Array Implementation Comparable object1 = new Int (57); Comparable object2 = new Int (57); List list = new OrderedListAsArray (1); List.insert(object1); List.withdraw(object1); // removes the object1 from the ordered list List.withdraw(object2); // displays object not found

U n i v e r s i t y o f H a i l Ordered List: Array Implementation public class OrderedListAsArray extends AbstractSearchableContainer implements OrderedList { protected Comparable[] array; protected class MyCursor implements Cursor // inner class { int offset; MyCursor (int offset) { this.offset = offset; } public Comparable getDatum () { if (offset = count) throw new IndexOutOfBoundsException (); return array [offset]; }

U n i v e r s i t y o f H a i l Ordered List: Array Implementation public class OrderedListAsArray extends AbstractSearchableContainer implements OrderedList { protected Comparable[] array; public Cursor findPosition (Comparable object) { int i = 0; while (i < count && array [i].isNE (object)) ++i; return new MyCursor (i); } public Comparable get (int offset) { if (offset = count) throw new IndexOutOfBoundsException (); return array [offset]; }

U n i v e r s i t y o f H a i l Ordered List: Array Implementation offset Cursor.insertAfter(3) offset

U n i v e r s i t y o f H a i l Ordered List: Array Implementation public class OrderedListAsArray extends AbstractSearchableContainer implements OrderedList { protected Comparable[] array; protected class MyCursor implements Cursor { int offset; public void insertAfter (Comparable object) { if (offset = count) throw new IndexOutOfBoundsException (); if (count == array.length) throw new ContainerFullException (); int insertPosition = offset + 1; for (int i = count; i > insertPosition; --i) array [i] = array [i - 1]; array [insertPosition] = object; ++count; }

U n i v e r s i t y o f H a i l Ordered List: Array Implementation offset Cursor.insertBefore(3) offset

U n i v e r s i t y o f H a i l Ordered List: Question What modification of insertAfter method must be added to implement the insertBefore method?

U n i v e r s i t y o f H a i l Ordered List: Array Implementation offset Cursor.withdraw() offset 8

U n i v e r s i t y o f H a i l Ordered List: Array Implementation public class OrderedListAsArray extends AbstractSearchableContainer implements OrderedList { protected Comparable[] array; protected class MyCursor implements Cursor { int offset; public void withdraw () // delete the item at the position specified by the cursor { if (offset = count) throw new IndexOutOfBoundsException (); if (count == 0) throw new ContainerEmptyException (); int i = offset; while (i < count - 1) { array [i] = array [i + 1]; ++i; } array [i] = null; // delete the last element --count; }

U n i v e r s i t y o f H a i l Lec 2

U n i v e r s i t y o f H a i l Ordered List: Linked-List Implementation public class OrderedListAsLinkedList extends AbstractSearchableContainer implements OrderedList { protected LinkedList linkedList; // no limit of the number of items //... }

U n i v e r s i t y o f H a i l Ordered List: Linked-List Implementation public class OrderedListAsLinkedList extends AbstractSearchableContainer implements OrderedList { protected LinkedList linkedList; // no limit of the number of items public OrderedListAsLinkedList () { linkedList = new LinkedList (); } public void insert (Comparable object) { linkedList.append (object); // add at the end of the ordered list ++count; } public Comparable get (int offset) { if (offset = count) throw new IndexOutOfBoundsException (); LinkedList.Element ptr = linkedList.getHead (); for (int i = 0; i < offset && ptr != null; ++i) ptr = ptr.getNext (); return (Comparable) ptr.getDatum (); // return the item at the position ptr }

U n i v e r s i t y o f H a i l Ordered List: Linked-List Implementation public class OrderedListAsLinkedList extends AbstractSearchableContainer implements OrderedList { protected LinkedList linkedList; // no limit of the number of items public boolean isMember (Comparable object) { for (LinkedList.Element ptr = linkedList.getHead (); ptr != null; ptr = ptr.getNext ()) if ((Comparable) ptr.getDatum () == object) return true; return false; } public Comparable find (Comparable arg) { for (LinkedList.Element ptr = linkedList.getHead (); ptr != null; ptr = ptr.getNext ()) { Comparable object = (Comparable) ptr.getDatum (); if (object.isEQ (arg)) return object; } return null; }

U n i v e r s i t y o f H a i l Ordered List: Linked-List Implementation public class OrderedListAsLinkedList extends AbstractSearchableContainer implements OrderedList { protected LinkedList linkedList; // no limit of the number of items public void withdraw (Comparable object) { if (count == 0) throw new ContainerEmptyException (); linkedList.extract (object); --count; }

U n i v e r s i t y o f H a i l Ordered List: Linked-List Implementation public class OrderedListAsLinkedList extends AbstractSearchableContainer implements OrderedList { protected LinkedList linkedList; // no limit of the number of items protected class MyCursor implements Cursor { LinkedList.Element element; // reference to an item in the linked list MyCursor (LinkedList.Element element) { this.element = element; } public Comparable getDatum () { return (Comparable) element.getDatum (); }

U n i v e r s i t y o f H a i l Ordered List: Linked-List Implementation public class OrderedListAsLinkedList extends AbstractSearchableContainer implements OrderedList { protected LinkedList linkedList; // no limit of the number of items public Cursor findPosition (Comparable arg) { LinkedList.Element ptr; for (ptr = linkedList.getHead (); ptr != null; ptr = ptr.getNext ()) { Comparable object = (Comparable) ptr.getDatum (); if (object.isEQ (arg)) break; } return new MyCursor (ptr); // returns the reference of arg }

U n i v e r s i t y o f H a i l Ordered List: Linked-List Implementation public class OrderedListAsLinkedList extends AbstractSearchableContainer implements OrderedList { protected LinkedList linkedList; // no limit of the number of items protected class MyCursor implements Cursor { LinkedList.Element element; public void insertAfter (Comparable object) { element.insertAfter (object); // provided by LinkedList class ++count; }

U n i v e r s i t y o f H a i l Ordered List: Linked-List Implementation public class OrderedListAsLinkedList extends AbstractSearchableContainer implements OrderedList { protected LinkedList linkedList; // no limit of the number of items protected class MyCursor implements Cursor { LinkedList.Element element; public void withdraw () { linkedList.extract (element.getDatum ()); --count; }

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath Sorted List A sorted list is like an ordered list: it is a serchable container that holds a sequence of objects. The position of an items in a sorted list is not arbitrary. public interface SortedList extends SearchableContainer { Comparable get (int i); // to access the object at a given position in the sorted list. Cursor findPosition (Comparable object); } // to find the position of an object in the sorted list.

U n i v e r s i t y o f H a i l Sorted List: Array Implementation public class SortedListAsArray extends OrderedListAsArray implements SortedList { // no additional field is required, the fields provided by the base class // OrderedListAsArray are suffcient //... }

U n i v e r s i t y o f H a i l Sorted List: Array Implementation insert(2)

U n i v e r s i t y o f H a i l Sorted List: Array Implementation public class SortedListAsArray extends OrderedListAsArray implements SortedList { public void insert (Comparable object) { if (count == array.length) throw new ContainerFullException (); int i = count; while (i > 0 && array [i - 1].isGT (object)) { array [i] = array [i - 1]; --i; } array [i] = object; ++count; }

U n i v e r s i t y o f H a i l Sorted List: Array Implementation public class SortedListAsArray extends OrderedListAsArray implements SortedList { protected int findOffset (Comparable object) { // binary search algorithm to find an item in a sorted list int left = 0; int right = count - 1; while (left <= right) { int middle = (left + right) / 2; if (object.isGT (array [middle])) left = middle + 1; else if (object.isLT (array [middle])) right = middle - 1; else return middle; } return -1; }

U n i v e r s i t y o f H a i l Sorted List: Array Implementation public class SortedListAsArray extends OrderedListAsArray implements SortedList { public Comparable find (Comparable object) { int offset = findOffset (object); if (offset >= 0) return array [offset]; else return null; } public Cursor findPosition (Comparable object) { return new MyCursor (findOffset (object)) { public void insertAfter (Comparable object) { throw new InvalidOperationException (); } public void insertBefore (Comparable object) { throw new InvalidOperationException (); } }; }

U n i v e r s i t y o f H a i l Sorted List: Array Implementation public class SortedListAsArray extends OrderedListAsArray implements SortedList { public void withdraw (Comparable object) { if (count == 0) throw new ContainerEmptyException (); int offset = findOffset (object); if (offset < 0) throw new IllegalArgumentException ( "object not found"); int i; for (i = offset; i < count - 1; ++i) array [i] = array [i + 1]; // move one position to the left array [i] = null; --count; }

U n i v e r s i t y o f H a i l Sorted List: Linked-List Implementation public class SortedListAsLinkedList extends OrderedListAsLinkedList implements SortedList { // No additional field is required }

U n i v e r s i t y o f H a i l Sorted List: Linked-List Implementation public class SortedListAsLinkedList extends OrderedListAsLinkedList implements SortedList { public void insert (Comparable arg) { LinkedList.Element ptr; LinkedList.Element prevPtr = null; for (ptr = linkedList.getHead (); ptr != null; ptr = ptr.getNext ()) { // to find the element that is greater than arg Comparable object = (Comparable) ptr.getDatum (); if (object.isGE (arg)) break; prevPtr = ptr; } if (prevPtr == null) linkedList.prepend (arg); else prevPtr.insertAfter (arg); // insert arg in the right position ++count; }