Data structure
Data structure
END) i) Display ITEM is NOT found, Set LOC = NULL MID=(14+3)/2 = 4/2 =2 IF( ITEM == DATA[2)) IF 30 == 20) NO ELSE IF(30< 20) NO ELSE Set BEG= 2+ 1=3 BEG = END = MID = 3 IF 30 == DATA(3})Difference between Linear search and Binary search Linear search Binary search 4) In linear search array (data) may or may not be sorted. ie. it is not neccessary that data should be sorted. 4) in binary search array (data) should be sorted, 2) Item to be search is compared with every element of array starting from first location, 2) Item to be search is compared with middle value of array. 3) Its less efficient than binary search 3) Its more efficient than linear search 4) Time complexity of linear search is O(n) 4) Time complexity of binary search is Ollog 4n) 5) ExampleAlgorithm : Sorting arranging elements in some logical order is called Bubble sort: Eg. Reration/ pai DATA NeGKeL PROS DATA NeS Ke2 Pree DATA N#=5 Ke 3 PIRe? 1.33 22 22 22 22 2 2 2 2.22 33-33 33 33 33-533 33 3.44) 44-44-44 44 4-4-1 prme*4.55) 55 55°" 55-41 mu oles 5 (ay) 11 11 11-88 55 55 «65 DATAIPTR) DATA PTR +a) e DATA (3) DATA (2) K=12,3,4 so om K=1to4 \k =iton- 4 DATA (PTR) > DATA (PTR + 1) ) ves Interchange their values PTR © PTR oDAlgorithm : Sorting arranging elements in some logical order is called as sorting. Bubble sort : Eg. Reration pas: DATA N=SK=i pm DATA N=S K=2 PIR=3) DATAN=5 K=3 Pim=2 aGG3)22 22 2 aGz} 22 22 22 1 (22) 22 2fz2}—3333 33 2[33p/ 33-33 330 pm-2 [33}~39. PETiMe eet) pm= 3044) 4446-11 3iaa) om-4/55| 85 55-55 af) 1a dias 44a) 44 Sia) aa aa as 555 55 55 55 5 ss 65 DATAIPTR| DATA (PTR +4) re eke DATA [| DATA(2) Koh 8,06 sn neee 33 22 [ksitea 7 om-1 22)—48 site fiat ta DATA [PTR|> DATA (PTR + 1)) | Yes | ral S [pre ser | Interchange their a & | ah “4 535) 55BUBBLE (DATA, N) Let DATA is a linear array of size N and this algorithm will sort DATA in accending order. Steps : 1) Start DATA N=S Kei.2.3.4 2) Repeat steps 3rd and 4th for K = 1toN- 1by 1 1[33}22 22 22 22 3) Set PTR=1 2 [22}/33-33 33 33 4) Repeat and 6th while PTR <= Nk a owe qcopatacae * a ee pres [ia] 41 11 11-~ss IF(DATA[PTR] > DATA[PTR + 1) then, — Interchange their values is (PTR <=N-K)? i) Set TEMP = DATA[PTR] TMP = 33 ig ig cS. 4)? i) Set DATA[PTR] = DATA[PTR+1] OATAIN=22 js (5 <= 4)? NO oe SRTArRCs= ver PATARI“33 eDATAIPTR] > DATAIPTR + 1) 6) Increment counter F(DATA[4) > DATAIS)) pre =PTR +4 (55> 19) YES 7) Display Sorted DATABUBBLE (DATA, N) Let DATA is a linear array of size N and this algorithm will sort DATA in accending order. ‘Steps : 1) Start DATA NeGKw1 2.3.4 2) Repeat steps 3rd and 4th for K = 1toN-1by 1 1 [G3}22 22 22 22 3) Set PTR=4 2 (aah 33-83 33 33 aiepeatvep snandemeaermeanck 3 EES 5) Compare elements water I(DATAIPTR] > DATAIPTR + 1) then, viele i aelhegliaallibad Interchange thelr values is (PTR <= N-K)? i) Set TEMP = DATA[PTR) TEMP*33 ig (g ce. 4)? ii) Set DATA[PTR] =DATA[PTR+1] OATAIR"22 is (g ce 4) ? NO i) Set DATA|PTR+1) = TEMP DATAIZ +33 IF(DATA[PTR] > DATAIPTR + 1]) ona IF(DATA[4] > DATA[S)) = | 1) 7) Display Sorted DATA ee 8) Stop iPointer Array: In array if each elements are pointer then its called as pointer array. Pointer is a variable which contain the address of the elements. Pointer points to an element in list. Let we have four groups. Each group consist of list of members. Membership list is to be stored in memory. The most efficient method is to form two [s | arrays. One is members consisting of all members one after the other. Ls | and another pointer array group contaning |_10_| the starting locatons of different groups. [ 12 | Observe that the elements of pointer array are starting addresses of each group Xil Class : Students (Members) MB=4 cs=3 rT =2 ELE=2Pointer Array : In array if each elements are pointer then its called as pointer array. Pointer is a variable which contain the address of the elements. Pointer points to an element in list. _- ooo n—25 _] c001H eo / coosH| 58 | { PTR1, PTR2, PTR3...} Addresses fig. Memory locations Pointer ArrayPointer Array : In array if each elements are pointer then its called as pointer array. Pointer is a variable which contain the address of the elements. Pointer points to an element in list. a : cooo Zool 24 ]2 ao = index pret conan 67 ]3 mnbers coo1n | -- 7 con] 58 | pra coal 38 5 { PTR14, PTR2, PTR3... } Addresses fig. Memory locations Pointer ArrayRecords : Its a collection of fields. Record may contain non-homogeneous data.Records : Its a collection of fields. Record may contain non-homogeneous data. ie. data items in record may have different Eg. data types. 4. Student The elements in record are described by 2. Roll No, level numbers. 2. ra ane i First Name // in record natural odering of elements is not possible 3 hocee thonal The number to left of each variable is called level number. 3.Last Name Each group item is followed by its subitem. 2. Gender The level of subitem is 1 more than the level of group item. 20 ‘Any particular field in record can be accessed by 3. Benth period{dot) Student Full Name.Last Name 2 ater 4. Student(15) It indicates 15 records ina file. Any particular field in file can be accessed by Student.Age(5] »Representation of records in memory Eg. +.student 2. Roll No. Records contain non homogeneous data. 2. Full Name To store record in memory we need parallel linear arrays. 3. First Name for this example of student record, we requires 5 linear 3. Last Name arrays such as Roll No., First Name, Last Name, Age 2.Age and Class _<_— one array for each elements of data item. K __RollNo FirstName Last Name Age ClassRepresentation of records in memory Eg. 4. Student 2. Roll No. Records contain non homogeneous data. 2. Full Name To store record in memory we need parallel linear arrays. 3. First Name for this example of student record, we requires 5 linear 3. Last Name arrays such as Roll No., First Name, Last Name, Age 2.Age and Class 2. Class ‘one array for each elements of data item. Roll Nok], First Name[K], Last Name{k], Age{K], Class[K] h must be belongs to same record RollNo FirstName _Last Name Age ClassLinked list ; Its a linear collection of data elements, called nodes. linear order is given by means of pointer. each node is divided into two parts, first part contains the information part of the elements and second part contains the address of the next node in the list Information part \\ Pointer of next node / (Link part) EPO EHE RHEE HE BH Start ‘fame (Head) L-Linked list ; Its a linear collection of data elements, called nodes. linear order is given by means of pointer. each node is divided into two parts, first part contains the information part of the elements and second part contains the address of the next node in the list Information part Pointer of next node / (Link part ) is [eH se [see le Ti] Fig. Linked list al 6 Sie. NULL te Start /Hame (Head)Linked list : Its a linear collection of data elements, called nodes. linear order is given by means of pointer. each node is divided into two parts, first part contains the information part of the elements and second part contains the address of the next node in the list \ per 7 oe v ¥ = ‘PHEEHE RHE [eH HE fk 8 4 2 6 3 Fig. Linked list with 6 nodes, The left part represents the information part of node. The right part represent pointer field of node. The arrow is drawn from this field to next node. pointer field of last node conatin null pointer. The address of first node in list called as start or name. We need only this address to trace the linked list.Representation of linked list in memory (aL pot er * FeCl He [2 HE Ti) [a 3 START Linked list can be represented by two parallel linear arrays. ‘One is INFO containing information part and other is LINK containing nex t pointer field. The name of link ed list, start, contains beginnings of the list. STARTRepresentation of linked list in memory . ae alge] Atle bh Ts Linked list can be represented by two parallel linear arrays. ‘One is INFO containing information part and other is LINK containing next pointer field. The name of linked list, start, contains beginnings of the list. START points to 5th element INFO[5] = A and LINK[S]=8 so next node address is 8 INFO[8] = B and LINK[8]= 4 so next node address is 4 INFO[4] = C and LINK(4]=2 so next node address is 2 INFO[2] = D and LINK[2]=6 so next node address is 6 INFO[6] = E and LINK[6]=3 so next node address is 3 INFO[3] = F and LINK(3] = NULL The list is ended here.Tree sts a non linear data structures. It used to represent data containing hierachical relationship between elements. > Root ®) Root / College _ ay | Ne ay 8 Arts ©) Commerce Science A Binary tree Tis defined as a finit 7 of elements, called nodes, such that - a) Tis empty ( null tree b) T contains a distiguished node R, called root of T and the remaining nodes of T form an ordered pair of disjoint binary trees T1 and T2. 7 Root Eg. T R Here T conatins root R and T4 and T2 are subtrees of R _/ \_ lf 11 is non empty, then its root is called as left 1 {2 successor of R and if T2is non empty, then its ~ root is called as right successor of R.a) Tis empty ( null tree or empty tree ) or b) T contains a distiguished node R, called root of T and the remaining nodes of T form an ordered pair of sjoint bin 'y trees T1 and 12. 7p) Roo! Eg. T R) Hi conatins root R and Ti and ¥2 are subti a non empty, then its root Is cal ay (T2) sue T2is non empty, left - a right root is Successor of Any node in binary tree has 0, 1 or 2 successor. The nodes with no successor (child) are called as termin: Pg Pia nodes, It is also called as leaf. \) Root : The node which has no parent. T r Sibling : children of the same parent. ~ The two binary tree are said to be similar if they have T: Binary tree same stucture. Y RootAlgebriac ex pression and binary tree the algebraic expression can be represented by binary tree. Eg. E=(@B/(Cer a Fe) Sparc nmer eh se i os * 3 Np ae ONS or wo[(a+b)*e]/[a*((b-¢)+a)] oor Ooa+2b at2*bC=(X+Y)°-(5A+B') C=(X+Y)43-(5*A+B42)Binary tree : parent and child Suppose Nis node in T with left successor S1 and right successor s2, then Nis called as parent (father) of s1 and s2. T parent The line drawn from node N toa successor is called an edge. Sequence of consecutive edges is called a path. et child N-$2-A-D Gi ($2, Terminal node is leaf and path ending in a leaf is called a branch — av The depth (height) of a tree Tis the maximum number of nodes: aa (B) ina branch of T. left childThe Tis said to be complete / symmetric if allits levels except the last, have maximum number of possible nodes. (RY bevero Depth (n) = 3 Cc a) leveld ifn=4 totalnodes=2 oo, B ifm=2 totalnodes=3 Level? QO © F ifn=3 totalnodes=7 = ne? ar ifn=4 totalnodes = 15 7 T: 2tree / extended binary tree Max Nodes =2"-4 ifm=5 totalnodes = 31 The tree T is said to be a 2 tree or an extended binary tree if each node N has either 0 or 2 childeren. the node with 2 children are called are internal nodes. the node with 0 children are called are external nodes.4) Linked representation Eg Let T be a binary tree root re [51/2] ~ ] “ ®) Fe [xiB]x] 7 [xDix] (x[E[x] —_— The linked representation uses three parallel arrays, INFO, LEFT, RIGHT and pointer variable ROOT. Each node N of T will corresponds to a location K such that: 4. INFO[K] contains the data at node N 2. LEFT[K] contains the location of left child of node N. 3. RIGHT[K] contains the location of right child of node N.1) Linked representation Eg, Let T be a binary tree [e|¢l4] 5 . [XBix] —[x/E[x] area 6 4 The linked representation uses three parallel arrays, INFO, LEFT, RIGHT and pointer varigble ROOT. Each node N of T will corresponds to a location K such that: 1. INFO[K] contains the data at node N 2. LEFT[K] contains the location of left child of node N. 3, RIGHT[K] contains the location of right child of node N. 4. ROOT will contain location of root R of T.1) Linked representation root 5|Al2 (ig) (sigia) ——(xiBIx) 5 2 [xD|x} [x] Ex] X= NULL 6 4 The linked representation uses three parallel arrays, INFO, LEFT, RIGHT and pointer variable ROOT. Each node N of T will corresponds to a location K such that: 4. INFO[K] contains the data at nocle N i 2. LEFT[K] contains the location of left child of node N. 3. RIGHT[K] contains the location of right child of node N. 4, ROOT will contain location of root R of T. Linked representation of binary tree2) Sequential representation Eg. Let Tbe a binary tree In sequential representaion a linear array is used. The root A is stored in TREE[1]. If anode N occupies TREE[K], then — its left child is stored in TREE[2 * K] and its right childis stored in TREE[2*K+ 1) © TREEIZ] Kea ( NAQRWN=2) Sequential representation Eg. Let T be a binary tree TREE root 7. 1Ta] ZK oO ae] oo OG 38 = 4 In sequential representaion a linear array is used. : | The root A is stored in TREE[1}. 7 -_] If anode IN occupies TREE[K], then its left child is stored in TREE[2 * K] and its right child is stored in TREE[2*K+4] © TREE[1] Kei Left Child C ; TREE[2 * K| TREE(2* 1) TREE (2) Right Child B : TREE[2 * K + 1] TREE[3]2) Sequential representation Eg. Let T be a binary tree : root(@) demas 1LA | atc | 3{_B | 4{_p | , onak . 5 E | In sequential representaion a linear array is used. 6p *_| The root Ais stored in TREE[1]. 7L If anode N occupies TREE[K], then its left chid is stored in TREE[2 * K] and its right child is stored in TREE[2 * K + 1] C TREE[2] K=2 Left Child D : TREE[2 * K] TREE[2* 2] TREE [4] Right Child E : TREE[2 * K + 4] TREE([5]Stack and Queue: fa [™ [a7] 1 2 3 4 Item = 56 K=123,4,5 item can be inserted at first position, last position as well as at middle position. Item = 14 K=2,1,3,4 item can be deleted from first position, last position as well as from middle position. If we want to restrict inseration and deletion operations, so that they can take place only at beginnig or the end of list, not in the middle. Then data structure stack and queue art useful in such situation.Stack : A stack is a data structure in which items may be PUSH POP added or removed only at one end. \ / the item can removed or added only from TP! Front the top of the stack, [46 _| Last item added = 46 ‘st item to be deleted = 4 "PUSH" is the term used to insert an element into stack | 14 "POP" is the term used to delete an element from stack __35_| A stack is also called as LIFO.( Last In First Out ) a Bottom / Rear jueue : rStack : A stack is a data structure in which items may be PUSH PoP added or removed only at one end. Top !Front| ¥ / the item can removed or added only from iP the top of the stack. [46 _| Last tem added = 46 first item to be deleted = 46 "PUSH" is the term used to insert an element into stack [| 14 | “POP" is the term used to delete an element from stack A stack is also called as LIFO. ( Last In First Out ) Bottom / Rear Queue : ‘A queue is a linear list in which items may be added por PUSH only at one end and items may be removed only at 27 | 35 | 14 | 46 / other end. r 2 3 4 The queue is also called as FIFO. ( First In First Out ) Front Rear r