Announcements Project checkpoint next week after lab sessions

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists.
Section 2.5 Single-Linked Lists. A linked list is useful for inserting and removing at arbitrary locations The ArrayList is limited because its add and.
Linked Lists. 2 Merge Sorted Lists Write an algorithm that merges two sorted linked lists The function should return a pointer to a single combined list.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
1 Lecture 21 Introduction to Sorting I Overview  What is Sorting?  Some Useful Array Handling Methods.  Selection Sort and its Implementation.  Brief.
CS 206 Introduction to Computer Science II 10 / 01 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
1 Graphs and Search Trees Instructor: Mainak Chaudhuri
CSE 131 Computer Science 1 Module 9: Linked Lists Using references to link objects Basic operations on linked lists Implementing a linked list of integers.
1 Linear and Binary Search Instructor: Mainak Chaudhuri
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
1 Arrays: Matrix Renamed Instructor: Mainak Chaudhuri
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
1 Improving search performance: Hash tables Instructor: Mainak Chaudhuri
Introduction to Data Structures and Algorithms
Chapter 5 Linked Lists II
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
Linked lists. Data structures to store a collection of items Data structures to store a collection of items are commonly used Typical operations on such.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
LINKED LIST’S EXAMPLES Salim Malakouti. Linked List? 523 Pointer Node ValuePointer.
1 Arrays and Methods Java always passes arguments by value – that is a copy of the value is made in the called method and this is modified in the method.
1 Examples of class: Recursive data structures Instructor: Mainak Chaudhuri
1 Arrays: Matrix Renamed Instructor: Mainak Chaudhuri
3/21/2016IT 2751 Tow kinds of Lists Array What can be done? What can be easily done? student 1 student 2 student 3 student 4 Linked List student 2 student.
Linked Lists Source: presentation based on notes written by R.Kay, A. Hill and C.Noble ● Lists in general ● Lists indexed using pointer arrays ● Singly.
Queues1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich,
Recursion.
Insertion sort Loop invariants Dynamic memory
Review Array Array Elements Accessing array elements
Single-Linked Lists.
Linked List :: Basic Concepts
CS 201 Data Structures and Algorithms
Vectors 5/31/2018 9:25 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
QueueStack CS1020.
Lectures linked lists Chapter 6 of textbook
Design & Analysis of Algorithm Priority Queue
Lecture 14 Searching and Sorting Richard Gesick.
CSC 222: Object-Oriented Programming
Stack and Queue APURBO DATTA.
Chapter 13: Searching and Sorting
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Arrays and Linked Lists
Selection Sort Find the smallest value in the array. Put it in location zero. Find the second smallest value in the array and put it in location 1. Find.
Data Structures ADT List
Queues 12/3/2018 Queues © 2014 Goodrich, Tamassia, Goldwasser Queues.
Lecture 11 Searching and Sorting Richard Gesick.
Selection Insertion and Merge
Can we solve this problem?
Problem Understanding
Chapter 16 Linked Structures
Sorting Chapter 8.
Data Structures ADT List
Linked Lists.
Data Structures & Algorithms
Data Structures & Algorithms
General List.
Exercise 5 1. We learned bubble sort during class. This problem requires you to modify the code for bubble sorting method to implement the selection sorting.
Why are arrays useful? We can use arrays to store a large amount of data without declaring many variables. Example: Read in a file of 1000 numbers, then.
Sorting and Searching -- Introduction
Recursion Chapter 12.
Module 8 – Searching & Sorting Algorithms
Applications of Arrays
Java Coding 6 David Davenport Computer Eng. Dept.,
Problem Understanding
Presentation transcript:

Announcements Project checkpoint next week after lab sessions We will look at your Java programs written so far Bring a one-page report describing what you have done till now Only Tuesday group will have lab this week Class on 25th October (Thursday) is canceled

Examples of class: Recursive data structures Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in

Recursive data structures Refers to a “connected” set of similar objects Object A “connects” or “refers” to object B For example, in a list of integers you can imagine each integer in the list as an object and each object connects to its next object thereby forming a list The structure is called recursive because an object of type class A connects to another object of the same class type Very important to build complex connected structures e.g., a network of cities where a “connection” could mean rail or road

List structure Why not just an array? Basic operations needed on a list: search, insertion, deletion All these are time consuming in arrays e.g., searching is O(n), insertion at head is O(n), and deletion from head is also O(n) With lists, arbitrary insertion and deletion can be made in O(1) time; searching is still costly (we will discuss techniques to improve it on average) Insertion and deletion involve changing a few references (independent of n)

List structure Consider insertion sort For each element, we compared it with all elements on its left until a comparison failed and then shifted up all the elements on the right to make a hole for insertion If the numbers are stored in a list, the last shift-up step can be completely avoided Just insert one element and delete another: O(1) Recall that with a reverse-sorted array, the number of comparisons is minimum while with a sorted array, the number of comparisons is maximum But former spends more time in shift-up leading to both cases requiring roughly the same amount of time With a list implementation, the first case will indeed be the fastest Program = Algorithm + Data structure

