Lecture No 01 Data Structures Data Structures Prepares

  • Slides: 48
Download presentation
Lecture No. 01 Data Structures

Lecture No. 01 Data Structures

Data Structures § Prepares the students for (and is a prerequisite for) the more

Data Structures § Prepares the students for (and is a prerequisite for) the more advanced material students will encounter in later courses. § Cover well-known data structures such as dynamic arrays, linked lists, stacks, queues, tree and graphs. § Implement data structures in C++

Data Structures § Prepares the students for (and is a prerequisite for) the more

Data Structures § Prepares the students for (and is a prerequisite for) the more advanced material students will encounter in later courses. § Cover well-known data structures such as dynamic arrays, linked lists, stacks, queues, tree and graphs. § Implement data structures in C++

Data Structures § Prepares the students for (and is a prerequisite for) the more

Data Structures § Prepares the students for (and is a prerequisite for) the more advanced material students will encounter in later courses. § Cover well-known data structures such as dynamic arrays, linked lists, stacks, queues, tree and graphs. § Implement data structures in C++

Grading § § Term Exam 1 20% Term Exam 2 20% Final 35% 8

Grading § § Term Exam 1 20% Term Exam 2 20% Final 35% 8 Programming Assignments 25% § Schedule of Topics: http: //www. vu. edu. pk/ds

Need for Data Structures § Data structures organize data more efficient programs. § More

Need for Data Structures § Data structures organize data more efficient programs. § More powerful computers more complex applications. § More complex applications demand more calculations.

Organizing Data § Any organization for a collection of records that can be searched,

Organizing Data § Any organization for a collection of records that can be searched, processed in any order, or modified. § The choice of data structure and algorithm can make the difference between a program running in a few seconds or many days.

Efficiency § A solution is said to be efficient if it solves the problem

Efficiency § A solution is said to be efficient if it solves the problem within its resource constraints. – Space – Time § The cost of a solution is the amount of resources that the solution consumes.

Selecting a Data Structure Select a data structure as follows: 1. Analyze the problem

Selecting a Data Structure Select a data structure as follows: 1. Analyze the problem to determine the resource constraints a solution must meet. 2. Determine the basic operations that must be supported. Quantify the resource constraints for each operation. 3. Select the data structure that best meets these requirements.

Some Questions to Ask • Are all data inserted into the data structure at

Some Questions to Ask • Are all data inserted into the data structure at the beginning, or are insertions interspersed with other operations? • Can data be deleted? • Are all data processed in some well-defined order, or is random access allowed?

Data Structure Philosophy § Each data structure has costs and benefits. § Rarely is

Data Structure Philosophy § Each data structure has costs and benefits. § Rarely is one data structure better than another in all situations. § A data structure requires: – space for each data item it stores, – time to perform each basic operation, – programming effort.

Goals of this Course 1. Reinforce the concept that costs and benefits exist for

Goals of this Course 1. Reinforce the concept that costs and benefits exist for every data structure. 2. Learn the commonly used data structures. – These form a programmer's basic data structure “toolkit. ” 3. Understand how to measure the cost of a data structure or program. – These techniques also allow you to judge the merits of new data structures that you or others might invent.

Arrays § Elementary data structure that exists as built-in in most programming languages. main(

Arrays § Elementary data structure that exists as built-in in most programming languages. main( int argc, char** argv ) { int x[6]; int j; for(j=0; j < 6; j++) x[j] = 2*j; }

Arrays § § § Array declaration: int x[6]; An array is collection of cells

Arrays § § § Array declaration: int x[6]; An array is collection of cells of the same type. The collection has the name ‘x’. The cells are numbered with consecutive integers. To access a cell, use the array name and an index: x[0], x[1], x[2], x[3], x[4], x[5]

Array Layout x[0] Array cells are contiguous in computer memory The memory can be

Array Layout x[0] Array cells are contiguous in computer memory The memory can be thought of as an array x[1] x[2] x[3] x[4] x[5]

What is Array Name? § ‘x’ is an array name but there is no

What is Array Name? § ‘x’ is an array name but there is no variable x. ‘x’ is not an lvalue. § For example, if we have the code int a, b; then we can write b = 2; a = b; a = 5; But we cannot write 2 = a;

Array Name § ‘x’ is not an lvalue int x[6]; int n; x[0] =

Array Name § ‘x’ is not an lvalue int x[6]; int n; x[0] = 5; x[1] = 2; x = 3; x = a + b; x = &n; // not allowed

Dynamic Arrays § § § You would like to use an array data structure

Dynamic Arrays § § § You would like to use an array data structure but you do not know the size of the array at compile time. You find out when the program executes that you need an integer array of size n=20. Allocate an array using the new operator: int* y = new int[20]; // or int* y = new int[n] y[0] = 10; y[1] = 15; // use is the same

Dynamic Arrays § § § ‘y’ is a lvalue; it is a pointer that

Dynamic Arrays § § § ‘y’ is a lvalue; it is a pointer that holds the address of 20 consecutive cells in memory. It can be assigned a value. The new operator returns as address that is stored in y. We can write: y = &x[0]; y = x; // x can appear on the right // y gets the address of the // first cell of the x array

Dynamic Arrays § We must free the memory we got using the new operator

Dynamic Arrays § We must free the memory we got using the new operator once we are done with the y array. delete[ ] y; § We would not do this to the x array because we did not use new to create it.

Traversing Array 1. 2. 3. 4. Set K=LB Repeat 3 and 4 while K≤UB

Traversing Array 1. 2. 3. 4. Set K=LB Repeat 3 and 4 while K≤UB Apply process to A[K] Set K=K+1 End of Loop 5. Exit AL 21

Insertion & Deletion in Array INSERTION(A, N, K, ITEM) 1. Set j=N 2. Repeat

Insertion & Deletion in Array INSERTION(A, N, K, ITEM) 1. Set j=N 2. Repeat 3 and 4 while j≥K 3. Set A[J+1]=A[j] 4. Set j=j-1 End of Loop 5. Set A[K]=ITEM 6. Set N=N+1 7. Exit

Insertion & Deletion in Array DELETE(A, N, K, ITEM) 1. Set ITEM = A[K]

Insertion & Deletion in Array DELETE(A, N, K, ITEM) 1. Set ITEM = A[K] 2. Repeat for j=K to N-1 Set A[j]=A[j+1] End Loop 3. Set N=N-1 4. Exit AL 23

Searching: The operation to find the location of particular data item in list; There

Searching: The operation to find the location of particular data item in list; There are two methods to find the location of data item in the array 1) Linear Search 2) Binary Search AL 24

