Download presentation
1
Chapter 4 ADT Sorted List
2
Lecture 9
3
Object-Oriented Design Methodology
Four stages to the decomposition process Brainstorming Filtering Scenarios Responsibility algorithms 3
4
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 4
5
Brainstorming Step 1: review brainstorming principles at the beginning of the meeting remind everyone that this is a group activity and personal style should be put aside. Step 2: state specific session objectives such as: “Today we want to come up with a list of candidate classes for the sorted ADT or grocery store checkout system.” Step 3: use a round-robin technique to allow the group to proceed at an even tempo but give people enough time to think. Each person should contribute a possible object class to the list. A facilitator should keep the discussion on target, and a scribe should take notes. 5
6
Filtering Determine which are the core classes in the problem solution
Build CRC cards for final classes 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 6
7
Scenarios a sequence of steps that describes an interaction between a client/user and an application/program Simulate class interactions to Assign responsibilities to each class Ask “What if?” questions There are two types of responsibilities What a class must know about itself (knowledge) What a class must be able to do (behavior) 7
8
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 8
9
Computer Example Let’s repeat the problem-solving process for creating an address list Brainstorming and filtering Circling the nouns and underlining the verbs Create an address list that includes each person’s name, address, phone number and address. This list should then be printed in alphabetical order. The names to be included in the list are on scraps of paper and business cards. 9
10
Computer Example First pass at a list of classes 10
11
Computer Example Filtered list 11
12
CRC Cards 12
13
Responsibility Algorithms
13
14
Object-oriented vs. Top Down
Object-oriented design focuses on the data objects that are to be transformed resulting in a hierarchy of objects nouns are the primary focus nouns in objects; verbs become operations Top-down design focus on the process of transforming the input into the output, resulting in a hierarchy of tasks verbs are the primary focus 14
15
Sorted Type Class Interface Diagram
MakeEmpty Private data: length info [ 0 ] [ 1 ] [ 2 ] [MAX_ITEMS-1] currentPos IsFull GetLength GetItem PutItem DeleteItem ResetList GetNextItem 15
16
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? PutItem DeleteItem 16
17
InsertItem algorithm for SortedList 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. 17
18
Implementing SortedType member function PutItem
// IMPLEMENTATION FILE (sorted.cpp) #include “itemtype.h” // also must appear in client code void SortedType :: PutItem ( ItemType item ) // Pre: List has been initialized. List is not full. // item is not in list. // List is sorted by key member using function ComparedTo. // Post: item is in the list. List is still sorted. { . } 18
19
void SortedType :: PutItem ( ItemType item )
{ bool moreToSearch; int location = 0; // find proper location for new element moreToSearch = ( location < length ); while ( moreToSearch ) { switch ( item.ComparedTo( info[location] ) ) { case LESS : moreToSearch = false; break; case GREATER : location++; } } // make room for new element in sorted list for ( int index = length ; index > location ; index-- ) info [ index ] = info [ index - 1 ]; info [ location ] = item; length++; 19
20
DeleteItem algorithm for SortedList 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. 20
21
Implementing SortedType member function DeleteItem
// IMPLEMENTATION FILE continued (sorted.cpp) void SortedType :: DeleteItem ( ItemType 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 ComparedTo. // Post: No item in list has key matching item’s key. // List is still sorted. { . } 21
22
void SortedType :: DeleteItem ( ItemType item )
{ int location = 0; // find location of element to be deleted while ( item.ComparedTo ( info[location] ) != EQUAL ) location++; // move up elements that follow deleted item in sorted list for ( int index = location + 1 ; index < length; index++ ) info [ index - 1 ] = info [ index ]; length--; } 22
23
Lecture 10
24
Improving member function GetItem
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? 24
25
Retrieving Eliot from a Sorted List
length info [ 0 ] Asad [ 1 ] Bradley [ 2 ] Henry [ 3 ] Maxwell . [MAX_ITEMS-1] The sequential search for Eliot can stop when Henry has been examined. Why? 25
26
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. 26
27
27 ItemType SortedType::GetItem ( ItemType 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 midPoint; int first = 0; int last = length - 1; bool moreToSearch = ( first <= last ); found = false; while ( moreToSearch && !found ) { midPoint = ( first + last ) / 2 ; // INDEX OF MIDDLE ELEMENT switch ( item.ComparedTo( info [ midPoint ] ) ) { case LESS : // LOOK IN FIRST HALF NEXT case GREATER : // LOOK IN SECOND HALF NEXT case EQUAL : // ITEM HAS BEEN FOUND } 27
28
Trace of Binary Search item = 45 info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] first midPoint last LESS last = midPoint - 1 info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] first midPoint last GREATER first = midPoint + 1 28
29
Trace continued item = 45 info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] first, last midPoint GREATER first = midPoint + 1 info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] first, midPoint, last LESS last = midPoint - 1 29
30
Trace concludes item = 45 info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] last first first > last found = false 30
31
31 ItemType SortedType::GetItem ( ItemType item, bool& found )
// ASSUMES info ARRAY SORTED IN ASCENDING ORDER { int midPoint; int first = 0; int last = length - 1; bool moreToSearch = ( first <= last ); found = false; while ( moreToSearch && !found ) { midPoint = ( first + last ) / 2 ; switch ( item.ComparedTo( info [ midPoint ] ) ) { case LESS : last = midPoint - 1; moreToSearch = ( first <= last ); break; case GREATER : first = midPoint + 1; case EQUAL : found = true ; item = info[ midPoint ]; } }return item; 31
32
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. 32
33
3 Kinds of Program Data STATIC DATA: memory allocation exists throughout execution of program. static long SeedValue; 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 33
34
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. 34
35
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 // the contents of ptr their beginning address 6000 6000 ptr 35
36
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 ‘u’ 6000 ‘B’ ‘y’ ‘e’ ‘\0’ ptr 36
37
class SortedType<char>
Private data: length listData currentPos ? MakeEmpty ~SortedType ‘C’ ‘L’ ‘X’ RetrieveItem InsertItem DeleteItem . GetNextItem 37 37
38
InsertItem algorithm for Sorted Linked List
Find proper position for the new element in the sorted list using two pointers predLoc and location, where predLoc trails behind location. Obtain a node for insertion and place item in it. Insert the node by adjusting pointers. Increment length. 38
39
The Inchworm Effect 39
40
Inserting ‘S’ into a Sorted List
predLoc location Private data: length listData currentPos ? ‘C’ ‘L’ ‘X’ moreToSearch 40
41
Finding proper position for ‘S’
predLoc location NULL Private data: length listData currentPos ? ‘C’ ‘L’ ‘X’ moreToSearch true 41
42
Finding proper position for ‘S’
predLoc location Private data: length listData currentPos ? ‘C’ ‘L’ ‘X’ moreToSearch true 42
43
Finding Proper Position for ‘S’
predLoc location Private data: length listData currentPos ? ‘C’ ‘L’ ‘X’ moreToSearch false 43
44
Inserting ‘S’ into Proper Position
predLoc location Private data: length listData currentPos ‘C’ ‘L’ ‘X’ ‘S’ moreToSearch false 44
45
Why is a destructor needed?
When a local list variable goes out of scope, the memory space for data member listPtr is deallocated. But the nodes to which listPtr points are not deallocated. A class destructor is used to deallocate the dynamic memory pointed to by the data member. 45 45
46
Implementing the Destructor
SortedType::~SortedType() // Post: List is empty; all items have // been deallocated. { NodeType* tempPtr; while (listData != NULL) tempPtr = listData; listData = listData->next; delete tempPtr; } 46
47
How do the SortedList implementations compare?
47
48
SortedList implementations comparison
48
49
List implementations comparison
50
Overview
51
Overview In the object-oriented design of an airline passenger reservation program, suppose that "airplane" has been identified as one object and "airplane seat" has been identified as another object. Focusing specifically on the airplane object, how does it relate to the airplane seat object? A. a has-a relationship B. an is-a relationship C. an independent and equal relationship D. a and b above E. none of the above Now, suppose that "airplane seat," "aisle seat," and "window seat" have been identified as objects. Focusing specifically on the aisle seat object, how does it relate to the airplane seat object?
52
Overview design method’s focus on
In which stage of the object-oriented methodology, the initial list of classes developed the list of classes refined "what if" questions asked the algorithms developed design method’s focus on the process of transforming the input into the output, resulting in a hierarchy of tasks the data objects that are to be transformed, resulting in a hierarchy of objects brainstorming filtering scenarios responsibility algorithms Top-down Object-oriented
53
Overview Inserting into an unsorted list and inserting into a sorted list are the same time complexity. In a sorted list, there is a semantic relationship between successive items in the list. Deleting from a sorted array based list requires that the elements below the one being deleted be moved up one slot. Searching an unsorted list and a sorted list are always the same time complexity. You can perform a binary search on both a linked and an array-based implementation of a sorted list. To dynamically allocate an array-based list there should be a parameterized constructor.
54
Overview the order of the operation that determines if an item is in
a list in a sorted, array-based implementation a list in an unsorted, array-based implementation a list in a sorted, linked implementation a list in an unsorted, linked implementation O(log N) O(N) O(N) O(N)
55
Overview location < length moreToSearch location < length false
// Pre: List contains valid data and List is sorted by key using function // ComparedTo. // There is at most one list item with the same key as item; there may be none. // Post: No list element has the same key as item. List is still sorted. void SortedType::DeleteItem(ItemType item) { int location = 0; int index; bool moreToSearch; moreToSearch = ____________________; bool found = false; while ( !found && _______________) switch (item.ComparedTo(info[location])) { case LESS : location++; break; case GREATER : moreToSearch = _____________________; case EQUAL : ________ = true; } if (found) { for (index = location + 1; index < length; index++) info[index - 1] = ______________; length--; location < length moreToSearch location < length false found info[index]
56
Overview EQUAL index < length length--
// Pre: List contains valid data, sorted by key values. // One and only one list item with the same key as item's is in the list. // Post: No list element has the same key as item; the list is still sorted. void SortedType::DeleteItem(ItemType item) { int location = 0; while (item.ComparedTo(info[location]) != ___________) location++; for (int index = location + 1; ________________; index++) info[index - 1] = info[index]; ___________; } EQUAL index < length length--
57
Overview length - 1 first <= last midPoint - 1 midPoint + 1
ItemType SortedType::GetItem(ItemType& item, bool& found) { // Uses binary search algorithm int midPoint; int first =______________; int last = ______________; bool moreToSearch = _______________; found = false; while (moreToSearch && !found) { midPoint = (first + last) / 2; switch (item.ComparedTo(info[midPoint])) { case LESS : last = _______________; moreToSearch = first <= last; break; case GREATER : first = _______________; moreToSearch = first <= last; case EQUAL : found = true; item =_________________; } return item; length - 1 first <= last midPoint - 1 midPoint + 1 info[midPoint]
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.