Chapter 4 ADT Sorted List Sorted Type Class

  • Slides: 43
Download presentation
Chapter 4 ADT Sorted List

Chapter 4 ADT Sorted List

Sorted Type Class Interface Diagram Sorted. Type class Make. Empty Is. Full Get. Length

Sorted Type Class Interface Diagram Sorted. Type class Make. Empty Is. Full Get. Length Get. Item Put. Item Delete. Item Reset. List Get. Next. Item Private data: length info [0] [1] [2] [MAX_ITEMS-1] current. Pos

Member functions Which member function specifications and implementations must change to ensure that any

Member functions Which member function specifications and implementations must change to ensure that any instance of the Sorted List ADT remains sorted at all times? • Put. Item • Delete. Item

Insert. Item algorithm for Sorted. List ADT • Find proper location for the new

Insert. Item algorithm for Sorted. List ADT • Find proper location for the new element in the sorted list. • Create space for the new element by moving down all the list elements that will follow it. • Put the new element in the list. • Increment length.

Implementing Sorted. Type member function Put. Item // IMPLEMENTATION FILE #include “itemtype. h” (sorted.

Implementing Sorted. Type member function Put. Item // IMPLEMENTATION FILE #include “itemtype. h” (sorted. cpp) // also must appear in client code void Sorted. Type : : Put. Item ( Item. Type item ) // Pre: List has been initialized. List is not full. // item is not in list. // List is sorted by key member using function Compared. To. // Post: item is in the list. List is still sorted. {. . . }

