Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
1 ADTs, Arrays, Linked Lists Outline and Required Reading: • ADTs (§ 2.1) • Using Arrays (§ 3.1) • Linked Lists (§ 3.2, 3.3, 3.4) CSE 2011, Winter 2017 Instructor: N. Vlajic Data Type A data type is a classification of data which tells the compiler/interpreter how the programmer intends to use the data. A data type also defines: 1) the operations that can be done on the data, 2) the meaning of the data, 3) the way values of that type can be stored. Most programming languages support various basic/primitive types of data, e.g.: 2 Abstract Data Type (ADT), textbook, pp. 62 3 “abstract” ⇒ implementation details (which DS and how the interface methods have been implemented) are not specified !!! Abstract Data Type – a known programming model for storing and manipulating more complex data types that consists of: 1) data structure (DS), which stores a set of data of the same type 2) set of operation/functions supported on the the given DS, also known as interface • name, input / output parameters, and observable behaviour of each operation need to be specified ADT interface data structure add() remove() find() request result Abstract Data Type (ADT) (cont.) Example [ car as an ADT ] 4 Abstract Data Type (ADT) (cont.) Example [ data structure vs. ADT ] 5 Abstract Data Type (ADT) (cont.) Example [ an ADT ] If we find this ADT in a language/library we know exactly what to expect in terms of its operation but not necessarily the details of its implementation. 6 Abstract Data Type (ADT) (cont.) Data Structures Arrays (static!), Linked Lists (dynamic!) Standard ADTs Stacks, Queues, Vectors, Lists, Trees, … What shell we learn about standard ADTs in this course? 7 (1) what operations they support (2) complexity of supported operations (3) some implementation approaches (4) storage requirements Why should we know standard ADTs!? • standard ADTs are great reusable components - readily available ADTs significantly facilitate user’s application development (think of Java’s Vector, Hashtable, etc. classes) • we may be required to adapt algorithms which use some of the standard ADTs Abstract Data Type (ADT) (cont.) 8 ADTs – Designer’s vs. User’s View Data Structure Interface request add() remove() find() result ADT designer’s concerns: • choice of data structure • implementation of operations ADT-user’s concerns: • correct performance • efficient performance ADT’s implementation details should be hidden and inaccessible to the user! • hidden – to spare the user from knowing all design details before using the ADT • inaccessible – to prevent erroneous / malicious user’s manipulation of the ADT 9 ADT Taxonomy Linear ADTs - we call an ADT “linear”, if for the respective underlying data structure following is true: (1) (2) (3) (4) 0 1 2 3 there is a unique first element there is a unique last element every element has a unique predecessor (except 1st) every element has a unique successor (except last) 4 5 6 A1 7 A2 Non-linear ADTs - if one or more of the above is not true, the ADT is non-linear A1 A2 A3 A1 10 Arrays Arrays, textbook pp. 34 A common programming task is to keep track of a group of related objects! One possible approach is to allocate a block of memory for this group, and place its elements in the contiguous locations of this block. 11 12 Arrays (cont.) Array – sequence of indexed components with the following properties: • array elements are placed contiguously in memory ⇒ address of any element can be calculated directly as its offset from the beginning of the array • consequently, array components can be efficiently inspected or updated in O(1) time, using their indices randomNumber = numbers[5]; numbers[2] = 100; • array size is fixed at the time of array’s construction int[] numbers = new numbers [10]; Index = 0 1 2 3 4 5 Array of length 8 6 7 Element at position 5 Arrays (cont.) 13 Arrays in Java – Properties (1) For an array of length n, the index bounds are 0 to (n-1). (2) Java arrays are homogeneous - all array components must be of the same (object or primitive) type. • but, an array of an object type can contain objects of any respective subtype (3) An array is itself an object. • it is allocated dynamically by means of “new” (4) When an array is first created, all values are initialized with • 0 – for an array of int[] or double[] type • false – for a boolean[] array • null – for an array of objects Example [ common error – uninitialized arrays ] int[] numbers; numbers[2] = 100; 14 Arrays (cont.) Arrays in Java – Properties (cont.) (5) The length of any array (object) can be accessed through its instance variable ‘length’. • the cells of an array A are numbered: 0, 1, .., (A.length-1) !!! (6) ArrayIndexOutOfBoundsException - thrown at an attempt to index into array A using a number larger than (A.length-1). Example [ declaring, defining and determining the size of an array ] int[] A={12, 24, 37, 53, 67} for (int i=0; i <= A.length; i++) { … } What is wrong with this code?! Array is defined at the time of its ‘declaration’. 15 Arrays (cont.) Arrays in Java – Properties (cont.) (7) Since an array is an object, the name of the array is actually a reference (pointer) to the place in memory where the array is stored. • reference to an object holds the address of the actual object Example 3 [ arrays as objects ] stack int[] A={12, 24, 37, 53, 67}; A int[] B=A; B B[3]=5; A heap 12 24 37 53 67 12 24 37 5 67 B Example 4 [ cloning an array ] int[] A={12, 24, 37, 53, 67}; int[] B=A.clone(); B[3]=5; A 12 24 37 53 67 B 12 24 37 53 67 A 12 24 37 53 67 B 12 24 37 5 67 Arrays (cont.) 16 Arrays in Java: a few useful methods (java.util.Arrays) equals(A, B) – returns true if A and B have an equal number of elements and every corresponding pair of elements in the two arrays are equal fill(A, x) – store element x into every cell of array A sort(A) – sort the array A in the natural ordering of its elements binarySearch(int[] A, int key) – search the specified array of ints for the specified value using the binary search algorithm Arrays (cont.) Example [ What is printed out ?! ] … int[] A={12, 24, 37, 53, 67}; int[] B=A.clone(); if (A==B) System.out.println(“ Superman ”); … if (A.equals(B)) System.out.println(“ Snow White ”); Checks whether the two reference variables refer to the same place in memory. Compares 2 array objects, and as long as the actual elements in the arrays are equal, both objects are considered equal. 17 Arrays (cont.) 18 Example [ 2D array in Java = array of arrays !!! ] int[][] nums = new int[5][4]; int[][] nums; nums = new int[5][]; for (int i=0; i<5; i++) { nums[i] = new int[4]; } Example [ 2D array of objects in Java = an array of an array of references !!! ] Square[][] board = new Square[2][3]; http://www.willamette.edu/~gorr/classes/cs231/lectures/notes.htm 19 Arrays (cont.) Arrays in General – Major Limitations! (1) static data structure – size must be fixed at the time the program creates the array – once set, array size cannot be changed • if: number of entered items > declared array size ⇒ out of memory • fix 1: use array size > number of expected items ⇒ waste of memory • fix 2: increase array size to fit the number of items ⇒ waste of time Example 5 [ time complexity of “growing” an array ] Number of elements to be copied. if (numberOfItems > numbers.length) { int[] newNumbers = new int[2*numbers.length]; System.arraycopy(numbers, 0, newNumbers, 0, numbers.length); numbers = newNumbers; Starting position Starting position in } in source array. destination array. What is this for?! (2) insertion / deletion in an array is time consuming – all the elements following the inserted element must be shifted appropriately [ will talk about this later ] 20 Linked Lists Linked List A common programming task is to keep track of a group of related objects! Another possible approach is to store elements of this group in separate/distant memory cells, but then link these cells together using pointers. 21 22 Linked List (cont.) Linked List – linear collection of nodes with the following properties: • each node is an object containing: 1) a single element - stored object or value 2) links - reference to one or both neighbouring nodes • each node (except the last one) has a successor, and each node (except the first one) has a predecessor • direct reference only to the first / last node is provided!!! node ∅ A1 … A2 head node element An ∅ tail node link Types of Linked Lists: • • Singly Linked List (each node contains reference only to the next node) Doubly Linked List (… contains reference to both previous & next node) 23 Linked List (cont.) NOTE: Linked list nodes normally are not stored contiguously in memory. Rather, they are logically contiguous. ∅ A1 A2 An ∅ Linked Lists – Major Advantages (1) dynamic data structure – its length can vary – nodes are added when needed or released when no longer needed (2) fast insertion / deletion – only 2 references need to be modified • all existing node objects remain at their current location in memory Linked Lists – Major Disadvantages (1) time consuming access to elements – an element can be accessed only by traversing the list from the front / back 24 What we need to know …, textbook pp. 688 Computer Memory – numbered sequence 0000 of bytes 01101010 byte b; 0001 01010110 Address – number associated with a byte When we declare a Primitive Variable, space in memory is automatically allocated. a byte requires 1 byte of memory 0002 10101110 0003 01111100 0004 01001110 0005 11101000 a char requires 2 bytes of memory char c; 0006 01101011 an int requires 4 bytes of memory 0007 01101110 int i; 0008 01111000 Object – collection of values – different 0009 01010010 objects occupy different amounts of memory 000A 11111111 When we declare an Object Variable, JVM does not know how much space the object will need. 000B 01101110 • • • • • • What we need to know … (cont.) JVM use of memory The stack and heap are parts of memory set aside for use by the runtime system. 25 What we need to know … (cont.) Object Variable – container that holds an address • when we declare an object variable, Java allocates space for a reference (i.e. pointer) to an object the pointer is initially set to null (0000) • new operator allocates space for the actual object, initializes the object, and returns its address • if we attempt to use an object variable before initializing it, NullPointerException will be thrown in Stack: Integer intRef; intRef intRef = new Integer(5); intRef in Heap: null 5 Integer Object 26 27 What we need to know … (cont.) Declaring Object Variables Allocating an Object Integer p, q; p q p = new Integer(5); p 5 marked for garbage collection Allocating Another Object p = new Integer(6); p 5 6 Assigning a Variable q = p; p 6 q 28 Object Variable (cont.) Allocating an Object q = new Integer(9); p 6 q 9 marked for garbage collection Assigning null to an Object Variable p = null; p 6 q 9 Assigning a Variable with a null Value q = p; p null q 9 marked for garbage collection 29 Nodes in Singly Linked List Singly Linked List – requires both of the following to be defined ∅ 1) SLLNode class 2) SLL class A1 A2 … head node SLLNode An ∅ tail node public class SLLNode { Object element; SLLNode next; Object references! public SLLNode(Object elem, SLLNode succ) { this.element = elem; this.next = succ; } } Nodes in Singly Linked List (cont.) Example [ singly liked list in memory ] 30 Nodes in Singly Linked List (cont.) 31 SLLNode Complying with Requirements hidden and inaccessible public class SLLNode { private Object element; private SLLNode next; public SLLNode(Object elem, SLLNode succ) { this.element = elem; this.next = succ; } accessor methods hide implementation details from user mutator methods carefully inspect users attempts to modify variables’ values public Object getElement() { return element; } public SLLNode getNext() { return next; } protected void setElement(Object newElement) { element = newElement; } protected void setNext(SLLNode newNext) { next = newNext; } } 32 Implementation of Singly Linked List SLL ∅ A1 A2 … An-1 An head ∅ tail public class SLL { private SLLNode head; private SLLNode tail; public SLL() { this.head = null; this.tail = null; } … Constructs an empty singly linked list! Elements/nodes added later. Why this?! } It is a good practice to maintain direct references to the head and tail of a SLL. 1) easy to delete or insert new node at the front of SLL; 2) easy to insert new node at the rear. It is still costly to delete the end node. Why?! What is the cost in big-O?