Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Representation Methods

Similar presentations


Presentation on theme: "Data Representation Methods"— Presentation transcript:

1 Data Representation Methods
array --- Chapter 5 linked --- Chapter 6 Each representation method is illustrated using the Linear List data structure as an example.

2 Linear List Array Representation
use a one-dimensional array element[] 1 2 3 4 5 6 a b c d e All array representations of a linear list use an array (say one-dimensional). Different array representations result when different mappings are used between list elements and array positions. The most intuitive and simple mapping is to map list element I into array position I. L = (a, b, c, d, e) Store element i of list in element[i].

3 Right To Left Mapping a b c d e
A different mapping of linear list elements into a one-dimensional array.

4 Mapping That Skips Every Other Position
b c d e Yet another mapping.

5 Wrap Around Mapping a b c d e
And if that isn’t enough, here is yet another mapping.

6 Representation Used In Text
1 2 3 4 5 6 a b c d e listSize = 5 put element i of list in element[i] use a variable listSize to record current number of elements This is the representation used in the text. When this mapping is used, list operations such as isEmpty(), size(), get(index) Become easy to implement.

7 Insert/Erase An Element
b c d e listSize = 5 insert(1,g) To insert an element we must physically relocate elements that are to be to the right of the newly inserted element. We must also increment listSize by 1. The slide shows insert(1,g).erase(1) is the reverse. To remove an element we must move elements to the right of the removed element left by 1 and reduce listSize by 1. a g b c d e listSize = 6

8 Data Type Of Array element[]
Data type of list elements is unknown. Define element[] to be of template type T. Use template type T To use this representation in C++, we must know/provide a data type and length of the array element[]. Use template type T to get a generic class that works for all data types.

9 Length of Array element[]
Don’t know how many elements will be in list. Must pick an initial length and dynamically increase as needed. Use variable arrayLength to store current length of array element[].

10 Increasing Array Length
Length of array element[] is 6. a b c d e f First create a new and larger array T* newArray = new T[15];

11 Increasing Array Length
Now copy elements from old array to new one. a b c d e f a b c d e f Copy() is an STL function. It copies element[0:5] to newArray[0:5]. #include <algorithm> copy(element, element + 6, newArray);

12 Increasing Array Length
Finally, delete old array and rename new array. delete [] element; element = newArray; arrayLength = 15; a b c d e f element[0]

13 template<class T>
void changeLength1D(T*& a, int oldLength, int newLength) { 1 if (newLength < 0) throw illegalParameterValue(); 2 T* temp = new T[newLength]; // new array 3 int number = min(oldLength, newLength); // number to copy 4 copy(a, a + number, temp); 5 delete [] a; // deallocate old memory 6 a = temp; }

14 How Big Should The New Array Be?
At least 1 more than current array length. Cost of increasing array length when array is full is Q(old length). Cost of n insert operations done on an initially empty linear list increases by Q(n2). When array length is increased by 1 each time we need to resize the array element[], the cost of n add operations goes up by Theta(n2). To see this suppose we start with an empty list and element.length = 1. Each element, other than the first, that is added to the list requires an array resize to be done. When element i, i > 0 is added, the array resizing requires i steps. So the total array resizing cost is Theta(n2). If the add operations are all done at the right end, then the cost of all n adds (in the absence of array resizing) is Theta(n). The resizing time dominates the overall effort. When the additions are done at the front of the list the resizing time is of the same order as the add time.

15 Space Complexity element[6] newArray = new char[7];
b c d e f newArray = new char[7]; space needed = 2 * newLength – 1 = 2 * maxListSize – 1

16 Array Doubling Double the array length. newArray = new char[12];
f newArray = new char[12]; a b c d e f Time for n inserts goes up by Q(n). Space needed = 1.5*newLength. (e.g. 1.5 * 12) Space needed <= 3*maxListSize – 3

17 How Big Should The New Array Be?
Resizing by any constant factor new length = c * old length increases the cost of n inserts by Q(n). Here, by the cost of n inserts, we mean the cost of a sequence of n insert operations performed on an initially empty linear list. Resizing by an additive constant increases the cost of n add operations by Q(n2).

18 How Big Should The New Array Be?
Resizing by any constant factor new length = c * old length requires at most (1+c) * (maxListSize -1) space. Resizing by an additive constant c requires at most (maxListSize – 1) + (maxListSize – 1 + c) = 2 * (maxListSize – 1) + c space.

19 What Does C++ Do? STL class vector … c = 1.5 arrayList of text … c = 2
The STL provides a class vector that uses an array to represent a linear list. This uses max of 1 and 1.5 * currentLength. The vector class has much of the functionality of linear list.The text class arrayList does array doubling.

20 To-do Practice: More on insertion sort:
More on insertion sort:


Download ppt "Data Representation Methods"

Similar presentations


Ads by Google