You are on page 1of 52
' 4, Introduction to Data Structure Introduction ... Need for Data Structures ...... Definitions .... eer Types of Data Structures ..... Algorithm Analysis...... Analysis of Algorithms.. j EIS Space Complexity 1-8 % Time Complexity 1-9 7. __Asfmptotic Notations (0,9)... . 2. Array as a Data Structure 1. Introduction ...... Oarons Array Representation... i 2. i 3. Applications of Arrays .. 4 4. Sorting.. ae i : 41° Bubble Sort 25. x 4.2 Insertion Sort 29 an | 43° Merge Sort 212 4.4 Quick Sort 2-16 4.5 Comparison of Sorting Methods 2-21” K€ Searching Methods 224 Linked List 1, Introduction to List... deat 2. Linked List Implementation .....c.00n0e 3 Types of Linked Lists . 4, Operations on a Linked List ~ Singly Linked Lis 6. Doubly Linked List. Circular List” { ne aaa Bi s7y 3 | ] Noa pa) &2- 7 4, Stack 1. introduction . Definition .. <3 Stack Representation a i 3.1 Static Implementation of Stack 3.2 Dynamic Implementation of Stack 4-7 Applications of Stack.. = Queue 1. Concept of Queue 2. Operations on a Queue. 3. Linear Queue ... 3.1 Static Implementation of a Linear Queue a 3.2 Dynamic Implementation of Linear Queue 5-7 Circular Queue . 4.1 Linked Representation of a Circular Queue 5-15 5. Priority Queue... 6. _ Difference between Stack and Queue x 6, Tree ‘ 7 4. Concept and Terminologies. 2. Applications of Trees. 3. Representation of Binary Trees 4, Tree Operations 4.1 Creating a Binary Search Tree 6-11 4.2 Tree Traversals 6-14 43° Search 6-22 44. Inserting into Binary Search Tree 6-24 45 Deletion from Binary Search Tree 6-25 4.7 Counting the Leaf Nodes of a Tree 6-29 48 Copying a Tree 6-29 4.9 Comparing Two Binary Search Trees’ 6.39 4.10 Mirroring a Given Tree 6-31 48 Counting the Total Nodes ina Tree 6.28 | ! Data Structure vie { 1/Graph A 2. Concept and Terminologies Graph Representation. 21° Adjacency Matrix 22 Adjacency List 23° Adjacency Multiist 7.8 Graph Traversals .... 3.1 Depth First Search 32 Breadth First Search Applications of Graphs. 4.1 AOVNetwork- Topological Sort 7-22 42 ‘AOE Network ~ Critical Path Method 7-24 7-13 7-18 BRK Unit a | tion to Data Structure ineae Introduc sme i r In pate ty 1. Introduction Computers are capable of performing complex operations but they are ‘cum’ machines. They have to be ‘told’ what to do by giving them instructions. A set of instructions is ¢ ‘program’. A program performs operations on input data and gives a meaningful output. The instructions must be written in «a language called a ‘programming language’. Each programming languege provides a se- of built in data types which allow data to be stored in-a specific format. The language also provides a set of operators to perform operations on this data. ‘New trends in computers have made it possible to solve more complex problems. Data storage and processing requirements are also becoming complex and large. However, the basic data types provided by programming languages are not enough. Thus, there is a need for additional “data structures”. Thesd data structures may be a combination or collection: of the basic data types witt their own properties and operations. \ Introduction to Data Structure wien ate Siuctue 42 Need for Data Structures To solve a data processing problem, four things have to be identified: 4 ‘The data elements that are concerned with the problem. ‘The operations that will be performed on these data elements. Methods of representing the data elements in memory to retain the logical relationship beeween them. ‘The programming language which suits the current requirements. Example: Calculate area of circle 1. 2 3 4, The data elements are radius, pi and area. ‘The operations performed on the above data elements are multiplication, assignment etc. ‘They can be stored in memory as bits/bytes and the user can access them in different forms. Example: integer, float etc. Languages like Pascal, C, C+, ete. can be used to solve the above problem. The first three points deal mainly with the data elements and their Tepresentation. Thus, a proper ‘ data organization is very important to achieve the solution. The Need for Data Structures are 1. 2 * 4. Modern day computing problems are complex and large. They need to handle data in large volumes and of complex structures. There is a need to store and process information of a variety of types: character, numeric, audio, video, graphics etc. Data hes to be stored such that the logical relationship between the data elements is retained. \_ Advantages of Data Structures are 1. Structured data makes it easier to access and manipulate the information as compared to raw or unstructured data. A variety of operations can be performed on structured data. Related data can be stored together and in the required format. Better algorithms can be used on organized data-which improves program efficiency. Data Structure 1e3 Introduction to Data Struciure Application Areas There are many areas where data structures can be applied in a variety of ways. These i 1. Compiler Design 2. Operating System 3 Database Management Systems 4. Statistical analysis 5. Numerical Analysis Graphics Artificial Intelligence Simulation ee aS Games (3. _ Definitions Data: Data is input given to a system, which the system processes. Thus, data (~~ unprocessed input given toa system, Data Output Data can be of two types: i Atomic Data: Atomic da‘a is a single, elementary piece oe of data, | which ‘cannot be further decomposed. For éxariple, the character ‘a’, number 10 etc. are atomic. where ¢ is a constant and is the fixed part. S, is the variable part which depends on the instance characteristics i.e. the program instance being run. oO TORCOM Examples (italia 1 float area(float r) i return 3.142 *r *r; } ‘The space needed is independent of the input and output characteristics. It uses only the value of r (assuming one word of storage). 2, int sum(int af), int n) { int r= 0, i; i for(i = 0; i ¢ nj ive) s-s + ali); x return 8; Ona uci $68 nvedutono Oat Situ Res The algorithm depends om «, which is the instance characteristic, The variables i, S and n are the fixed part e=3 S)= 1 (ie. the size of af]) ; } int rsum(int a(], int n) ( if (no) return 0; return rsum(a,n-1) + a[n]; } This is a recursive function characterized by n. The recursion depth depends upon n. The ‘eoursion stack space includes space for the formal parameters, local variables and veture address, Thus the recursion stack space is (n+1)#3 (i. one word for n, pointer to a and renurn address each). Time Complexity 7,2 total amount of dime shat this take For Successfull execudtio rm {The time complexity ofa program isthe amount of time the program requires to execute, However, {itis very difficult to caleulate the exact running time. However a rough estimate can he made. To de ‘this, we have to identify the active operations in the rogram (Which are central to the program and executed often). The other operations like assignments, manipulation of the loop variable ete. i + {called book keeping operations and will not execute as many times as the active operations, Thus, “Tehey need not be considered [The total number of active operations is defined as its frequency count. We can say that, the jOrecution time is proportional to the frequency count ofthe program. In some cases, the frequency jCount may be a polynomial. However, for the time complexity only the largest powe- needs to be | considered. The frequency count is written as a function of n which is usually the input size * “Afier calculating the frequency count, the time complexity is expressed using an asympcotic notation ‘. -0",2,0 etc. Example: If f(N) = N‘ + 20N? +N +40, then the term with the highest order of magnitude ie N* determines the time complexity of the function {(N), which is denoted as Ow’). itis based ol Frequency court of operation of prey there ave twotyped of opeactior NReak kee bing Oecohion 2) Active openabior wa Daté'Sfiicure - 11810 introduction to Data Stueture Examples 7 La = arb; ‘The statement a = a+b executes one time. Thus, its frequency count is 1. 2 for (ietsicensite) a = atb; ‘The statement a = a+b executes n times. Thus, its frequency count is n. 3. for (i=a;i¢emsis+) { for(j jen; j++) i ae ; oo a exeehtes en “Fe every value “of 7, j”vaties inl "time n eaney at 4. for(ist;icenjite) for (j=2;5¢eisj++) a = atb; The frequency count is 5. Hyunchon. iipieh Cee b he shove longa ig sear sop AULD, 09. ofS 3 ie WY, en \a pul dail Bost Casi, Average Gate and Worst C28 irgy; soy m san. be executed for the giver: The best, Case performance, niu parameters. The Worst case step ant is the m: given parameters, Waitt viaweu ot Houta? Ate Ta tot count is the aera? number of steps executed. ygitnty TEx, a, ‘rare the n probable inputs then the step counts are ee Best Case. T,=Min(T,, a oT,). ; 1 Average Case Ta = Sp possibfe mpats © Tn. -o%) Usually, the average case and worst-case step count is the same. Data Structure 1644 Introduction to Dat 7) Asymptotic Notations (0,2) The complexity of an algorithm can be expressed in several ways. 1, The algorithm never takes more than (some function »f n) operations. 2, The algorithm’s running time i always less th:a1 some function of n. The above can be expressed using Asymptotic notations O and Q. They are calle because they apply for large values of n. Before we study these notations, we w common functions, which are used as complexity functions (running time functions) i Constant 2. | Logarithmie 3. [Linear 4. 5. Linearlog Quadratic 6. | Cubic The growth rates of these functions are as illustrated in the table below. 4 0 1 4 4 2 2 1 2 4 e 4 4[ 2 8 16 | 64 16 e{_3 24 | 64 | 512 256 16] 4 64 | 256 | 4096 | 65536 on } 32] 5 160 [1024 | "32768_| 4204967256 | e men 64] 6 384 | 4096 | 262144 | Too large The above can be represented graphically as: win Dota Structure 1612 Introduction to Data Structure 1, © Notation (Big - Oh notation) This notation is used to denote upper bounds. It is expressed as O(f{n)) which means that the time taken never exceeds roughly f(n) operations. It is the most commonly used notation. Definition: The function f{n) = O(g(n)) {read as f of n is big oh of g of n] iff there exists positive constants ¢ and ng such that f(n) <=c # g(n) forall n,n>=no. i ( 2. Q Notation (Omega Notation) 5 This is used to denote lower bounds. ~ It is written as i Ty= O(f{n)) Which indicates that Ty takes at least about f(n) operations. Definition: The function f(a) = 2 (g(a) iff there exists positive constants ¢ and ny such that ; f(n) 2 ¢ » g(n) for all n,n > 19. { These can be depicted graphically as shown eg(n) f(a) f(n) f eg(n) ; : i a Po f(n) = O(g(n)) 0 f(n) = Q¢G(n)) (a) (b) Solved Examples 1. What is the time complexity of the following piece of code? int arrayadd(int a[], int n) q ae { int sum=0, i; \ a po for(i=0; i0) ‘ sum = sum+n; n=n/2; } I@ Which notation is used to denote lower bound? = Corn eFG nstefio? : 13. Arrange the following in ascending order of time complexities: O(n), O(log # F O(n og n), 0(2", : (Osa the components of space complexity. 2 wiion Data Structure 41616 Introduction to Data Structure B: Subjective Ll. Specify the need for data structures. + What are the advantages of structured data versus unstructured data? Explain the types of data structures, Write a short note on ADT. « Which are the criteria upon which an algorithm can be evaluated? \®) Write a note on Space Complexity. Write @ note on Time Complexity. UISION “phugical 45 \ogican ds Array as a Data Structure 1. Introduction The primitive data types in a language like integer, float, char etc. are capable of storing only a single value at a time. In many applications, we need to store many elements together as a group. For example, if we have to sort n numbers, the n numbers must be stored and be treated as a group and have to be compared to arrange them in the required order, Tais will be very difficult if the numbers are stored as independent elements. An array stores a set of elements as a singl group hence it is easy to access any element and perform operations on them. As defined earlier, data structures can be linear or non-linear. In a are stored in a sequence or a line. An array is a linear data organization i.e, elements are stored in an order or sequence. linear data structure, the elements structure which uses sequential Definition An array is a finite, ordered set of homogenous elements. * Finite: There are a specific number of elements in the arrey. Ordered: The elements are arranged so that there is a first elemeat, second, end so on. Homogeneous: All the elements are of the same type. 2et On vision Data Stucture 262 Array as @ Data Structure Mathematically, an array can be considered as a set of pairs ~ (Index, value). For each index, there is an associated value. This means that, there is a one-to-one mapping or correspondence between the set of indices and tke set of values. PArimitive Operations on an array Create (array, type, size): This operation creates a new array. This operation needs infcrmation like type of data elements and the number of data elements to be stored. ii, Store (array, index, value): Store a value at a particular position: This operation needs the name of the arrey, the position in the array and the value to be stored at that position. Retrieve (array, index): Retrieve a value at a particular position: This operation requires the array name and an index and it returns the corresponding value. 7 5 2e6+3)FL ® Array Representation Ore O°. S0% 00 +(e | ° ays can be physically represented in two ways | . . Row-major representation: In this representation, the elements are arranged row-wise ic. 7 Elements of row ( are stored first, elements of row | are stored next and so on 2. / Column-major representation: Elements are arranged column-wise ic. elements of , column 0 are stared first, elements of column | are stored next and so on. x “The following diagrams show the logical and physical view of a two-dimensional array. _, Logical View = Columns—> 0 1 2 a Rows wysical View: Row Major Representation ‘ t[2]s]a[s[e Te — ve Row 0. Raw 1916 Row 2 Row 3 Data Structure = 283 Amay as a Data Structure sical View: Column Major Representation (1[a]7 tol 2; s[s[n]slelolz ‘Column 0. Column 1—><— Column 2—> m= Address Calculation Sor an array int a{r]{c]; where r = number of rows and c = number of columns, the loc =lement afii}{j] in the array can be calculated as: Row Major Representation address of a{i] (j] = Base-address+ ixceelementsize + jrelementsize = Base-address + (isc+) + elementsize Example address of a{1}[2] = 1000 + (1#342) + sizeof (int) = 1000+10 = 1010 ii, — Column Major Representation address of a[i}[j] = Base-address+ j+r+elementsize + itelementsize = Base-address+ (jer + i)+ elementsize Example address of a[1][2] = 1000 + (2441) ssizeof (int) “= 1000+18 fi = 1018 We can generalize the above for an ‘n’ dimensional array stored in row-major order int alsi){s2] . . . (sn); To access element a {il} [i]... . [in]; we frst have to pass through il arrays of size s2+ then through i2 arrays of size s34s4. sn and so forth. Finally we have to pass ‘in’ cleme + address of afi1][i2].. [in] = baseaddress + i1*52*. . +..,+in *esize = base address + (i1*52*... sn + j2*53*,, sn +. + Sn * esize + j2 * j3 + st . + in] tesize vision ae YY) V #6 D ala Sure 284 prey ana Cate Strctre PAE Example: Given an integer array int a5] [4] [3] whose base address is element af2) (3) [1] Se address is 1000, calculate the address of Solution Given s1=5, s2=4, 83=3, il=2, i2=3, i3=1 address of a[2] [3] [1] = 1000 + (i1#s2¢53 + i203 + i3)*sizeof (int) 1000 + (2463 + 34341) « sizeof (int) = 1068 Aa Applications of Arrays Being such an important data structure, there are several applications which require the use of arrays, In fact, this is one of the most widely used data structures. It is hard to think of applications, which do no‘ need arrays since almost all applications handle large volumes of data. M. VO a ‘4 < Polynomial Representation Representation of Sparse Matrix Searching Sorting Static Implementation of data structures like Stack, Queue, Tree, Graph etc. In this chapter, we shall be considering Sorting as an important application of arrays. 4. Sorting key valu . eal Sorting is\the process of arranging or ordering elements in theJascending or descending ordedbf aki Definitions and Terminology 1 A record is a collection of fields. Example: Student record may contain fields like roll number, name, total, grade. A file is a collection of records r{0] . . . r[n-1] Record Key: One or more fields of a record may be designated as key-field ie K{i) is ‘associated with record r{i]. s. h tba 1 i | 54“ unsie sort «ec OM Example on Coxting Data Structure 268 Anay as a Data Structure a 4. Internal Sorting: Sorting is done on data which is stored in main memory 5. External Sorting: Sorting is done on data stored in auxiliary storage devices. 6. Stable Sorting Technique: A sorting method is said to be stable if the order of keys having same values in the unsorted set is retained in the sorted set. Its possible for two or more records in the file to have same key value. A sorting technique is said to be stable if there exist records i and j with keys k[i] = = k{jJ, then if record[i] precedes record] in the original file, record{i] precedes record{j] in the sorted file also. 7. In-place Sorting: A sorting algorithm is said to be in-place if it does not use a lot of additional memory for sorting. ease \) This is one of the simplest and m¢ ular sorting metho 1¢ basic idea jj clements sequentially several times (n-1 times) In each pass we compare successive pairs of elements (x{i] with x{i+1]) and interchange the two if they are not in the required order. né element is placed in its correct position in each pass. In the first pass, the largest element will sink to the bottom, second largest in the second pass and so on. Thus, a total of n-1 passes are required to sort ‘n’ keys. Example \ ree e { Sovtinr 2 37 1. 48 52 33, ywha is an eztetnet Sorting Ugorithm 5 25 25 25 «2525 > pigo. rat uses tape o7 Cs . 12 120«-12—«12 digk duving sost 12 442 c 373737 phat uses main Pass 1: 4a 48 bag pap 4848 1) algo sang Gott lS a er ees memory J Hania : Saeriega itis rea ie 57] ipiernal Sortie Z Pass 2: = 4) wors) Corse complexity oF Be Mecae2aaighi ta) at? 12 ia 2% 25 25 bubble 50 > 9(n2) a7 Lay 2 377 484g Lag ce 33 a) Average case comple rity 3 33 33 433 faa OF bUbhHIE 897! ~> OC pr) 57] (57) 57] (57) [7] ae a win ala Siucte 266 Aray as a Dal Structure M Pass 3: , 12 avd 120 12 Os fe 25 25 7 37 f= 33 33° «33-3337 ] 148} 48] [48] | 48} 57 57 57} [57 s Pass 4: | fee 12 12 } 25 25 25 33 Las 33} 37 37| 37} i [48] [48] [4a] i 57 [57 57} | Pass 5: ! fe 12 25 [25] : 33} 133} : a7] 7 48) [48] i 57| 57| + Algorithm i 1. Start | 2. Pass ' 4. ifx[iJ>x (+1) interchange x (i] and x [i+1] i=i+1 6 Ific=n -Pass goto step 4 7. Pass = Pass +1 If Pass void BubbleSort (int a[20), int n) i | int i, temp, pass; for (pass=1; pase for (i-0;"icen- { 1; pass++) Bass-1; i++) if(ali)>ali+1)) { tempea [i]; alij-aljea); ali+1]-temp; Void main() { ; ( ~-int a[20],i,n; Print£("How many numbers :"); scant ("¢d", gn) ; printf ("\nEnter the unsorted elements :"); for (is0;ix [i+] Pe Interchange x {i] and x [i+1] Pr Swap = TRUE 6 i=it a 7. Ifi<=n-Pass-l goto step 5 8. Ifswap = FALSE goto step 11 9. Pass = Pass +1 10. IfPassafi+1)) ‘ / temp=a [i] ; i / ali)=ali+1); ; ali+1)=temp; Bs swap=1; =0) /* no interchange */ {$2~“Insertion Sort ‘The basic idea of this method is to place an unsorted element into its correct position in a growing Sorted list of elements. We select one element from the unsorted set at a time and insert it ine ig correct position in the sorted set. The process continues till the unsorted set becomes empty. For example, in order to arrange playing cards, we pick one card at a time and insert this card in sc proper position in the cards held in the hand. : Sorted list Unsorted data Mlustration i ne ufon Data Structure 2610 Array as a Data Structure When a new element is to be inserted into the sorted list, it is compared with the elements in the sorted list and inserted at its correct place. This is done by shifting elements which are greater than the element one position to the right. The comparison is done backwards from the last element. 7 For example, to insert 14 in the example above, it is first compared with 22. Since 22>14, 22 is shifted right. Then it is compared with 15. Since 15>14, 15 is shifted right. Then it is compared with 7. Since 7<14, 14 is placed to the right of 7. ‘The insertior. process begins from the second element since the first element can be directly placed in the sorted set at position 0. Algorithm x is an array of elements. x{0] may be considered as'a sorted array of one element. The unsorted _listis x{1] to x[n-I]. 1. Start 2. j= ie. unsorted position starts from 1. 3. key =x {iJ 4. i=J—1 ile. sorted set starts from j-1 5. while x [i] > key and i>=0 xfi-1=xfi] i.e. shift element to the right x [i+ ]=key ice. insert key in correct position 7. j=j+1 /next unsorted element 8 Ifj

You might also like