Data Representation Methods

Slides:



Advertisements
Similar presentations
Pointers.
Advertisements

Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Lecture 6 Hashing. Motivating Example Want to store a list whose elements are integers between 1 and 5 Will define an array of size 5, and if the list.
Arrays and ArrayLists Ananda Gunawardena. Introduction Array is a useful and powerful aggregate data structure presence in modern programming languages.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Odds and Ends Strings (from Chapter 9) StringTokenizer.
Lecture 6 Hashing. Motivating Example Want to store a list whose elements are integers between 1 and 5 Will define an array of size 5, and if the list.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Chapter 10 Strings, Searches, Sorts, and Modifications Midterm Review By Ben Razon AP Computer Science Period 3.
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
SNU IDB Lab. Ch5. Linear Lists – Array Representation © copyright 2006 SNU IDB Lab.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Data Structure Specification  Language independent  Abstract Data Type  C++  Abstract Class.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Elementary Data Structures Array Lists Array Lists Dale.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
EGR 2261 Unit 11 Pointers and Dynamic Variables
Review Array Array Elements Accessing array elements
Principles of Computer Science I
Arrays.
Hash table CSC317 We have elements with key and satellite data
Array, Strings and Vectors
Introduction to Computer Science / Procedural – 67130
The Class ArrayLinearList
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Chapter 12: Data Structures
ARRAYLIST AND VECTOR.
List Representation - Array
Data Structures 1 1.
HW-6 Deadline Extended to April 27th
Welcome to CSE 143!.
Stacks, Queues, and Deques
Data Representation Methods
More Data Structures (Part 1)
TCSS 143, Autumn 2004 Lecture Notes
Data Representation Methods
بردارها و ماتريس ها ساختمان داده ها
Stacks, Queues, and Deques
Can store many of the same kind of data together
Linked List Intro CSCE 121 J. Michael Moore.
ArrayLists.
Words exercise Write code to read a file and display its words in reverse order. A solution that uses an array: String[] allWords = new String[1000]; int.
int [] scores = new int [10];
Chapter 10 ArrayList reading: 10.1
Welcome to CSE 143!.
Can store many of the same kind of data together
Arrays and Collections
Arrays.
int [] scores = new int [10];
Can store many of the same kind of data together
Introduction to Data Structure
Java Programming Language
Data Structures & Algorithms
Using a Queue Chapter 8 introduces the queue data type.
Using a Queue Chapter 8 introduces the queue data type.
Data Representation Methods
Data Representation Methods
Stacks, Queues, and Deques
Linked List Intro CSCE 121.
Dynamic Array: Implementation of Stack
CSE 373 Set implementation; intro to hashing
SPL – PS2 C++ Memory Handling.
Lecture-Hashing.
Presentation transcript:

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.

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

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

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

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

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.

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

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.

Length of Array element[] Don’t know how many elements will be in list. Must pick an initial length and dynamically increase as needed.

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];

Increasing Array Length Now copy elements from old array to new one. a b c d e f a b c d e f

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

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;

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

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.

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

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

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

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.

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.