1 Examples of class: Recursive data structures Instructor: Mainak Chaudhuri

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

Inserting a Node into a Specified Position of a Linked List To create a node for the new item newNode = new Node(item); To insert a node between two nodes.
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.
Data Structure Lecture-5
Garfield AP Computer Science
David Weinberg presents Linked Lists: The Background  Linked Lists are similar to ArrayLists in their appearance and method of manipulation  They do.
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.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Data Structures and Algorithms (60-254)
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Variations on Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Searching Kruse and Ryba Ch and 9.6. Problem: Search We are given a list of records. Each record has an associated key. Give efficient algorithm.
Chapter 7 Sorting Part I. 7.1 Motivation list: a collection of records. keys: the fields used to distinguish among the records. One way to search for.
1 Selection Sort and Quick Sort Instructor: Mainak Chaudhuri
Recursion practice. Problem 0 Using recursion (and no arrays), write the code to read in a series of numbers (until EOF) and then print them backwards.
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.
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
1 Sorting Algorithms (Part I) Sorting Algoritms (Part I) Overview  What is Sorting?  Some Useful Array Handling Methods.  Selection Sort and its Implementation.
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
CS 1031 Queues Definition of a Queue Examples of Queues Design of a Queue Class Different Implementations of the Queue Class.
1 Graphs and Search Trees Instructor: Mainak Chaudhuri
Building Java Programs Chapter 13 Searching reading: 13.3.
CPT: Search/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss searching: its implementation,
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.
CS 1031 Tree Traversal Techniques; Heaps Tree Traversal Concept Tree Traversal Techniques: Preorder, Inorder, Postorder Full Trees Almost Complete Trees.
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
1 Linear and Binary Search Instructor: Mainak Chaudhuri
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
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.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
data ordered along paths from root to leaf
1 Arrays: Matrix Renamed Instructor: Mainak Chaudhuri
Queues. Queue Definition Ordered list with property: All insertions take place at one end (tail) All insertions take place at one end (tail) All deletions.
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
CS 206 Introduction to Computer Science II 11 / 16 / 2009 Instructor: Michael Eckmann.
The List ADT A sequence of zero or more elements A 1, A 2, A 3, … A N-1 N: length of the list A 1 : first element A N-1 : last element A i : position i.
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.
1 Insertion sort [ ] i=1 j=1 i=2 j=2 insert i=3 at j=1 [ ] i=3 j=1 insert i=4 at j=0 [
Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Priority Queue.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
LINKED LIST’S EXAMPLES Salim Malakouti. Linked List? 523 Pointer Node ValuePointer.
Selection Sort main( ) { int a[ ] = { 17, 6, 13,12, 2 } ; int i, j, t ; for ( i = 0 ; i
Week 4 - Friday.  What did we talk about last time?  Continued doubly linked list implementation  Linked lists with iterators.
Java linked list.
1 Arrays: Matrix Renamed Instructor: Mainak Chaudhuri
CS 46B: Introduction to Data Structures July 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
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.
Arrays and Sorting. Process Open a file that contains integers, one per line. Read each line, convert to short and store each into an array Sort the array.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Chapter 12 – Data Structures
QueueStack CS1020.
Design & Analysis of Algorithm Priority Queue
Announcements Project checkpoint next week after lab sessions
Stack and Queue APURBO DATTA.
Lecture No.02 Data Structures Dr. Sohail Aslam
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.
Sorting and Searching -- Introduction
Presentation transcript:

1 Examples of class: Recursive data structures Instructor: Mainak Chaudhuri

2 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

3 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 and in the worst case) Insertion and deletion involve changing a few references (independent of n)

4 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

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

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

7 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(); }

8 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); }

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

10 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

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

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

13 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

14 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 return this; } } // next slide

15 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

16 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

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

18 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

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