1 Data Structures CSCI 132, Spring 2014 Lecture 22 Searching.

Slides:



Advertisements
Similar presentations
Sorted Lists CS Data Structures Sections 4.1, 4.2 & 4.3.
Advertisements

Stacks, Queues, and Linked Lists
DATA STRUCTURES USING C++ Chapter 5
Lists: An internal look
Chapter6 LISTS AND STRINGS. Outline 1. List Specifications 2. List Implementations (a) Class Templates (b) Contiguous (c) Simply Linked (d) Simply Linked.
CSE Lecture 12 – Linked Lists …
Linked Lists CENG 213 Data Structures.
Binary Searching.
Monday, 11/11/02, Slide #1 CS 106 Intro to Comp. Sci. 1 Monday, 11/11/02  Questions? HW 04 due today at 5.  Today – Lists and an introduction to searching.
1 Data Structures CSCI 132, Spring 2014 Lecture 8 Implementing Queues.
1 Data Structures CSCI 132, Spring 2014 Lecture 37 Binary Search Trees II.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Contd... Objectives Explain the design, use, and operation of a linear list Implement a linear.
Chapter 4 ADT Sorted List.
1 Chapter 9 Maps and Dictionaries. 2 A basic problem We have to store some records and perform the following: add new record add new record delete record.
Chapter 7 Chapter 7 SEARCHING`. Outline 1. Introduction, Notation 2. Sequential Search 3. Binary Search 4. Comparison Trees 5. Lower Bounds 6. Asymptotics:
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
Void write_method() /* Post: A short string identifying the abstract data type used for the structure is written. */ Project 2 -- Analysis.
Data Structures Topic #3. Today’s Agenda Ordered List ADTs –What are they –Discuss two different interpretations of an “ordered list” –Are manipulated.
Preliminaries Multiway trees have nodes with greater than two children. Multiway trees of order k have nodes with most k children Trees –For all.
Introduction - The Need for Data Structures Data structures organize data –This gives more efficient programs. More powerful computers encourage more complex.
Binary Search Trees Chapter 6.
INTRODUCTION TO AVL TREES P. 839 – 854. INTRO  Review of Binary Trees: –Binary Trees are useful for quick retrieval of items stored in the tree –order.
Data Structures: CSCI 362 – Stack Implementation Data Structures: CSCI 362 – Stack Implementation lecture notes adapted from Data Structures with C++ using.
Data Structures Using C++ 2E
HASHING Section 12.7 (P ). HASHING - have already seen binary and linear search and discussed when they might be useful (based on complexity)
Searching Chapter 7. Objectives Introduce sequential search. – Calculate the computational complexity of a successful search. Introduce binary search.
Data Structures Using C++1 Chapter 9 Search Algorithms.
C++ Classes and Data Structures Jeffrey S. Childs
A Review of Binary Search Trees Dr. Gang Qian Department of Computer Science University of Central Oklahoma.
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.
CSC 211 Data Structures Lecture 13
1 Data Structures CSCI 132, Spring 2014 Lecture 36 Binary Search Trees.
Data Structure Introduction.
CPSC 252 Operator Overloading and Convert Constructors Page 1 Operator overloading We would like to assign an element to a vector or retrieve an element.
WEEK 1 Hashing CE222 Dr. Senem Kumova Metin
1 Data Structures CSCI 132, Spring 2014 Lecture 20 Linked Lists.
1 Data Structures CSCI 132, Spring 2014 Lecture 19 Lists.
Computer Science Projects Internal Assessment. Mastery Item Claimed Justification Where Listed Random Access File – Searching Lines P. 53 Random.
Sets of Digital Data CSCI 2720 Fall 2005 Kraemer.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
1 Data Structures CSCI 132, Spring 2014 Lecture 34 Analyzing Hash Tables.
Chapter 6 LISTS AND STRINGS 1. List Definition 2. List Implementations (a) Class Templates (b) Contiguous (c) Simply Linked (d) Doubly Linked 3. Linked.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
Searching ( 搜索) 1. Introduction 2. Sequential Search 3. Binary Search 4. Comparison Trees.
1 Data Structures CSCI 132, Spring 2014 Lecture 33 Hash Tables.
1 Data Structures CSCI 132, Spring 2014 Lecture23 Analyzing Search Algorithms.
1 Chapter 4 Unordered List. 2 Learning Objectives ● Describe the properties of an unordered list. ● Study sequential search and analyze its worst- case.
© 2006 Pearson Addison-Wesley. All rights reserved15 A-1 Chapter 15 External Methods.
Kruse/Ryba Ch071 Object Oriented Data Structures Searching STL Vector Sequential Search Binary Search Comparison Trees.
Advanced Data Structures Lecture 1
Chapter 4 Unordered List.
Chapter 4 The easy stuff.
CSCI-255 LinkedList.
Andy Wang Object Oriented Programming in C++ COP 3330
MIS 215 Module 1 – Unordered Lists
CS Data Structures Chapter 8 Lists Mehmet H Gunes
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
CISC/CMPE320 - Prof. McLeod
Data Structures & Algorithms
CSI 1340 Introduction to Computer Science II
Eighth step for Learning C++ Programming
Dynamic allocation (continued)
Data Structures and Algorithms Memory allocation and Dynamic Array
Applications of Arrays
Presentation transcript:

1 Data Structures CSCI 132, Spring 2014 Lecture 22 Searching

2 Advantages of linked lists Advantages: Dynamic memory allocation--Use only as much memory as needed (But links themselves take up space). Can change or insert or delete items in middle of the list fairly quickly. Traversing the list to find the position can be faster than shifting many large list items (as you would in contiguous lists). Disadvantages: Linked lists are not good for random access--when you want to access entries from many different positions in the list.

3 Linked vs. Contiguous lists Use contiguous lists when: Entries are small Size of list is known when program is written. Few insertions and deletions occur except at the end of the list. Random access is important. Use linked lists when: Entries are large. The size of the list is not known in advance. Flexibility in inserting, deleting or rearranging elements is necessary.

4 Finding Data in a List Many computer programs must find data in a list of items. Often, one piece of information is known and we want to retrieve information associated with it. For example, the name is known, and we want the associated phone number. The piece of information we know (and are searching for) is called the key. The associated information is contained in a record.

5 Assumptions and requirements Every record is associated with one key. Keys can be compared for equality or relative ordering. Records can be compared to each other or to keys by converting a record to its associated key. We are (for purposes of analyzing run times) working with contiguous lists.

6 The Key class // Definition of a Key class: class Key{ public: // Add any constructors and methods for key data. private: // Add declaration of key data members here. }; // Declare overloaded comparison operators for keys. bool operator == (const Key &x, const Key &y); bool operator > (const Key &x, const Key &y); bool operator < (const Key &x, const Key &y); bool operator >= (const Key &x, const Key &y); bool operator <= (const Key &x, const Key &y); bool operator != (const Key &x, const Key &y);

7 The Record class // Definition of a Record class: class Record{ public: Record( ); //constructor operator Key( ); // implicit conversion from Record to Key. // Add any constructors and methods for Record objects. private: // Add data components. };

8 Example of Record and Key class Key { public: Key (int x = 0); int the_key( ) const;//Return value of key private: int key; }; class Record { public: Record(Key aKey, int data1, int data2); operator Key( ); private: Key theKey; int some_data; int some_other_data; };

9 Implementing Key and Record Key::Key (int x ) { key = x; } int Key:: the_key( ) const { return key; } Record::Record(Key aKey, int data1, int data2){ theKey = aKey; some_data = data1; some_other_data = data2; } Record::operator Key( ) { return theKey; }

10 Implementing boolean operator overrides. class Key { public: Key (int x = 0); int the_key( ) const;//Return value of key private: int key; }; //in the key.h file bool operator == (const Key &x, const Key &y); //Other comparisons here //Implementation of overloaded comparison, in key.cc file bool operator == (const Key &x, const Key &y) { return x.the_key( ) == y.the_key( ); }

11 Sequential Search Error_code sequential_search(const List &the_list, const Key &target, int &position) /* Post: If an entry in the_list has key equal to target, then return success and the output parameter position locates such an entry within the list. Otherwise return not_present and position becomes invalid. */ { //we will work this out in class }

12 Sequential Search Error_code sequential_search(const List &the_list, const Key &target, int &position) /* Post: If an entry in the_list has key equal to target, then return success and the output parameter position locates such an entry within the list. Otherwise return not_present and position becomes invalid. */ { int s = the_list.size( ); for (position = 0; position < s; position++) { Record data; the_list.retrieve(position, data); if (data == target) return success; } return not_present; }

13 Running time of sequential search The running time of a search is approximately proportional to the number of comparisons needed to perform the search. The running time of a sequential search depends on where the item is located in the list. If the item is at the beginning, the number of comparisons needed is 1. If the item is at the end of a list of length n, the number of comparisons is n. On average, the running time is proportional to n/2 (we will show this in class).

14 Ordered Lists More efficient searches can be performed when the items are arranged in order within the list--an ordered list. class Ordered_list: public List { public: Ordered_list( ); //Make sure insert and replace keep list ordered. //An overloaded function has the same name, //but different parameters as follows Error_code insert(const Record &data); //A function in the child class with the same name and //the same parameters as one in the parent class //overrides the parent class function as follows Error_code insert(int position, const Record &data); Error_code replace(int position, const Record &data); };

15 Overloading the insert function Error_code Ordered_list :: insert(const Record &data) { int s = size( ); int position; for (position = 0; position < s; position++) { Record list_data; retrieve(position, list_data); if (list_data >= data) break; } return List :: insert(position, data); }