List structure public class IntegerList { private int data; private IntegerList next; public IntegerList (int x, IntegerList rest) { data = x; next = rest; } // next slide

List structure public IntegerList (int x) { data = x; next = null; } public int GetHead () { return data; // next slide

List structure public IntegerList GetBody () { return next; } public int Length () { if (next==null) return 1; return (1 + GetBody().Length()); public int GetTail () { if (next==null) return data; return GetBody().GetTail();

List structure public IntegerList Search (int x) { if (data==x) return this; if (next==null) return null; return GetBody().Search(x); } public int ExtractElement (int index) { // This is slower compared to array if (index==0) return data; if (next==null) { System.out.println (“Query index too large!”); return -1; return GetBody().ExtractElement (index-1);

List structure public void Enqueue (int x) { if (next==null) { next = new IntegerList (x); } else { GetBody().Enqueue(x); // next slide

List structure public IntegerList Dequeue () { return next; } public IntegerList Reverse () { if (next==null) return this; IntegerList temp = GetBody().Reverse(); temp.Enqueue (data); return temp; // next slide

List structure public void SetBody (IntegerList x) { next = x; }

List structure public void Print () { if (next==null) { System.out.println (data); } else { System.out.print (data + “, ”); GetBody().Print(); // next slide

List structure public IntegerList SearchAndDelete (int x) { // return the new head of the list IntegerList temp = this; IntegerList prev = null; while ((temp != null) && (temp.GetHead() != x)) { prev = temp; temp = temp.GetBody(); } // next slide

List structure if (temp != null) { // found x if (prev == null) { // first one is x return temp.GetBody(); // new head of list } else { prev.SetBody (temp.GetBody()); return this; else { // did not find x } // next slide

List structure public IntegerList SortedInsert (int x) { // return the new head of the list IntegerList temp = this; IntegerList prev = null; IntegerList newBorn = new IntegerList (x); while ((temp != null) && (temp.GetHead() < x)) { prev = temp; temp = temp.GetBody(); } // next slide

List structure if (temp != null) { if (prev == null) { // Insert at head newBorn.SetBody(this); return newBorn; } else { // Insert in middle prev.SetBody(newBorn); newBorn.SetBody(temp); return this; // next slide

List structure else { // Insert at end prev.SetBody(newBorn); return this; } } // end class Sometimes a tail reference helps Could enqueue at end without traversing the entire list

List structure class ListBuilder { public static void main (String a[]) { IntegerList iL = new IntegerList (5); iL = new IntegerList (6, iL); iL = new IntegerList (-2, iL); iL.Enqueue (13); System.out.println (“Length: ” + iL.Length()); System.out.println (“Position 2: ” + iL.ExtractElement (2)); iL.Print (); // next slide

List structure iL = iL.Reverse(); iL.Print(); iL = iL.SearchAndDelete(-2); iL = iL.SortedInsert(10); }

Maintaining a tail reference Want to have a “global” tail reference shared by all the objects in the list Need to declare tail as a static variable These are also called class variables because these are attached to a class as opposed to specific objects

Maintaining a tail reference public class IntegerListWithTail { private int data; private IntegerListWithTail next; private static IntegerListWithTail tail = null; // Could maintain a global length also private static int length = 0; public IntegerListWithTail (int x, IntegerListWithTail rest) { data = x; next = rest; if (tail == null) tail = this; length++; } // Next slide

Maintaining a tail reference public IntegerListWithTail (int x) { data = x; next = null; tail = this; length++; } public static int GetTail () { if (tail == null) return -1; else return tail.GetHead(); // Next slide

Maintaining a tail reference public static int Length() { return length; } public IntegerListWithTail SearchAndDelete (int x) { // return the new head of the list IntegerListWithTail temp = this; IntegerListWithTail prev = null; while ((temp != null) && (temp.GetHead() != x)) { prev = temp; temp = temp.GetBody(); // Next slide

Maintaining a tail reference if (temp != null) { // found x length--; if (prev == null) { // first one is x return temp.GetBody(); // new head of list } else { prev.SetBody (temp.GetBody()); // update tail if (temp.GetBody() == null) tail = prev; return this; // Next slide

Maintaining a tail reference else { // did not find x return this; } public int GetHead () { return data; public IntegerListWithTail GetBody() { return next; // Next slide

Maintaining a tail reference public void SetBody (IntegerListWithTail x) { next = x; } } // end class class ListBuilder { public static void main (String a[]) { IntegerListWithTail iL = new IntegerListWithTail (5); iL = new IntegerListWithTail (6, iL); iL = new IntegerListWithTail (-2, iL); System.out.println (“Length: ” + IntegerListWithTail.Length()); // Next slide

Maintaining a tail reference iL = iL.SearchAndDelete(-2); System.out.println(“Tail: ” + IntegerListWithTail.GetTail()); iL = iL.SearchAndDelete(5); System.out.println(“Length: ” + IntegerListWithTail.GetLength()); }