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 simulated pointer --- Chapter 7 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). Element.length = 15. 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 size = 5 put element i of list in element[i] use a variable size 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 Add/Remove An Element size = 5 add(1,g) size = 6 a b c d e a g b c d e
To add an element we must physically relocate elements that are to be to the right of the newly inserted element. We must also increment size by 1. The slide shows add(1,g). remove(1) is the reverse. To remove an element we must move elements to the right of the removed element left by 1 and reduce size by 1. a g b c d e size = 6

8 Data Type Of Array element[]
Data type of list elements is unknown. Define element[] to be of data type Object. Cannot put elements of primitive data types (int, float, double, char, etc.) into our linear lists. To use this representation in Java, we must know/provide a data type and length of the array element[]. Wrapper types Integer, Float, Double, etc provided in Java.

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.

10 Increasing Array Length
Length of array element[] is 6. a b c d e f First create a new and larger array newArray = new Object[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

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

13 Altogether Now // create a new array of proper length and data type
Object [] newArray = new Object [newLength]; // copy all elements from old array into new one System.arraycopy(element, 0, newArray, 0, element.length); arraycopy(sourceArray, sourceStart, destArray, destStart, numberOfElementsToCopy) When writing a general method to change the length of an array whose data type is any subclass of Object, we need to use the more complex new array creation statement used on page 160 of the text. // rename array element = newArray;

14 public static Object [] changeLength(Object [] a, int newLength) {
Object [] newArray = new Object [newLength]; System.arrayCopy(…); return newArray; } Type cast back to Integer [] doesn’t work. So must use the more complex way described in book to create a new larger array. Integer [] a = new Integer [10]; …. a = (Integer []) changeLength(a, 100); // erroneous

15 How Big Should The New Array Be?
At least 1 more than current array length. Cost of increasing array length is Theta(new length) Cost of n add operations done on an initially empty linear list increases by Theta(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.

16 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

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

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

19 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.

20 What Does Java Do? java.util.Vector … array doubling
java.util.ArrayList … c = 1.5 dataStructures.ArrayLinearList of text … c = 2 Java provides 2 classes that use an array to represent a linear list—Vector and ArrayList. ArrayLinearList of text does array doubling.


Download ppt "Data Representation Methods"

Similar presentations


Ads by Google