Searching Linear Search: The simplest and direct approach, scan the entire list, one record

Searching Linear Search: The simplest and direct approach, scan the entire list, one record at a time, for a given value. If found return the location otherwise the search is not successful. AL 25

Searching LINEAR (A, N, LOC) 1. Set A[N+1]=ITEM 2. Set LOC=1 3. Repeat while

Searching LINEAR (A, N, LOC) 1. Set A[N+1]=ITEM 2. Set LOC=1 3. Repeat while A[LOC]≠ITEM Set LOC=LOC+1 End Loop 4. If LOC=N+1 Search is not Successful else Return LOC. AL 26

Searching BINARY(A, LB, UB, ITEM LOC) 1. Set BEG=LB, END =UB , MID= (LB+UB)/2

Searching BINARY(A, LB, UB, ITEM LOC) 1. Set BEG=LB, END =UB , MID= (LB+UB)/2 2. Repeat 3 and 4 while BEG≤END and A[MID]≠ITEM 3. If ITEM<A[MID] Set END=MID-1 Else Set BEG=MID+1 4. Set MID=INT(BEG+END)/2 5. If A[MID]=ITEM LOC=MID Else LOC=NULL

Sorting : Arrangement of data in some specific order i. e Ascending or Descending,

Sorting : Arrangement of data in some specific order i. e Ascending or Descending, Alphabetically. How to sort the integers in this array? 20 8 5 10 7 5 8 10 20 7

Sorting • • AL Elementary Sorting Algorithms: Selection Sort Insertion Sort Bubble Sort 29

Sorting • • AL Elementary Sorting Algorithms: Selection Sort Insertion Sort Bubble Sort 29

Selection Sort • Main idea: – find the smallest element – put it in

Selection Sort • Main idea: – find the smallest element – put it in the first position – find the next smallest element – put it in the second position –… • And so on, until you get to the end of the list AL 30

Selection Sort -- Example 55 7 12 a 19 0 1 2 3 :

Selection Sort -- Example 55 7 12 a 19 0 1 2 3 : 7 12 a 50 19 1 2 3 : 12 a 50 71 19 2 3 : AL 19 a 50 71 12 2 3 : 19 a 50 71 12 2 3 31

Selection Sort Min(A, K, N , LOC) 1. Set Min=A[K] and LOC=K 2. Repeat

Selection Sort Min(A, K, N , LOC) 1. Set Min=A[K] and LOC=K 2. Repeat for j= K+1 to N if Min>A[j] then Set Min=A[j] and LOC=j End of loop 3. Return SELECTION SORT(A, N) 1. Repeat Step 2 and 3 for K=1 to N-1 2. Call Min(A, K, N, LOC) 3. Interchange A[K] and A[LOC] Set TEMP=A[K], A[K]=A[LOC] and A[LOC]=TEMP End step 1 loop 4. Exit 32

Insertion Sort • Basic idea (sorting cards): – Starts by considering the first two

Insertion Sort • Basic idea (sorting cards): – Starts by considering the first two elements of the array data, if out of order, swap them – Consider the third element, insert it into the proper position among the first three elements. – Consider the forth element, insert it into the proper position among the first four elements. –…… AL 33

