1 data structures csci 132, spring 2014 lecture 22 searching

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

Upload: anthony-small

Post on 21-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Data Structures CSCI 132, Spring 2014 Lecture 22 Searching

1

Data Structures

CSCI 132, Spring 2014Lecture 22Searching

Page 2: 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.

Page 3: 1 Data Structures CSCI 132, Spring 2014 Lecture 22 Searching

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.

Page 4: 1 Data Structures CSCI 132, Spring 2014 Lecture 22 Searching

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.

Page 5: 1 Data Structures CSCI 132, Spring 2014 Lecture 22 Searching

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.

Page 6: 1 Data Structures CSCI 132, Spring 2014 Lecture 22 Searching

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

Page 7: 1 Data Structures CSCI 132, Spring 2014 Lecture 22 Searching

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.};

Page 8: 1 Data Structures CSCI 132, Spring 2014 Lecture 22 Searching

8

Example of Record and Keyclass 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;};

Page 9: 1 Data Structures CSCI 132, Spring 2014 Lecture 22 Searching

9

Implementing Key and RecordKey::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;

}

Page 10: 1 Data Structures CSCI 132, Spring 2014 Lecture 22 Searching

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 filebool operator == (const Key &x, const Key &y);//Other comparisons here

//Implementation of overloaded comparison, in key.cc filebool operator == (const Key &x, const Key &y){ return x.the_key( ) == y.the_key( );}

Page 11: 1 Data Structures CSCI 132, Spring 2014 Lecture 22 Searching

11

Sequential Search

Error_code sequential_search(const List<Record> &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

}

Page 12: 1 Data Structures CSCI 132, Spring 2014 Lecture 22 Searching

12

Sequential Search

Error_code sequential_search(const List<Record> &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;}

Page 13: 1 Data Structures CSCI 132, Spring 2014 Lecture 22 Searching

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).

Page 14: 1 Data Structures CSCI 132, Spring 2014 Lecture 22 Searching

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<Record>{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);};

Page 15: 1 Data Structures CSCI 132, Spring 2014 Lecture 22 Searching

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<Record> :: insert(position, data);}