1 / 38

Fall 2010

Fall 2010. Chapter 4. ADT Sorted List. 1. Goals. Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations using an array-based implementation Create and destroy a list Determine whether the list is full Insert an element

cmansour
Télécharger la présentation

Fall 2010

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Fall 2010 Chapter 4 ADT Sorted List 1

  2. Goals • Describe the Abstract Data Type Sorted List from three perspectives • Implement the following Sorted List operations using an array-basedimplementation • Create and destroy a list • Determine whether the list is full • Insert an element • Retrieve an element • Delete an element

  3. Goals • Implement the list operations outlined above using a linked implementation • Implement the binary search algorithm • Compare the two implementations of the ADT Sorted List in terms of Big-O approximations • Compare the implementations of the Unsorted List ADT and the Sorted List ADT in terms of Big-O Analysis • Distinguish between bounded and unbounded ADTs at the logical and implementation levels

  4. ADT Sorted List Remember the difference between an unsorted list and a sorted list? Remember the definition of a key? If the list is a sorted list of people, what would be the key? of bank accounts, what would be the key? of students, what would be the key?

  5. ADT Unsorted List Operations Logical Level • Transformers • MakeEmpty • InsertItem • DeleteItem • Observers • IsFull • GetLength • RetrieveItem • Iterators • ResetList • GetNextItem change state observe state process all

  6. ADT Sorted List • Which member function specifications and implementations must change to ensure that any instance of the Sorted List ADT remains sorted at all times? • InsertItem • DeleteItem What about the other transformer MakeEmtpy?

  7. Array Implementation What do you have to do to insert Clair into the following list? Anne Betty Mary Susan

  8. Array Implementation • 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

  9. Array Implementation • InsertItem Initialize location to position of first item Set moreToSearch to (have not examined Info(last)) while moreToSearch switch (item.ComparedTo(Info(location))) case LESS : Set moreToSearch to false case EQUAL : // Cannot happen case GREATER : Set location to Next(location) Set moreToSearch to (have not examined Info(last)) for index going from length DOWNTO location + 1 Set Info(index ) to Info(index-1) Set Info(location) to item Increment length Why can't EQUAL happen?

  10. Array Implementation • DeleteItem Initialize location to position of first item Set found to false while NOT found switch (item.ComparedTo(Info(location))) case GREATER : Set location to Next(location) case LESS : // Cannot happen case EQUAL : Set found to true for index going from location +1 TO length -1 Set Info(index - 1) to Info(index) Decrement length Why can't LESS happen?

  11. Array Implementation • Can we improve searching in a sorted list? • With the Unsorted List ADT we examined each list element beginning with info[ 0 ], until we found a matching key or we examined all the elements. Binary Search Algorithm

  12. Binary Search Algorithm • Examine the element in the middle of the array • Match item? • Stop searching • Middle element too small? • Search second half of array • Middle element too large? • Search first half of array • Repeat the process in half that should be examined next. • Stop when item is found or when there is no where else to look.

  13. Binary Search Algorithm • void SortedType::RetrieveItem ( ItemType& item, bool& found ) • // Pre: Key member of item is initialized. • // Post: If found, item’s key matches an element’s key and a copy • // of element has been stored in item; otherwise, item is • // unchanged. • { 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 : . . . // LOOK IN FIRST HALF NEXT • case GREATER : . . . // LOOK IN SECOND HALF NEXT • case EQUAL : . . . // ITEM HAS BEEN FOUND • } • } • }

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

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

  16. InsertItem Find O(1) O(N) search Put O(1) O(N) moving down Combined O(1) O(N) DeleteItem Find O(N) O(N) search Put O(1) swap O(N) moving up Combined O(N) O(N) Array-Based Big-O Comparison OPERATION UnsortedList SortedList RetrieveItem O(N) O(N) linear search O(log2N) binary search

  17. Linked Implementation • What is the linked implementation of searching? Can it be improved?

  18. Linked Implementation What about if you pass the spot where the item would be if in the list?

  19. Linked Implementation • Is Inserting as easy? Let's see Set location to listData Set moreToSearch to (location != NULL) while moreToSearch switch (item.ComparedTo(location->info)) case GREATER : Set location to location->next Set moreToSearch to (location != NULL) case LESS : Set moreToSearch to false See the problem ?

  20. Linked Implementation We need a trailing pointer

  21. ‘C’ ‘L’ ‘X’ moreToSearch Inserting ‘S’ into a Sorted List predLoc location Private data: length 3 listData currentPos ?

  22. ‘C’ ‘L’ ‘X’ moreToSearch true Finding proper position for ‘S’ predLoc location NULL Private data: length 3 listData currentPos ?

  23. ‘C’ ‘L’ ‘X’ moreToSearch true Finding proper position for ‘S’ predLoc location Private data: length 3 listData currentPos ?

  24. ‘C’ ‘L’ ‘X’ moreToSearch false Finding Proper Position for ‘S’ predLoc location Private data: length 3 listData currentPos ?

  25. ‘S’ moreToSearch false Inserting ‘S’ into Proper Position predLoc location Private data: length 4 listData currentPos ‘C’ ‘L’ ‘X’

  26. Bounded and Unbounded ADTs • Bounded ADT • An ADT for which there is a logical limit on the number of items in the structure • Unbounded ADT • An ADT for which there is no logical limit on the number of items in the structure How do you relate the logical limit to the physical limit?

  27. What is a Class Template? • A class template allows the compiler to generate multiple versions of a class type by using type parameters. • The formal parameter appears in the class template definition, and the actual parameter appears in the client code. Both are enclosed in pointed brackets, < >.

  28. Using class templates • The actual parameterto the templateis a data type. Any type can be used, either built-in or user-defined. • When creating class template • Put .h and .cpp in same file or • Have .h include .cpp file 28

  29. 6000 ‘H’ ‘e’ ‘l’ ‘l’ ‘o’ ‘\0’ msg [0] [1] [2] [3] [4] [5] [6] [7] Recall that . . . • char msg [ 8 ]; • msgis thebase addressof the array. We say msg is a pointer because its value is an address. It is a pointer constant because the value of msg itself cannot be changed by assignment. It “points” to the memory location of a char.

  30. x number ch Addresses in Memory • When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is the address of the variable. For example: • int x; • float number; • char ch; • 2000 2002 2006

  31. Obtaining Memory Addresses • The address of a non-array variable can be obtained by using the address-of operator &. • using namespace std; • int x; • float number; • char ch; • cout << “Address of x is “ << &x << endl; • cout << “Address of number is “ << &number << endl; • cout << “Address of ch is “ << &ch << endl;

  32. ADT Sorted List Operations • Transformers • MakeEmpty • InsertItem • DeleteItem • Observers • IsFull • LengthIs • RetrieveItem • Iterators • ResetList • GetNextItem change state observe state process all

  33. ‘C’ ‘L’ ‘X’ class SortedType<char> SortedType Private data: length 3 listData currentPos ? MakeEmpty ~SortedType RetrieveItem InsertItem DeleteItem . . . GetNextItem

  34. What is a Circular Linked List? • A circular linked list is a list in which every node has a successor; the “last” element is succeeded by the “first” element.

  35. External Pointer to the Last Node

  36. What is a Doubly Linked List? • A doubly linked list is a list in which each node is linked to both its successor and its predecessor.

  37. Linking the New Node into the List

  38. Deleting from a Doubly Linked List

More Related