Insertion Sort -- Example a: 19 12 5 0 2 3 a: 12 19

Insertion Sort -- Example a: 19 12 5 0 2 3 a: 12 19 5 7 0 1 7 1 2 3 a: 5 12 19 7 0 a: 5 AL 0 1 2 3 7 12 19 1 2 3 34

Insertion Sort INSERTION(A, N) 1. Set A[0]= -∞ 2. Repeat 3 to 5 for

Insertion Sort INSERTION(A, N) 1. Set A[0]= -∞ 2. Repeat 3 to 5 for K=2…. N 3. Set TEMP=A[K] and PTR=K-1 4. Repeat while TEMP<A[PTR] a) Set A[PTR+1]=A[PTR] b) Set PTR=PTR-1 End of Loop 5. Set A[PTR+1]=TEMP End of Loop 6. Return AL 35

Bubble Sort • Basic idea: – Exchange neighboring items until the largest item reaches

Bubble Sort • Basic idea: – Exchange neighboring items until the largest item reaches the end of the array – Repeat for the rest of the array AL 36

Bubble Sort -- Example a: 19 5 12 7 0 1 2 3 a:

Bubble Sort -- Example a: 19 5 12 7 0 1 2 3 a: 5 19 12 7 0 1 2 3 a: 5 12 19 7 0 1 2 3 a: 5 12 7 19 0 AL 1 2 3 a: 5 12 7 19 0 a: 5 0 a: 5 1 2 3 7 12 19 37 0 1 2 3

Bubble Sort BUBBLE(A, N) 1. Repeat 2 and 3 for K=1 to N-1 2.

Bubble Sort BUBBLE(A, N) 1. Repeat 2 and 3 for K=1 to N-1 2. Set PTR=1 3. Repeat while PTR≤N-K a) if A[PTR]>A[PTR+1] Interchange A[PTR] and A[PTR+1] b) Set PTR=PTR+1 End of Inner Loop End of Outer Loop 4. Exit AL 38

The LIST Data Structure § The List is among the most generic of data

The LIST Data Structure § The List is among the most generic of data structures. § Real life: a. b. c. d. shopping list, groceries list, list of people to invite to dinner List of presents to get

Lists § A list is collection of items that are all of the same

Lists § A list is collection of items that are all of the same type (grocery items, integers, names) § The items, or elements of the list, are stored in some particular order § It is possible to insert new elements into various positions in the list and remove any element of the list

Lists § A list is collection of items that are all of the same

Lists § A list is collection of items that are all of the same type (grocery items, integers, names) § The items, or elements of the list, are stored in some particular order § It is possible to insert new elements into various positions in the list and remove any element of the list

Lists § A list is collection of items that are all of the same

Lists § A list is collection of items that are all of the same type (grocery items, integers, names) § The items, or elements of the list, are stored in some particular order § It is possible to insert new elements into various positions in the list and remove any element of the list

Lists § List is a set of elements in a linear order. For example,

Lists § List is a set of elements in a linear order. For example, data values a 1, a 2, a 3, a 4 can be arranged in a list: (a 3, a 1, a 2, a 4) In this list, a 3, is the first element, a 1 is the second element, and so on § The order is important here; this is not just a random collection of elements, it is an ordered collection

Lists § List is a set of elements in a linear order. For example,

Lists § List is a set of elements in a linear order. For example, data values a 1, a 2, a 3, a 4 can be arranged in a list: (a 3, a 1, a 2, a 4) In this list, a 3, is the first element, a 1 is the second element, and so on § The order is important here; this is not just a random collection of elements, it is an ordered collection

List Operations Useful operations • create. List(): create a new list (presumably empty) •

List Operations Useful operations • create. List(): create a new list (presumably empty) • copy(): set one list to be a copy of another • clear(); clear a list (remove all elments) • insert(X, ? ): Insert element X at a particular position in the list • remove(? ): Remove element at some position in the list • get(? ): Get element at a given position • update(X, ? ): replace the element at a given position with X • find(X): determine if the element X is in the list • length(): return the length of the list.

List Operations § We need to decide what is meant by “particular position”; we

List Operations § We need to decide what is meant by “particular position”; we have used “? ” for this. § There are two possibilities: 1. Use the actual index of element: insert after element 3, get element number 6. This approach is taken by arrays 2. Use a “current” marker or pointer to refer to a particular position in the list.

List Operations § We need to decide what is meant by “particular position”; we

List Operations § We need to decide what is meant by “particular position”; we have used “? ” for this. § There are two possibilities: 1. Use the actual index of element: insert after element 3, get element number 6. This approach is taken by arrays 2. Use a “current” marker or pointer to refer to a particular position in the list.

List Operations § If we use the “current” marker, the following four methods would

List Operations § If we use the “current” marker, the following four methods would be useful: § § start(): moves to “current” pointer to the very first element. tail(): moves to “current” pointer to the very last element. next(): move the current position forward one element back(): move the current position backward one element