void Sorted. Type : : Put. Item ( Item. Type item ) { bool

void Sorted. Type : : Put. Item ( Item. Type item ) { bool more. To. Search; int location = 0; // find proper location for new element more. To. Search = ( location < length ); while ( more. To. Search ) { switch ( item. Compared. To( info[location] ) ) { case LESS : more. To. Search = false; break; case GREATER : location++; more. To. Search = ( location < length ); break; } } // make room for new element in sorted list for ( int index = length ; index > location ; index-- ) info [ index ] = info [ index - 1 ]; info [ location ] = item; length++; }

Delete. Item algorithm for Sorted. List ADT • Find the location of the element

Delete. Item algorithm for Sorted. List ADT • Find the location of the element to be deleted from the sorted list. • Eliminate space occupied by the item by moving up all the list elements that follow it. • Decrement length.

Implementing Sorted. Type member function Delete. Item // IMPLEMENTATION FILE continued (sorted. cpp) void

Implementing Sorted. Type member function Delete. Item // IMPLEMENTATION FILE continued (sorted. cpp) void Sorted. Type : : Delete. Item ( Item. Type item ) // Pre: List has been initialized. // Key member of item is initialized. // Exactly one element in list has a key matching item’s key. // List is sorted by key member using function Compared. To. // Post: No item in list has key matching item’s key. // List is still sorted. {. . . }

void Sorted. Type : : Delete. Item ( Item. Type item ) { int

void Sorted. Type : : Delete. Item ( Item. Type item ) { int location = 0; // find location of element to be deleted while ( item. Compared. To ( info[location] ) location++; != EQUAL ) // move up elements that follow deleted item in sorted list for ( int index = location + 1 ; index < info [ index - 1 ] = info [ index ]; length--; } length; index++ )

Improving member function Get. Item Recall that with the Unsorted List ADT we examined

Improving member function Get. Item Recall that with the Unsorted List ADT we examined each list element beginning with info[ 0 ], until we either found a matching key, or we had examined all the elements in the Unsorted List. How can the searching algorithm be improved for Sorted List ADT?

Retrieving Eliot from a Sorted List length info 4 [0] [1] Asad Bradley [2]

Retrieving Eliot from a Sorted List length info 4 [0] [1] Asad Bradley [2] Hsing [3] Maxwell [MAX_ITEMS-1] . . . The sequential search for Eliot can stop when Hsing has been examined. Why?

Binary Seach in a Sorted List • Examines the element in the middle of

Binary Seach in a Sorted List • Examines the element in the middle of the array. Is it the sought item? If so, stop searching. Is the middle element too small? Then start looking in second half of array. Is the middle element too large? Then begin looking in first half of the array. • Repeat the process in the half of the list that should be examined next. • Stop when item is found, or when there is nowhere else to look and item has not been found.

Item. Type Sorted. Type: : Get. Item ( Item. Type item, bool& found )

Item. Type Sorted. Type: : Get. Item ( Item. Type item, bool& found ) // Pre: Key member of item is initialized. // Post: If found, item’s key matches an element’s key in the list // and a copy of that element is returned; otherwise, // original item is returned. { int mid. Poin; int first = 0; int last = length - 1; bool more. To. Search = ( first <= last ); found = false; while ( more. To. Search && !found ) { mid. Point = ( first + last ) / 2 ; // INDEX OF MIDDLE ELEMENT switch ( item. Compared. To( info [ mid. Point ] ) ) { case LESS : . . . // LOOK IN FIRST HALF NEXT case GREATER : . . . // LOOK IN SECOND HALF NEXT case EQUAL : . . . // ITEM HAS BEEN FOUND } } }

Trace of Binary Search item = 45 15 info[0] 26 [1] 38 [2] 57

Trace of Binary Search item = 45 15 info[0] 26 [1] 38 [2] 57 [3] first 62 [4] 78 [5] 84 [6] info[0] first [7] 108 [8] mid. Point 26 [1] 119 [9] last LESS 15 91 last = mid. Point - 1 38 [2] mid. Point GREATER 57 [3] 62 [4] 78 [5] 84 [6] 91 [7] 108 [8] last first = mid. Point + 1 119 [9]

Trace continued item = 45 15 info[0] 26 [1] 38 [2] 57 [3] 62

Trace continued item = 45 15 info[0] 26 [1] 38 [2] 57 [3] 62 [4] 78 [5] 84 [6] 91 [7] 108 [8] 119 [9] first, last mid. Point GREATER 15 info[0] 26 [1] 38 [2] first = mid. Point + 1 57 [3] 62 [4] 78 [5] 84 [6] 91 [7] 108 [8] first, mid. Point, last LESS last = mid. Point - 1 119 [9]

Trace concludes item = 45 15 info[0] 26 [1] 38 57 [2] [3] last

Trace concludes item = 45 15 info[0] 26 [1] 38 57 [2] [3] last first > last 62 [4] 78 [5] 84 [6] 91 [7] found = false 108 [8] 119 [9]

Item. Type Sorted. Type: : Get. Item ( Item. Type item, bool& // ASSUMES

Item. Type Sorted. Type: : Get. Item ( Item. Type item, bool& // ASSUMES info ARRAY SORTED IN ASCENDING ORDER { int mid. Point; int first = 0; int last = length - 1; bool more. To. Search = ( first <= last ); found = false; found ) while ( more. To. Search && !found ) { mid. Point = ( first + last ) / 2 ; switch ( item. Compared. To( info [ mid. Point ] ) ) { case LESS : last = mid. Point - 1; more. To. Search = ( first <= last ); break; case GREATER : first = mid. Point + 1; more. To. Search = ( first <= last ); break; case EQUAL : found = true ; item = info[ mid. Point ]; break; } } return item; }

Allocation of memory STATIC ALLOCATION Static allocation is the allocation of memory space at

Allocation of memory STATIC ALLOCATION Static allocation is the allocation of memory space at compile time. DYNAMIC ALLOCATION Dynamic allocation is the allocation of memory space at run time by using operator new.

3 Kinds of Program Data • STATIC DATA: memory allocation exists throughout execution of

3 Kinds of Program Data • STATIC DATA: memory allocation exists throughout execution of program. static long Seed. Value; • AUTOMATIC DATA: automatically created at function entry, resides in activation frame of the function, and is destroyed when returning from function. • DYNAMIC DATA: explicitly allocated and deallocated during program execution by C++ instructions written by programmer using unary operators new and delete

Arrays created at run time If memory is available in an area called the

Arrays created at run time If memory is available in an area called the free store (or heap), operator new allocates memory for the object or array and returns the address of (pointer to) the memory allocated. Otherwise, the NULL pointer 0 is returned. The dynamically allocated object exists until the delete operator destroys it.

Dynamic Array Allocation char *ptr; // ptr is a pointer variable that // can

Dynamic Array Allocation char *ptr; // ptr is a pointer variable that // can hold the address of a char ptr = new char[ 5 ]; // dynamically, during run time, allocates // memory for 5 characters and places into contents of ptr their beginning address 6000 ptr // the

Dynamic Array Allocation char *ptr ; ptr = new char[ 5 ]; strcpy( ptr,

Dynamic Array Allocation char *ptr ; ptr = new char[ 5 ]; strcpy( ptr, “Bye” ); ptr[ 1 ] = ‘u’; // a pointer can be subscripted std: : cout << ptr[ 2] ; 6000 ptr ‘B’ ‘u’ ‘y’ ‘e’ ‘’

class Sorted. Type<char> Sorted. Type Make. Empty ~Sorted. Type Retrieve. Item Insert. Item Delete.

class Sorted. Type<char> Sorted. Type Make. Empty ~Sorted. Type Retrieve. Item Insert. Item Delete. Item. . . Get. Next. Item Private data: length 3 ‘C’ list. Data current. Pos ? ‘L’ ‘X’

Insert. Item algorithm for Sorted Linked List • Find proper position for the new

Insert. Item algorithm for Sorted Linked List • Find proper position for the new element in the sorted list using two pointers pred. Loc and location, where pred. Loc trails behind location. • Create a new node and place item in it. • Insert the node by adjusting pointers of pred. Loc and location. • Increment length.

Inchworm Effect: moving two pointers

Inchworm Effect: moving two pointers

Inserting ‘S’ into a Sorted List pred. Loc location Private data: length 3 ‘C’

Inserting ‘S’ into a Sorted List pred. Loc location Private data: length 3 ‘C’ list. Data current. Pos more. To. Search ? ‘L’ ‘X’

Finding proper position for ‘S’ pred. Loc NULL location Private data: length 3 ‘C’

Finding proper position for ‘S’ pred. Loc NULL location Private data: length 3 ‘C’ list. Data current. Pos more. To. Search ‘L’ ‘X’ ? true as location->info < ’s’, move forward: pred. Loc = location; //pred. Loc catches up location = location->next; //location move one step forward

Finding proper position for ‘S’ pred. Loc location ‘C’ ‘L’ Private data: length 3

Finding proper position for ‘S’ pred. Loc location ‘C’ ‘L’ Private data: length 3 list. Data current. Pos more. To. Search ‘X’ ? true location->info is still less than ’s’ move ahead…

Finding Proper Position for ‘S’ pred. Loc location Private data: length 3 ‘C’ list.

Finding Proper Position for ‘S’ pred. Loc location Private data: length 3 ‘C’ list. Data current. Pos ‘L’ ‘X’ ? location->info is still larger than ’s’ stop! more. To. Search false ’s’ should be inserted before ‘x’!

Inserting ‘S’ into Proper Position pred. Loc location Private data: length 4 list. Data

Inserting ‘S’ into Proper Position pred. Loc location Private data: length 4 list. Data ‘C’ ‘L’ ‘X’ current. Pos ‘S’ more. To. Search false

How do the Sorted. List implementations compare?

How do the Sorted. List implementations compare?

Why is a destructor needed? When a local list variable goes out of scope,

Why is a destructor needed? When a local list variable goes out of scope, the memory space for data member list. Ptr is deallocated. But the nodes to which list. Ptr points are not deallocated. A class destructor is used to deallocate the dynamic memory pointed to by the data member.

Implementing the Destructor Unsorted. Type: : ~Unsorted. Type() // Post: List is empty; all

Implementing the Destructor Unsorted. Type: : ~Unsorted. Type() // Post: List is empty; all items have // been deallocated. { Node. Type* temp. Ptr; while (list. Data != NULL) { temp. Ptr = list. Data; list. Data = list. Data->next; delete temp. Ptr; } }

Object-Oriented Design Methodology • Four stages to the decomposition process • Brainstorming • Filtering

Object-Oriented Design Methodology • Four stages to the decomposition process • Brainstorming • Filtering • Scenarios • Responsibility algorithms

Brainstorming • A group problem-solving technique that involves the spontaneous contribution of ideas from

Brainstorming • A group problem-solving technique that involves the spontaneous contribution of ideas from all members of the group • All ideas are potential good ideas • Think fast and furiously first, and ponder later • A little humor can be a powerful force • Brainstorming is designed to produce a list of candidate classes

Filtering • Determine which are the core classes in the problem solution • There

Filtering • Determine which are the core classes in the problem solution • There may be two classes in the list that have many common attributes and behaviors • There may be classes that really don’t belong in the problem solution

Scenarios • Simulate class interactions • Ask “What if? ” questions • Assign responsibilities

Scenarios • Simulate class interactions • Ask “What if? ” questions • Assign responsibilities to each class • There are two types of responsibilities • What a class must know about itself (knowledge) • What a class must be able to do (behavior)

Responsibility Algorithms • The algorithms must be written for the responsibilities • Knowledge responsibilities

Responsibility Algorithms • The algorithms must be written for the responsibilities • Knowledge responsibilities usually just return the contents of one of an object’s variables • Action responsibilities are a little more complicated, often involving calculations

Computer Example • Let’s repeat the problem-solving process for creating an address list •

Computer Example • Let’s repeat the problem-solving process for creating an address list • Brainstorming and filtering • Circling the nouns and underlining the verbs

Computer Example • First pass at a list of classes

Computer Example • First pass at a list of classes

Computer Example • Filtered list

Computer Example • Filtered list

CRC Cards

CRC Cards

Responsibility Algorithms

Responsibility Algorithms