You are on page 1of 27
i c . Often, we use these tools without Concern about their actual implementation, such as a welder can use a welding machine without knowing much about electricity. However, built-in data structures provide limited applications. So in order to get number of interesting and useful ways of structuring data, programmer must build various data s ‘tructures in his program. In this chapter, we will study some useful data structures. In order to store and retrieve individual data items, the accessing function are defined. Due to these functions, the collected data is organized. For implementation of any data structure we should know: (a) How much memory space is required (b) How to access data by coded functions Data is simply a set of values. Generally collection of data is organized into hierarchy of fields, records, and files. e.g. If you consider student's information as follows: Name Roll No. Physics Chemistry Maths « Sachin 1 oii 90 92 In above e.g. name, roll no., physics, chemistry and maths are fields. The collection of actual values of fields forms a record. The collection of such type of tecords forms a file. Primary Ke' : ‘ a Bea field may uniquely determine the record in a file. Such a field is rimary key. In the above example Roll No. is a primary key. re vThe Bae Greanization of data into fields, records, files may not be complex enough to maintain and efficiently process certain collections of data. For this reason data is organized into more complex type of structures. 2.1 DATA STRUCTURE _ The logical or mathematic: called data structure. » The choice of data mo 1) It must be rich enough data in real world. al model of a particular organization of data is depends on two considerations, ‘i 3 is structure to mirror the actual relationships of ———— Data Structures 33 2) Structure should be simple enough that can effectively process the data whenever necessary. Few examples of data structures are array, list, binary tree etc. Data Structure Operations Z The data in data structures are processed by certain operations like: 1)Traversing: For processing certain item in record, each record is accessed exactly once. (Visiting a record) 2)Searching: Finding location of a record with given key value or finding the locations of all records, which satisfy one or more conditions. 3)Inserting: Adding a new record to the structure. 4)Deleting: Removing a record from a structure. 5)Sortin; 6)Merging: Combining the records in two different sorted files into a single sorted file. : Arranging the records in some order. 2.2 ALGORITHMIC NOTATIONS An algorithm is a finite step-by-step list of well-defined instructions for solving a particular problem. The form for formal representation of an algorithm consists of two parts, 1) It tells the purpose of algorithm, identification variables, which occur in algorithm and lists, input data. 2) It contains list of steps that is to be executed. There are certain conventions, which has to be followed in algorithms. i) Step, control, exit The steps of algorithm are executed one after another beginning with step1. Sometimes control may be transferred to step ‘n’ of algorithm by statement ‘goto step n' or using control structures. The algorithm is completed with the statement ‘Exit’, ii) Comments It is given in brackets either at beginning or end of step. It indicates the main purpose of step. iii) Variables Variable names are generally given in capital letters. Also variables used as counters or subscripts are in capitals. Assignment Statement Tt uses: = notation e.g. MAX: =5 Where MAX is a variable Sat ae o 2 2) Selection Logic faba nO This logic employs a number of conditions. Accordingly which condition is satisfied that module is executed. Generally the following statement gives end of such a structure. m [End of IF Structure] These structures are of three types > — Single Alternative - This structure has the form IF condition, then: [Module A] [End of IF Structure] Fig. (2.2) Single Alternative If condition holds true then Module A is executed, otherwise Module A is skipped and control transfers to the next step of algorithm. > Double Alternative 2 This structure has following form IF condition, then: {Module A] Else: {Module B] [End of IF Structure] ‘Where K = Index variable R = Initial value of K S = Final value of K __T=Step (increment/decrement) *The loop is executed for all values of K starting from R to S with step of T. + K> Auto (1999) ar array _ N=Total no. of elements in the array K = Any positive integer, K<=N a ‘This algorithm inserts an element ITEM into the K™ position in LA. 1, [Initialize Counter] Set J: =N 2. Repeat steps 3 and 4 while J>K 3, [Move J"element downward] Set LA [J+1]:=LA UJ] 4. [Decrease Counter] Set J: = J-1 [End of step 2 loop] S: {Insertelement] Set LA [K]: = ITEM 6. [Reset N] Set N: = N+1 Te Exit Number 9s inserted 4 is deleted 3 3 3 4 4 9 5 9 5 6 5 6 8 6 8 8 Original Addition of Deletion of Array element in element from middle arrat array Fig. (2.6) Deleting Element in array It refers to the operation of removing one of the elements from A. Deleting) an end of array is simple but deleting an element somewhere in the middle of th array would require that each subsequent element be moved one location upward if order to fill up the array. Algorithm for deleting an element This algorithm deletes the K® element from a linear array LA and assigns itl a variable ITEM. DELETE [LA, N, K, ITEM] LA =Linear array _ N = Total no. of elements in array K = Any positive integer such that K<=N 1. Set ITEM: =LA [K] 2. Repeat for J: = K to N-1 Data Structures “a ee 3. [Move J+1" element upward.] Set LA [J]: =LA [J+1] [End of loop] 4. [Reset the number N of elements in LA.] Set N: =N-1 5. Exit 2.4 BUBBLE SORTING Sorting means, rearranging the elements in increasing or decreasing order. Suppose A[1], A(2I,..... A{n] are in memory. Then bubble sort algorithm works as follows: Compare A [1] and A [2] and arrange them in the desired order, so that Afi], A[2]. Then compare A [2] and A [3] and arrange them so that A [2], A [3]. Continue this process until we compare A [N-1] with A [N] so that A [N-1], A [N]- Step 1: It involves n-1 comparisons. During this step, the largest element is coming like a bubble to the n' position. When step 1 is completed A[N] will contain the largest element. Step 2: Repeat step 1 with one less comparison. Step 2 involves n-2 comparisons, when step 2 is completed we get second largest element A[N-2]. Step 3: Repeat step 1 with two less comparisons. It involves N-3 comparison. Step N-1: Compare A[1] with A(2] and arrange them so that A(1] ,[A(2}. ‘After n-1 steps the list will be sorted in increasing order. Pass The process of sequentially traversing through all or part of a list-is called a pass. Each of above step is actually a pass. The bubbie sort algorithm requires N-1 passes where n is number of input items. The time required for sorting algorithm is measured in terms of comparisons. Specifically there are n-1 comparisons during first pass n-2 comparisons in second pass and so on. Complexity of algorithm f(a) =(n-1) + (0-2) +... 4241 n(n-l) 2 =o(n’) ‘The time required to execute bubble sort algorithm is proportional to n° where ‘nis number of input items. [Bubble Sort] BUBBLE [DATA, N 1 is an array with N elements. This algorithm sorts the elem ay 1. Repeat steps 2 and 3 for K = 1 to N-1 . Set PTR: = | [? pass pointer PTR.] . Repeat while PTR<= N-K. [Executes pass.] a) IF DATA [PTR] > DATA [PTR + 1], then: Interchange DATA [PTR] and DATA [PTR + 1] {End of IF structure] b) Set PTR: =PTR+1 [End of inner loop.] {End of step 1 outer loop.] 4. Exit Searching Searching refers to the operation of finding the location of given element in the list. The most commonly used searching algorithms are linear’search and bina search. In linear search the given element is compared with each element of list one’ by one. This method is also called sequential search. Algorithm Linear Search LINEAR [DATA, N, ITEM, LOC] Here DATA is a linear array with N elements and ITEM is a given item of information. This algorithm finds the location LOC of ITEM in DATA or sets LOC: = 0, if search is unsuccessful. 1, [Insert ITEM at the end of DATA] set DATA [N+1]: = ITEM 2. [Initialize counter] set LOC: = 1 3. [Search for ITEM] Repeat while DATA [LOC] + ITEM Set LOC: = LOC +1 [End of loop] 4. [Successful?] IF LOC = N+1 then set LOC: =0 5, “Exit 2.5 BINARY SEARCH While using this searching method array should be sorted in increasin numerical order. ] Suppose DATA is an array, which is sorted in increasing numerical order, af we want to find out the location LOC of a given ITEM of information in DATA. Then binary search algorithm applied works as follows. During each seat for ITEM is reduced to a segment of elements of data: 4 i DATA [BEG], DATA [BEG +1], DATA [BEG+2], DATA [END] Measured by the number of f (n) of comparisons to ATA contains n elements. Each comparison reduceg in half. So we require at most f (n) comparisons to locate ITEM where -£() = [log n}+1 Applications: () This algorithm is used to search ordered array. (2) To find out the target element whether it is Present or not. If present ij Correspondingly it gives Position in the array. Limitations: (a) The given list must be sorted, (b) The access of list must be random means; the middle element can be accessed. 2.6 POINTER ARRAYS An array whose each element is a pointer is called pointer array. eg. int * p[4]— pointer array int a [4] — array of integers pLO]= &alo} Pll] = &afi] PI2}= &a[2] = &a[3] This pointer array p holds the address of each element in array a so it is calf pointer array. ) Detailed Example of Pointer Array Suppose TEAM is an array, more specifically locations of first Team which contains the locations of different teams lements in different teams, Teami| 4 Sactun Ajay Ravi Rakesh Ait Nilesh] Team 2 Viay Kapil Narendral Ramesh|* Team 3 Niren $$$ Team 1 Team 2 4 een he =e Team 3 9 Team 4 12+ O©oVYF®H®FWN + 6 eee Sentinel of group [End of group] Fig.(2.7) Example of an Array Data Structures eee eee Team [L] and T |- i i Pena {L] and Team (L+1]-1 contains respectively the first and last elements e.g. Team 1———>1 ———+ first element of Team 1 Team esa 4 —— last element of Team 1 Team 4 points to the sentinel of lists. 2.7 RECORD Ee: A record is collection of related data items. Each data item is termed as field. pa collection of similar records. Each data item may be a group item composed of sub items. Comparison Record Linear Array qd) A record may be a collection of ‘The data items in an array non homogeneous data; may have same data types. i.e. the data items in a record may have different data types. ‘There may be natural (2) _ The data items ina record are ordering of its elements. indexed by attributes . So there may not be a natural ordering if its elements Example of Record Suppose a college keeps following data items. ITEM Name Address Stream Phone a record of each student, which contains the SUBITEM Last, First, MI (Middle Initials) division The structure of above record is described as follows: 1, Student 2. Name 3, Last 3. First 3, MI (Middle Initial) 2. Address 2. Phone: 2. Stream 3, Division SHOE HOHE Information part of node 2 Next pointer field of node 2 Fig. (2.8) A Linked List The following diagram shows a linked list 1) Name of player and no. of runs from an information part while next pointer field gives the next node's address in linked list. 1) Start —> 4 2)4 ——> 1 3) 1 ——> 3 4)3 ——»2 5)2 —— last node Name of Player Runs Next START | 4 Ajay 84 3 Sachin 90 4] NULL Ravi 40 ale apo Ajit 80 1 Fig. (2.9) Representation of Linked List in Memory Let List be a linked list. Then LIST will be maintained in memory as follows, 1) LIST requires 2 arrays; we will call them here INFO and LINK such that INFO [K] and LINK [K] contain respectively the information part and next pointer field of a node K of list. 2) LIST also requires a variable name such as START, which contains the location of beginning of list and next pointer sentinel denoted by NULL, which indicates end of list. ‘The following e.g. shows how linked list is stored in memory. The nodes of the list need not occupy adjacent elements in the arrays INFO and LINK and more than one list may be maintained in the same linear arrays INFO and LINK. But each list must have its pointer variable giving the location of its first node. Pi INFO [9] = L, LINK [9] = 3 , LINK (3 .... last node i.e. "LINK LIST" string is stored in form of Linked List. Advantages of Linked List es Generally lists are stored in form of arrays. But in arrays insertion ai ‘deletion is not easy. Also array size can't be easily increased. Using linked list th problem can be solved. 2.9 TREE DATA STRUCTURE ss In case of Link data structure, each node has link information of next nod Thus sequentially, we can access all the nodes. It will be time consuming | searching long list;because each node has information about next node only. an In order to reduce time consumption, binary tree concept was develop Before going to binary search tree, let us learn basics of tree structure. ‘Tree is an hierarchical structure of collected data items.It is nonli structure. 0 ‘ A Node : It is as element of list. It may be a character, strin; or number. 4 BCmIAKANVALNE Sequential Representation en ‘Suppose T is complete binary tree then only single line as follows: .ar array TREE is used SCIAKHALNE Lo S\= 22 Tf: /\ — 15 ah 30 90 VA cele 15 25 88 ny 12 Fig. (2.17) A % 1) The root R is stored in TREE [0] 1) If node N occupies TREE [K] then left child is stored in TREE [2 * K] and its right child is stored in TREE [2 * K + 1] 2) If TREE [1] = NULL then it is empty. 2.10 STACK AND QUEUE DATA STRUCTURE 4 A stack is a list of elements in which an clement may be inserted/deleted only at one end, called the top of stack. Elements are removed from a stack in a reverse order of that in, which they were inserted into the stack. So it is generally referred as Data Structures 2) Suppose A is deleted from queue: a Soe J J Front element Rear element 3) E & F elements are added: (ey eS Pe Cae pre pe Rear element 4) B is deleted: 1) Select the correct a) b ©) d es Front element Cc D Front element Rear element QUESTIONS t alternative and rewrite the following: e with depth of 5 is 3 Maximum number of nodes of symmetric binary tre (Mar. 2002) i) 5, ii) 25, iii) 31, iv) 32 4d the location of record with a given key value is called __. ii) Sorting, iii) Finding, iv) Merging (Mar. 2005) ing the process of sequentially traversing through all or To fin i) Searching, In bubble sort part of a list is called ___- i) Count, iv) None of these ns required for bubble sorting of an array of 'n' (Specimen Paper) i) Step, ii) Pass, The number of compariso! elements is ___- i) n(n-1)/2, ii) n/2, iii) Fogen, iv) logion is the operation of rearranging the elements of an array either in increasing or decreasing order. i) Sorting, ii) Searching, iii) DMS, iv) DBMS (Apr 90) a) Traversing b) Searching c) Sorting i) Maximum number of nodes of symmetric binary tree with depth of 7 is a) 125 Bio? sees (Ma D j) Stored list is essential in-process of an array- i) Linear Search ii) Binary Search iii) Traversing, iv) Insertion (Mar. k) Record contains —— data. ) none of these i) Homogeneous ii) Non-homogeneous iii) same iv) data structure does not require contiguous memory allocation. inter array d) Linked list ) a) array b) string c) po’ m) A record is collection of —— a)files b) arrays) fields d) maps 2 : Define the terms: a) Field b)Record c)File 3. Explain the following data structures with suitable diagram. b) Linked list, ¢) Tree a) Linear array, 4. What is linked list? Explain it with suitable example. What are types of binary tree? Explain it with suitable example. What is Binary Tree? With a suitable example, explain the terminology. family relationship between the elements of a binary tree. (Mar.05 7. What is a Binary Tree? With a suitable example, show the relationship betwe total number of nodes and depth of a binary tree. 8. How binary trees are represented in memory? Explain it. 9. Write the difference between linear search and binary search. 10. Explain any four data structure operations. "13, Explain the algorithm for inserting an element from see 14, What js a record? How it differs from a linear array? 15, What is searching? Explain binary search algorithm. 16, Explain linear search algorithm with a suitable example. 17. What do you understand by the term “Searching”? Which are the searching algorithms? Explain the linear searching algorithm. (Mar.2004,07) 18. How Linked list are represented in memory? (Mar. 2017) yy Linked list? With suitable examples show the representation of (Mar.2003,2004) six nodes 19. What is meant b; linked list in memory. 20. What is linked list? Show a linked list with suitable example having (Mar. 03, Mar. 07, Mar. 08) with a properly labeled diagram. information elements 21. With a suitable example and diagram show a link list with it and the link fields from start to null pointer. (March 2005) 22. With a suitable example, show labeled diagram for link between two nodes (March 2006) having the information part and next pointer field. advantages of binary search algorithm with a suitable example. State 23. Explain the any two disadvantages or limitations of binary search. (Mar. 07) 24, Explain the bubble sort algorithm with suitable example.(Mar. 02, 05, Mar. 08) 25. What is sorting? Explain bubble sorting algorithm. (Mar.2011) tree? Draw the tree structure for the following expression: 26. What is binary (Mar. 2002). E=(a-by/{(c* d) +e] 27. Draw the tree structure for the following expression: Bs [a+(b-o)] *(-©)/(F+e-h)] binary tree? Draw the tree structure for the followii

You might also like