Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Fall 2010 1 Chapter 4 ADT Sorted List. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List.

Similar presentations


Presentation on theme: "1 Fall 2010 1 Chapter 4 ADT Sorted List. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List."— Presentation transcript:

1 1 Fall 2010 1 Chapter 4 ADT Sorted List

2 2 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 –Retrieve an element –Delete an element

3 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 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 5 ADT Unsorted List Operations Transformers –MakeEmpty –InsertItem –DeleteItem Observers –IsFull –GetLength –RetrieveItem Iterators –ResetList –GetNextItem change state observe state process all Logical Level

6 6 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 ADT Sorted List What about the other transformer MakeEmtpy?

7 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 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 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 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 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 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; intlast = 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 } Binary Search Algorithm

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

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

16 Array-Based Big-O Comparison OPERATION UnsortedList SortedList RetrieveItem O(N) O(N) linear search O(log 2 N) binary search 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)

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

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

19 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 20 Linked Implementation We need a trailing pointer

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

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

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

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

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

26 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 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 parameter to the template is 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 29 Recall that... char msg [ 8 ]; msg is the base address of 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. msg [0] [1] [2] [3] [4] [5] [6] [7] ‘H’ ‘e’ ‘l’ ‘l’ ‘o’ ‘\0’ 6000

30 30 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 x number ch

31 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 33 class SortedType MakeEmpty ~SortedType DeleteItem. InsertItem SortedType RetrieveItem GetNextItem ‘C’ ‘L’ ‘X’ Private data: length 3 listData currentPos ?

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

35 35 External Pointer to the Last Node

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

37 37 Linking the New Node into the List

38 38 Deleting from a Doubly Linked List


Download ppt "1 Fall 2010 1 Chapter 4 ADT Sorted List. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List."

Similar presentations


Ads by Google