ds unit 1.ppt

Embed Size (px)

DESCRIPTION

uhit 2

Citation preview

  • BooksTenenbaum et al, Data Structures and Program Design in C, Prentice Hall Lipschtez S(Schaums Outline), Theory and Problems of Data Structures, TMH

  • What is Data Structure?A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently. Data Structure is a group of memory locations used to represent the information used by the algorithm.It is a way to store and organize data in the structured manner. Computer's memory is divided into small parts, we use different data structures to store data in those small blocks. Example: Arrays, Linked list, Trees ,etc.Why Do we need themComputers take on more and more complex tasks.Software implementation and maintenance is difficult.

  • Types of Data StructuresLinear Data Structures: Linear data structure is linear if element is adjacent to each other. It has exactly two neighbors elements to which it is connected as its previous and next member. Eg: Array, Linked list, Stack, Queue.Non Linear Data Structures: Non-Linear data structure is that if one element can be connected to more than two adjacent element then it is known as non-linear data structure.Eg: Tree, Graph.

  • Arrays Array is basic data structure in which we store data in continues memory locations.Array is variable which holds multiple elements ofsame type.

    Generic form of arrays:data type array_name[size];

    Data type: what type of data(int, float, char...) Size: number elements you want to store.

    Every element will have index value

    If you want to access any element we must use index value of the element.

  • StackStackNew nodes can be added and removed only at the topSimilar to a pile of dishesLast-in, first-out (LIFO)Eg: A stack of booksWhen a person wear bangles the last bangle worn is the first one to be removed and the first bangle would be the last to be removed.

  • QueueQueueSimilar to a supermarket checkout lineFirst-in, first-out (FIFO).Nodes are removed only from the head.Nodes are inserted only at the tail.The bullet in a machine gun..(you cannot fire 2 bullets at the same time).

  • Linked ListsIt is a list or chain of items where each item points to the next one in the list. Each item in a linked list is called a node.Each node contains the data and also the location to the next item. START.

  • TreesThe data structure which reflects a hierarchical relationship between various elements is called rooted tree/tree.

  • GraphA graph G = (V,E) is composed of:V: set of verticesE: set of edges connecting the vertices in VAn edge e = (u,v) is a pair of verticesExample: V= {a,b,c,d,e}

    E= {(a,b),(a,c),(a,d), (b,e),(c,d),(c,e), (d,e)}

    abdec

  • Static and Dynamic Data StructuresStatic:A static data structure is a data structure created for an input data set which is not supposed to change within the scope of the problem. When a single element is to be added or deleted, the update of a static data structure incurs significant costs, often comparable with the construction of the data structure from scratch. In real applications, dynamic data structures are used, which allow for efficient updates when data elements are inserted or deleted.Have fixed maximum size.Memory is reserved at the time of compilation.Eg: Array

  • Dynamic:Have flexible size Memory allocation for the data structure takes place at the run time, only required amount of memory is allocated.Eg: Linked List

  • Address Calculation in Linear ArraysA linear array is a list of a finite number n of homogeneous data elements.(a) The elements of the array are referenced respectively by an index set consisting of n consecutive numbers.(b) The elements of the array are referenced respectively by an index set consisting of n consecutive numbers.n- length/ size if index set consists of 1,2,3n

  • Length= UB-LB+1UB= largest indexLB=lowest indexLet LA be liner arrayK- indexLOC(LA[k])- Address of the element LA[k] of the array LA.LOC(LA[K])= Base(LA)+w(K-LB)

  • EgConsider the array AUTO which records the number of automobiles sold each year from 1932 through 1984. given Base(AUTO)=200, w=4. Find the address of the array element for the year 1965.LOC(AUTO[1965]=Base(AUTO)+w(1965-LB) =200+4(1965-1932)=332

  • Two Dimensional ArrayColumn major order- subscript (1,1)(2,1)(3,1)- 1 columnRow major order-(1,1)(1,2)(1,3)- 1 rowm*n arrayLOC(A[j,k]=Base(A)+w[M(k-1)+(j-1)]- column majorLOC(A[j,k]=Base(A)+w[N(j-1)+(k-1)]- row major

  • General Multidimensional ArraysLi- length of ith dimensionLi=UB-LB+1Ei=Ki-LBEffective IndexColoum majorBase(c)+w((EnLn-1+En-1)Ln-2)++E3)L2+E2)L1+E1)LOC(C[K1,k2Kn])=base(C)+w((E1L2+E2)L3+E3)L4++En-1)Ln+En)n is dimension

  • EgSuppose a 3d array MAZE is declared using MAZE(2:8, -4:1, 6: 10), base(MAZE)=200, w=4. find loc of MAZE[5,-1,8]L1=8-2+1=7, L2=1-(-4)+1=6, L3=10-6+1=5Total elements in maze= li.l2.l3=7.6.5=210E1=5-2=3, E2=-1-(-4)=3, E3=8-6=2Row majorE1L2=3.6=18E1L2+E2=18+3=21(E1L2+E2)L3=21.5=105

  • (E1L2+E2)L3+E3=105+2=107LOC(MAZE[5,-1,8])=200+4(107)=200+428=628

  • Sparse MatricesMatrices with a relatively high portion of zero entries are called sparse matrices.If all entries above the main diagonal are zero is known as lower triangular matrices.If non zero entries can only occur on the diagonal or on elements immediately above or below the diagonal, is called a tridiagonal matrix. B[1]=a11, B[2]=a21, B[3]=a224 5 63-5 2 4 81 96 9 3 6

  • Traversing Linear Array( Printing/counting elements)

    1. [initialize counter] Set K:=LB2.Repeat Step 3 and 4 while K

  • Inserting into a linear array (N no of elements not the size of array )INSERT(LA,N,K,ITEM)1. [Initialize counter] Set J:=N2. Repeat Steps 3 and 4 while J>=K 3. [Move Jth element downward] Set LA[J+1]:=LA[J] 4. [Decrease Counter] Set J=J-1[End of step 2 loop]5. [Insert element] Set LA[K]:= ITEM6. [Reset N] Set N=N+17. Exit

  • Deleting from a Linear ArrayDELETE(LA,N,K,ITEM)1. Set ITEM:=LA[K]2. Repeat for J=K to N-1 [Move J+Ist element upward] Set LA[J]=LA[J+1] [End of Loop]3. [Reset the number N of elements in LA] Set N=N-1exit