You are on page 1of 109

ARULMIGU MEENAKSHI AMMAN COLLEGE OF ENGINEERING NAMANDI POST, VADAMAVANDAL 604 410.

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

CS-2208 DATA STRUTURES LABORATOR

N!"# R#&'()#* N+",#* #!* - B*!./0 S#"#()#* A/!1#"'/ #!*

$ %%%%%%%%%.. $ %%%%%%%%%.. $ %%%%%%%%%.. $ %%%%%%%%%.. $ %%%%%%%%%.

ARULMIGU MEENAKSHI AMMAN COLLEGE OF ENGINEERING VADAMAVANDAL 604 410

CERTIFICATE
This is to Certify that the bonafide record of the practical work done by Register Number ....

of II year B.E Computer !cience and Engineering" submitted for the B.E # $egree practical e%amination III S#"#()#*2 in CS2208 DATA &'(& ) &'(*

STRUCTURES LAB during the academic year

S)!33 '. -C0!*&#

H#!1 43 )0# D#5!*)"#.)

!ubmitted for the practical e%amination held on #################

I.)#*.!6 E7!"'.#*

E7)#*.!6 E7!"'.#*

INDE8

S.NO 1 2

DATE

CONTENT
IMPLEMENT SINGL AND DOUBL LINKED LISTS REPRESENT A POL NOMIAL AS A LINKED LIST AND 9RITE FUNCTIONS FOR POL NOMIAL ADDITION IMPLEMENT STACK AND USE IT TO CONVERT INFI8 TO POSTFI8 E8PRESSION IMPLEMENT A DOUBLE-ENDED ;UEUE<DE;UEUE29HERE INSERTION AND DELETION OPERATIONS ARE POSSIBLE AT BOTH THE ENDS IMPLEMENT AN E8PRESSION TREE. PRODUCE ITS PRE-ORDER, INORDER, AND POST-ORDER TRAVERSALS IMPLEMENT BINAR SEARCH TREE

PAGE NO SIGNATURE

6 > 8

IMPLEMENT INSERTION IN AVL TREES IMPLEMENT PRIORIT ;UEUE USING BINAR HEAPS IMPLEMENT HASHING 9ITH OPEN ADDRESSING IMPLEMENT PRIM@S ALGORITHM USING PRIORIT ;UEUES TO FIND MST OF AN UNDIRECTED GRAPH

? 10

E8.NO$01<!2 DATE$
IMPLEMENT SINGL LINKED LISTS

AIM$ To write a program to implement singly linked list. ALGORITHM$ S)#51+ !tart S)#52+ Creation+ ,et the number of elements- and create the nodes ha.ing structures $/T/ 0IN1 and stores the element in $ata field- link them together to form a linked list. S)#5:+ Insertion+ ,et the number to be inserted and create a new node store the .alue $/T/ field. /nd insert the node in the re2uired position. S)#54+ $eletion+ ,et the number to be deleted. !earch the list from the beginning and locate the node then delete S)#56+ !top. the node. S)#5=+ $isplay+ $isplay all the nodes in the list. in

SOURCE CODE$ 3include4stdio.h5 3include4conio.h5 3include4stdlib.h5 3define N600 ' typedef struct list 7 int no8 struct list9ne%t8 :0I!T8 0I!T 9p-9t-9h-9y-9ptr-9pt8 .oid create .oid"8 .oid insert .oid"8 .oid delet .oid"8 .oid display .oid"8 int ;-pos-k<(-count8 .oid main " 7int n-i<(-opt8 clrscr "8 p<N6008 printf =>d=-si?eof 0I!T""8 printf =enter the no of nodes+@n="8

scanf =>d=-An"8 count<n8 while i4<n" 7 create "8 iBB8 : printf =@n enter your option+@n="8 printf =(.insert@t &.delete@t *.display@t C.e%it@n="8 do 7 scanf =>d=-Aopt"8 switch opt" 7 case (+ insert "8 countBB8 break8 case &+ delet "8 count##8 if count<<'" 7

printf =@n list is empty @n="8 : break8 case *+ printf =list elements are+ @n="8 display "8 break8 : printf =@n enter your option @n="8 : while optD<C"8 getch "8 : .oid create " 7if p<<N600" 7 p< 0I!T 9"malloc si?eof 0I!T""8 printf =enter the element+@n="8 scanf =>d=-Ap#5no"8 p#5ne%t < N6008 h<p8 :else 7

t< 0I!T9"malloc si?eof 0I!T""8 printf =@n enter the elements="8 scanf =>d=-At#5no"8 t#5ne%t<N6008 p#5ne%t<t8 p<t8 :: .oid insert " 7 t<h8 p< 0I!T9"malloc si?eof 0I!T""8 printf =enter the element to be inserted+@n="8 scanf =>d=-Ap#5no"8 printf =enter the position to insert+@n="8 scanf =>d=-Apos"8 if pos<<(" 7 h<p8 h#5 ne%t<t8 : else 7 for ;<(8;4 pos#("8;BB"

t<t#5ne%t8 p<t#5ne%t8 t#5ne%t<p8 t<p8 : : .oid delet " 7t<h8 printf =enter the position to delete+@n="8 scanf =>d=-Apos"8 if pos<<(" 7 h<h#5ne%t8 : else 7 t<h8 for ;<(8;4 pos#("8;BB" t<t#5ne%t8 pt<t#5ne%t#5ne%t8 free t#5ne%t"8 t#5ne%t<pt8 t<p8

: : .oid display " 7 t<h8 while t#5ne%tD<N600" 7 printf =@t>d=-t#5no"8 t<t#5ne%t8 : printf =@t>d@t=-t#5no"8 :

OUTPUT$ Center the no of nodes8 * enter the element+ ( enter the elements& enter the elements* enter your option+ (.insert * list elements are8 ( ( enter the elements to be inserted+ (& enter the position to insert+ ( enter your option * list elements are8 (& ( enter the elements to be inserted+ (* enter the position to insert+ * enter your option ( enter the elements to be inserted+ (C enter the position to insert+ ( & * enter your option & * enter your option &.delete *.display C.e%it

E enter your option * list elements are8 (& ( (* & * (C enter your option

RESULT$

E8.NO$01<,2 DATE$
IMPLEMENT DOUBL LINKED LISTS

AIM$ To write a program to implement doubly linked list with Insert- $elete and $isplay operations.

ALGORITHM$ ()#51+ !tart ()#52+ Creation+ ,et the number of elements to create the list. Then create the field. 0ink them together to form a doubly linked list. S)#5:$ Insertion+ ,et the number to be Inserted- create a new node to store the !earch the list and insert the node in its right position. ()#54$ $eletion+ ,et the number to be deleted. !earch the list from the beginning to locate node p with $/T/. If found then delete the node else display I$ata not FoundJ. S)#56$ $isplay+ $isplay all the nodes in the list. S)#5>$ !top. node. and try .alue. node ha.ing the structure B0IN1 $/T/ F0IN1 and store the elements in $ata

S)#5=$ F0IN1 GHs pre.ious node to GHs Ne%t node. B0IN1 GHs Ne%t node to GHs Gre.ious

SOURCE CODE$ 3include4stdio.h5 3include4conio.h5 3include4stdlib.h5 3define N600 ' typedef struct list 7 int no8 struct list9ne%t8 struct list9pre.8 : 0I!T8 0I!T 9p-9t-9h8 .oid create .oid"8 .oid insert .oid"8 .oid delet .oid"8 .oid display .oid"8 int ;-pos-k<(-count8 .oid main " 7 int n-i<(-opt8 clrscr "8 p<N6008

printf =enter the no of nodes+@n="8 scanf =>d=-An"8 count<n8 while i4<n" 7 create "8 iBB8 : printf =@n enter your option+@n="8 printf =(.insert@t &.delete@t *.display C.e%it@n="8 do 7 scanf =>d=-Aopt"8 switch opt" 7 case (+ insert "8 countBB8 break8 case &+ delet "8 count##8 if count<<'"

7 printf =@n list is empty@n="8 : break8 case *+ printf =@n list element are +@n="8 display "8 break8 : printf =@n enter your option @n="8 : while optD<C"8 getch "8 : .oid create " 7 if p<<N600" 7 p< 0I!T9"malloc si?eof 0I!T""8 printf =enter the element +@n="8 scanf =>d=-Ap#5no"8 p#5ne%t<N6008 p#5pre.<N6008

h<p8 : else 7 t< 0I!T9"malloc si?eof 0I!T""8 printf =@n enter the element="8 scanf =>d=-At#5no"8 t#5ne%t<N6008 p#5ne%t<t8 t#5pre.<p8 p<t8 :: .oid insert " 7 t<h8 p< 0I!T9"malloc si?eof 0I!T""8 printf =enter the elements to be inserted+@n="8 scanf =>d=-Ap#5no"8 printf =enter the position to insert+@n="8 scanf =>d=-Apos"8 if pos<<(" 7 h<p8

h#5ne%t<t8 t#5pre.<h8 h#5pre.<N6008 : else 7 for ;<(8;4 pos#("8;BB" t<t#5ne%t8 p#5ne%t<t#5ne%t8 t#5ne%t<p8 p#5pre.<t8 :: .oid delet " 7 printf =enter the position to delete+@n="8 scanf =>d=-Apos"8 if pos<<(" 7 h<h#5ne%t8 h#5pre.<N6008 : else 7

t<h8 for ;<(8;4 pos#("8;BB" t<t#5ne%t8 t#5ne%t<t#5ne%t#5ne%t8 t#5ne%t#5pre.<t8 free t#5ne%t"8 :: .oid display " 7 t<h8 while t#5ne%tD<N600" 7 printf =>d@n=-t#5no"8 t<t#5ne%t8 : printf =>d=-t#5no"8 :

OUTPUT$ enter the number of nodes <* enter the element( enter the elements& enter the elements* enter your option+ (.insert ( enter the elements to be inserted+ && enter the position to insert+ ( enter your option * list elements are+ && ( & * enter your option ( enter the elements to be inserted+ (( enter the position to insert+ K enter your option * list elements are+ && ( & * (( enter your option &.delete *.display C.e%it

& enter the position to delete+ ( enter your option * list elements are+ ( & * ((

RESULT$

E8.NO$02
REPRESENT A POL NOMIAL AS A LINKED LIST

DATE$

AND 9RITE FUNCTIONS FOR POL NOMIAL ADDITION

AIM$ To de.elop a program to represent a polynomial as a linked list and write functions for polynomial addition.

ALGORITHM$ S)#51$ !tart. S)#5 2$ Create a 0inked lists used to represent and manipulate polynomials. S)#5:$ / polynomial can be represented as G L" < an%neBan#(%n#(eBBa(%(e a Mhere ai ) non?ero coefficients-'4i4n ei ) e%ponent ()#54$ Each node in the polynomial is considered as a node .Each node contains three fields Node !tructure C4#33'/'#.) E754.#.) L'.A N Coefficient Field # Mhich holds the coefficient of a term N E%ponent Field # Mhich holds the e%ponent .alue of that term N 0ink Field # /ddress of the ne%t term in the polynomial S)#5=$ G and O are two polynomials.G A O can be represented as linked list. S)#56$ G+K%&BE%BP S)#5>$ O+*%*BC%&B% S)#58$ The resultant polynomial can be represented like this. R+*%*BQ%&BP%BP S)#5?$ !top.

SOURCE CODE$ 3include4stdio.h5 3include4malloc.h5 3define N600 ' struct node 7 float coef8 int e%po8 struct node9link8 :8 struct node9polyRadd struct node9-struct node9"8 struct node9enter struct node9"8 struct node9insert struct node9-float-int"8 .oid main " 7 struct node 9p(Rstart-9p&Rstart-9p*Rstart8 p(Rstart<N6008 p&Rstart<N6008 p*Rstart<N6008 printf =polynomial (+@n="8 p(Rstart<enter p(Rstart"8 printf =polynomial &+@n="8 p&Rstart<enter p&Rstart"8

p*Rstart<polyRadd p(Rstart-p&Rstart"8 printf =polynomial ( is +="8 display p(Rstart"8 printf =polynomial & is +="8 display p&Rstart"8 printf =added polynomial is +="8 display p*Rstart"8 : struct node9enter struct node9start" 7 int i-n-e%8 float co8 printf =how many terms you want to enter+="8 scanf =>d=-An"8 for i<(8i4<n8iBB" 7 printf =enter co efficient for term >d+=-i"8 scanf =>f=-Aco"8 printf =enter e%ponent for term >d+=-i"8 scanf =>d=-Ae%"8 start<insert start-co-e%"8 : return start8

: struct node9insert struct node9start-float co-int e%" 7 struct node 9ptr-9tmp8 tmp<malloc si?eof struct node""8 tmp#5coef<co8 tmp#5e%po<e%8 if start<<N600SSe%5start#5e%po" 7 tmp#5link<start8 start<tmp8 : else 7 ptr<start8 while ptr#5linkD<N600AAptr#5link#5e%po5e%" ptr<ptr#5link8 tmp#5link<ptr#5link8 ptr#5link<tmp8 if ptr#5link<<N600" tmp#5link<N6008 : return start8

: struct node9polyRadd struct node9p(-struct node9p&" 7 struct node 9p*Rstart-9p*-9tmp8 p*Rstart<N6008 if p(<<N600AAp&<<N600" return p*Rstart8 while p(D<N600AAp&D<N600" 7 tmp<malloc si?eof struct node""8 if p*Rstart<<N600" 7 p*Rstart<tmp8 p*<p*Rstart8 : else 7 p*#5link<tmp8 p*<p*#5link8 : if p(#5e%po5p&#5e%po" 7 tmp#5coef<p(#5coef8

tmp#5e%po<p(#5e%po8 p(<p(#5link8 : else if p&#5e%po5p(#5e%po" 7 tmp#5coef<p&#5coef8 tmp#5e%po<p&#5e%po8 p&<p&#5link8 : else if p(#5e%po<<p&#5e%po" 7 tmp#5coef<p(#5coefBp&#5coef8 tmp#5e%po<p(#5e%po8 p(<p(#5link8 p&<p&#5link8 :: while p(D<N600" 7 tmp<malloc si?eof struct node""8 tmp#5coef<p(#5coef8 tmp#5e%po<p(#5e%po8

if p*Rstart<<N600" 7 p*Rstart<tmp8 p*<p*Rstart8 : else 7 p*#5link<tmp8 p*<p*#5link8 : p(<p(#5link8 : while p&D<N600" 7 tmp<malloc si?eof struct node""8 tmp#5coef<p&#5coef8 tmp#5e%po<p&#5e%po8 if p*Rstart<<N600" 7 p*Rstart<tmp8 p*<p*Rstart8 : else

7 p*#5link<tmp8 p*<p*#5link8 : p&<p&#5link8 : p*#5link<N6008 return p*Rstart8 :display struct node9ptr" 7 if ptr<<N600" 7 printf =empty @n="8 return8 : while ptrD<N600" 7 printf = >.(f%T>d"B=-ptr#5coef-ptr#5e%po"8 ptr<ptr#5link8 : printf =@b@b@n="8 return '8 :

OUTPUT$ polynomial (+ how many terms you want to enter +* enter coefficint for terms (+K enter e%ponent for term(+& enter coefficint for terms &+* enter e%ponent for term&+( enter coefficint for terms *+( enter e%ponent for term*+' polynomial &+ how many terms you want to enter +* enter coefficint for terms (+* enter e%ponent for term(+* enter coefficint for terms &+C enter e%ponent for term&+& enter coefficint for terms *+C enter e%ponent for term*+(

RESULT$

E8.NO$0: DATE$
IMPLEMENT STACK AND USE IT TO CONVERT INFI8 TO POSTFI8 E8PRESSION

AIM$ To write a program to implement stack and use it to con.ert infid to postfi% e%pression.

ALGORITHM$ S)#51$ !tart. S)#52$ Create a stack to store operand and operator. S)#5:$ In Gostfi% notation the operator follows the two operands and in the infi% notation the operator is in between the two operands. S)#54$ Consider the sum of / and B. /pply the operator IBJ to the operands / and B and write the sum as /BB is INFIL. B /B is GREFIL. /BB is GU!TFIL ()#5=$ ,et an Infi% E%pression as input and e.aluate it by first con.erting it to postfi% and then e.aluating the postfi% e%pression. S)#56$ The e%pressions with in innermost parenthesis must first be con.erted to postfi% so that they can be treated as single operands. In this way successi.ely eliminated until the entire e%pression is con.erted. S)#5>$ The last pair of parentheses to be opened with in a group of parentheses the first e%pression with in that group to be transforming infi% to Grefi%. S)#58$ !top. immediately suggests the use of !tack. Grecedence encloses transformed. This last#in first#out plays an important role in the Garentheses can be

SOURCE CODE$ 3include4stdio.h5 3include4string.h5 3include4math.h5 3define blank 3define tab V@tV 3define W/L K' long int pop "8 long int e.alRpost "8 charinfi% XW/LY-postfi% XW/LY8 long int stack XW/LY8 int top8 main " 7 long int .alue8 char choice <VyV8 while choice<<VyV" 7 top<'8 printf =enter infi% +="8 fflush stdin"8 gets infi%"8

infi%RtoRpostfi% "8 printf =postfi%+>s@h=-postfi%"8 .alue<e.alRpost "8 printf =.alue of e%pression +>d@n=- .alue"8 printf =want to continue y n"+="8 scanf =>c=-Achoice"8 :: infi%RtoRposrfi% " 7 int i<'-p<'-type-precedence-len8 char ne%t8 stackXtopY<V3V8 len<strlen infi%"8 infi%XlenY<V3V8 for i<'8infi%XiYD<V3V8iBB" 7 if DwhiteRspaceXinfi%XiYY" 7 switch infi%XiY" 7 case V V+ push infi%XiY"8 break8

while stackXtopYD<V3V" postfi%XpBBY<pop "8 postfi%XpY<V@oV8 : prec char symbol" 7 case V V+ return '8 case VBV+ case V#V+ raturn (8 case V9V+ case VZV+ case V>V+ return &8 case VTV+ return *8 : : push long int symbol" 7 if top5W/L" 7

printf =stack o.erflow @n="8 e%it ("8

OUTPUT$ enter the postfi% e%pression+ aBb" in.alid char e%pression error+stack is empty... error+stack is full in.alid char e%pression

the tree is created.... inorder tra.ersal+ b preorder tra.ersal+ b postorder tra.ersal+ b

RESULT$

E8.NO$04 DATE$
IMPLEMENT A DOUBLE-ENDED <DE;UEUE2 9HERE INSERTION AND DELETION OPERATIONS ARE POSSIBLE AT BOTH THE ENDS

AIM $ To write a program to implement a double ended 2ueue $EO6E6E" where insertion and deletion operations are possible at both the ends.

ALGORITHM$ S)#51$ !tart. S)#52$ The operations that can be performed on a de2ueue are as follows N Insert at front N Insert at rear N $elete from front N $elete from rear S)#5:$ /fter inserting the element B at front end- the de2ueue will look like this BC$E front rear S)#54$ /fter inserting the element F at rear end-the de2ueue will look like this BC$EF front rear S)#5=$ Me delete an element from front which has to be B. C$EF front rear S)#56$ Me delete an element from rear which has to be F. C$E front rear S)#5>$ !top.

SOURCE CODE$ 3include4stdio.h5 3define W/L K int de2ueRarrXW/LY8 int left<#(8 int right<#(8 main " 7 int choice8 clrscr "8 printf =(.input restricted de2ueue@n="8 printf =&.output restricted de2ueue@n="8 printf =enter your choice+="8 scanf =>d=-Achoice"8 switch choice" 7 case (+ inputR2ue "8 break8 case &+ outputR2ue "8 break8 default+

printf =wrong choice@n="8 :return8 : inputR2ue " 7 int choice8 while (" 7 printf =(.insert at right@n="8 printf =&.delete from left@n="8 printf =*.delete from right@n="8 printf =C.display@n="8 printf =K.2uit@n="8 printf =enter your choice+="8 scanf =>d=-Achoice"8 switch choice" 7 case (+ insertRright "8 break8 case &+ deleteRleft "8 break8

case *+ deleteRright "8 break8 case C+ displayR2ueue "8 break8 case K+ e%it "8 default+ printf =wrong choice="8 : : : outputR2ue " 7 int choice8 while (" 7 printf =(.insert at right@n="8 printf =&.insert at left@n="8 printf =*.delete from left@n="8 printf =C.display@n="8 printf =K.2uit@n="8

printf =enter your choice+="8 scanf =>d=-Achoice"8 switch choice" 7 case (+ insertRright "8 break8 case &+ insertRleft "8 break8 case *+ deleteRleft "8 break8 case C+ displayR2ueue "8 break8 case K+ e%it "8 default+ printf =wrong choice@n="8 : : :

insertRright " 7 int addedRitem8 if left<<'AAright<<W/L#("SS left<<rightB("" 7 printf =2ueue o.erflow@n="8 return8 : if left<<#(" 7 left<'8 right<'8 : else if right<<W/L#(" right<'8 else right<rightB(8 printf =input the element for adding in 2ueue+="8 scanf =>d=-AaddedRitem"8 de2ueRarrXrightY<addedRitem8 return8 :

insertRleft " 7 int addedRitem8 if left<<'AAright<<W/L#("SS left<<rightB("" 7 printf =2ueue o.erflow@n="8 return8 : if left<<#(" 7 left<'8 right<'8 : else if left<<'" left<W/L#(8 else left<left#(8 printf =input the element for adding in 2ueue+="8 scanf =>d=-AaddedRitem"8 return8 : deleteRleft "

7 if left<<#(" 7 printf =2ueue underflow@n="8 return8 : printf =element deleted from 2ueue is +>d @n=-de2ueRarrXleftY"8 if left<<right" 7 left<#(8 right<#(8 : else if left<<W/L#(" left<'8 else left<leftB(8 return8 : deleteRright " 7 if left<<#(" 7

printf =2ueue underflow@n="8 return8 : printf =element deleted from 2ueue is +>d @n=-de2ueRarrXrightY"8 if left<<right" 7 left<#(8 right<#(8 : else if right<<'" right<W/L#(8 else right<right#(8 return8 : displayR2ueue " 7 int frontRpos<left-rearRpos<right8 if left<<#(" 7 printf =2ueue is empty@n="8 return8

: printf =2ueue elements +@n="8 if frontRpos4<rearRpos" 7 while frontRpos4<rearRpos" 7 printf =>d=-de2ueRarrXfrontRposY"8 frontRposBB8 : : else 7 while frontRpos4<W/L#(" 7 printf =>d=-de2ueRarrXfrontRposY"8 frontRposBB8 : frontRpos<'8 while frontRpos4<rearRpos" 7 printf =>d=-de2ueRarrXfrontRposY"8 frontRposBB8 :

: printf =@n="8 return8 :

OUTPUT$ (.insert at right &.delete from left *.delete from right C.display K.2uit enter your choice+( input the element for addid in 2ueue+&Q (.insert at right &.delete from left *.delete from right C.display K.2uit enter your choice+& element deleted from 2ueue is +(' (.insert at right &.delete from left *.delete from right C.display K.2uit enter your choice+C 2ueue elements

&'*'&Q (.insert at right &.delete from left *.delete from right C.display K.2uit enter your choice+* element deleted from 2ueue is +&Q (.insert at right &.delete from left *.delete from right C.display K.2uit enter your choice+C 2ueue elements &'*' (.insert at right &.delete from left *.delete from right C.display K.2uit enter your choice+* element deleted from 2ueue is +*'

(.insert at right &.delete from left *.delete from right C.display K.2uit

RESULT$

E8.NO$0= DATE$
AIM$ To de.elop a program to implement an e%pression tree- that produce its pre#order- in#order and post#order tra.ersals. ALGORITHM$ S)#51+ !tart. S)#52$ Create a binary tree with the nodes partitioned into three dis;oint subsets. The first subset consists of a single element called the root of the tree. The other two subsets are themsel.es binary trees called the left and right sub trees of the original tree./ left or right sub tree can be empty.Each element of a binary tree is called node of the tree. S)#5:$ / common operation in the binary tree is to tra.erse that is to pass three ways they are preorder - in order- post order tra.ersal. S)#54$ To tra.erse a nonempty binary tree preorder we perform the following operations as depth#first order" o [isit the root o Tra.erse the left subtree in preorder o Tra.erse the right subtree in preorder S)#5=$ To tra.erse a nonempty binary tree inorder we perform the following operations as symmetric order" o Tra.erse the left subtree in inorder o [isit the root o Tra.erse the right subtree in inorder S)#56$ To tra.erse a nonempty binary tree postorder we perform the following operations o Tra.erse the left subtree in postorder o Tra.erse the right subtree in postorder o [isit the root S)#5>$ !top. three three three through the tree enumerating each nodes once. The tra.ersal of the binary tree can be done in

IMPLEMENT AN E8PRESSION TREE PRODUCE ITS PRE-ORDER, INORDER, AND POST-ORDER TRAVERSALS

SOURCE CODE$ 3include4stdio.h5 3include4conio.h5 3include4ctype.h5 3include4alloc.h5 3define si?e &' 3define null ' typedef struct node 7 char data8 struct node9left8 struct node9right8 : btree8 btree9stackXsi?eY8 int top8 .oid main " 7 btree9root8 char e%pX\'Y8 btree9create char e%pX\'Y"8 .oid inorder btree9root"8 .oid preorder btree9root"8

.oid postorder btree9root"8 clrscr "8 printf =@n enter the postfi% e%pression+@n="8 scanf =>s=-e%p"8 top<#(8 root<create e%p"8 printf =@n the tree is created...@n="8 printf =@n inorder tra.ersal+@n@n="8 inorder root"8 printf =@n preorder tra.ersal+@n@n="8 preorder root"8 printf =@n postorder tra.ersal+@n@n="8 postorder root"8 getch "8 : btree9create char e%pXY" 7 btree9temp8 int pos8 char ch8 .oid push btree9"8 btree9pop "8 pos<'8

ch<e%pXposY8 while chD<V@'V" 7 temp< btree9"malloc si?eof btree""8 temp#5left<temp#5right<N6008 temp#5data<ch8 if isalpha ch"" push temp"8 else if ch<<VBVSSch<<V#VSSch<<VZV" 7 temp#5right<pop "8 temp#5left<pop "8 push temp"8 : else printf =@n in.alid char e%pression@n="8 posBB8 ch<e%pXposY8 : temp<pop "8 return temp"8 : .oid push btree 9node"

7 if topB(5<si?e" printf =error+stack is full@n="8 topBB8 stackXtopY<node8 : btree9pop " 7 btree9node8 if top<<#(" printf =@n error+stack is empty...@n="8 node<stackXtopY8 top##8 return node"8 : .oid inorder btree9root" 7 btree9temp8 temp<root8 if tempD<N600" 7 inorder temp#5left"8 printf =>c=-temp#5data"8

inorder temp#5right"8 :: .oid preorder btree9root" 7 btree9temp8 temp<root8 if tempD<N600" 7 printf =>c=-temp#5data"8 preorder temp#5left"8 preorder temp#5right"8 : : .oid postorder btree 9root" 7btree 9temp8 temp<root8 if tempD<N600" 7 postorder temp#5left"8 postorder temp#5right"8 printf =>c=-temp#5data"8 : :

OUTPUT$ Enter the postfi% e%pression+ abBcd#9 The tree is created..... Inorder tra.ersal+ aBb9c#d Greorder tra.ersal+ 9Bab#cd Gostorder tra.ersal+ abBcd#9

RESULT$

E8.NO.$06 DATE$
IMPLEMENT BINARY SEARCH TREE

AIM $ To write a program to create binary search tree and perform insertion and search operations on it.

ALGORITHM$ S)#51$ !tart S)#52$ Create a binary tree using linked representation with nodes ha.ing the structure 0EFT$/T/RI,]T. Build the tree in such a way that .alues at each left is less and .alues at each right is greater. S)#5:$ Insert+ ,et the .alue to be inserted. Create a new node and set the data field to L. then set the left and right links to N600. S)#54$ Compare L with root node data in L 4root continue search in left sub tree. If L<root- then display I$uplicate dataJ- if L5root then search in right subtree. Then insert the node in its right position. S)#5=$ $isplay+ $isplay all the data in the tree. S)#56$ !top.

SOURCE CODE$ 3include4stdio.h5 .oid main " 7 int arrX&'Y-start-end-middle-n-i-item8 clrscr "8 printf = how many elements you want to enter in array="8 scanf =>d=-An"8 for i<'8i4n8iBB" 7 printf =enter the element >d+=-iB("8 scanf =>d=-AarrXiY"8 : printf =enter the element to be searched+="8 scanf =>d=-Aitem"8 start<'8 end<n#(8 middle< startBend"Z&8 while itemD<arrXmiddleYAAstart4<end" 7 if item5arrXmiddleY" start<middleB(8

else end<middle#(8 middle< startBend"Z&8 : if item5<arrXmiddleY" printf =>d found at position >d@n=-item-middleB("8 if start5end" printf =>d not found in array @n=-item"8 getch "8 :

OUTPUT$ how many elements you want to enter in the array E enter the elements '+( enter the elements &+&C enter the elements C+* enter the elements E+K enter the element to be searched+& & not found in array

RESULT$

E8.NO$0> DATE$
IMPLEMENT INSERTION IN AVL TREES

AIM$ To de.elop a program to implement the insertion on /[0 Trees.

ALGORITHM$ S)#51$ !tart. S)#52$ $eclare the node with leftlink- rightlink- data and height of node. S)#5:$ Enter the number of elements to be inserted. S)#54$ Mhile inserting each element the height of each node will be checked. S)#5=$ If the height difference of left and right node is e2ual to & for an node then the height is unbalanced at the node. S)#56$ The present node while inserting a new node at left sub tree then perform rotation with left child otherwise rotation with right child. S)#5>$ ]eight is unbalanced at ,rand Garent node while inserting a new node at right subtree of parent node then perform double rotation with left. S)#58$ ]eight is unbalanced at grandparent node while inserting a new node then perform double rotation with right. S)#5?$ To .iew the tree perform tra.ersal on tree. S)#510$ !top.

SOURCE CODE$ 3include4conio.h5 3include4stdio.h5 typedef enum7F/0!E-TR6E:bool8 struct node 7 int info8 int balance8 struct node9lchild8 struct node9rchild8 : 9root8 struct node9search struct node9ptr-int info" 7 if ptrD<N600" if info4ptr#5info" ptr<search ptr#5lchild-info"8 else if info5ptr#5info" ptr<search ptr#5rchild-info"8 return ptr"8 : struct node9insert int info-struct node9ptr-int 9htRinc" 7

struct node9aptr8 struct node9bptr8 if ptr<<N600" 7 ptr< struct node9"malloc si?eof struct node""8 ptr#5info<info8 ptr#5lchild<N6008 ptr#5rchild<N6008 ptr#5balance<'8 9htRinc<TR6E8 return ptr"8 : if info4ptr#5info" 7 ptr#5lchild<insert info-ptr#5lchild-htRinc"8 if 9htRinc<<TR6E" 7 switch ptr#5balance" 7 case #(+ ptr#5balance<'8 9htRinc<F/0!E8 break8

case '+ ptr#5balance<(8 break8 case (+ aptr<ptr#5lchild8 if aptr#5balance<<(" 7 printf =left to left rotation@n="8 ptr#5lchild<aptr#5rchild8 aptr#5rchild<ptr8 ptr#5balance<'8 aptr#5balance<'8 ptr<aptr8 : else 7 printf =left to right rotation @n="8 bptr<aptr#5rchild8 aptr#5rchild<bptr#5lchild8 bptr#5lchild<aptr8 ptr#5lchild<bptr#5rchild8 bptr#5rchild<ptr8 if bptr#5balance<<("

ptr#5balance<#(8 else ptr#5balance<'8 if bptr#5balance<<#(" aptr#5balance<(8 else aptr#5balance<'8 bptr#5balance<'8 ptr<bptr8 : 9htRinc<F/0!E8 : : : if info5ptr#5info" 7 ptr#5rchild<insert info-ptr#5rchild-htRinc"8 if 9htRinc<<TR6E" 7 switch ptr#5balance" 7 case (+ ptr#5balance<'8

9htRinc<F/0!E8 break8 case '+ ptr#5balance<#(8 break8 case #(+ aptr<ptr#5rchild8 if aptr#5balance<<#(" 7 printf =right to right rotation @n="8 ptr#5rchild<aptr#5lchild8 aptr#5lchild<ptr8 ptr#5balance<'8 aptr#5balance<'8 ptr<aptr8 : else 7 printf =right to left rotation @n="8 bptr<aptr#5lchild8 aptr#5lchild<bptr#5rchild8 bptr#5rchild<aptr8 ptr#5rchild<bptr#5lchild8

bptr#5lchild<ptr8 if bptr#5balance<<#(" ptr#5balance<(8 else ptr#5balance<'8 if bptr#5balance<<(" aptr#5balance<#(8 else aptr#5balance<'8 bptr#5balance<'8 ptr<bptr8 : 9htRinc<F/0!E8 ::: return ptr"8 : main " 7 bool htRinc8 int info8 int choice8 clrscr "8 root< struct node9"malloc si?eof struct node""8

root<N6008 printf =(.insert@n &.display@n *.e%it@n="8 while (" 7 printf =enter your choice+="8 scanf =>d=-Achoice"8 switch choice" 7 case (+ printf =enter the .alue to be inserted++="8 scanf =>d=-Ainfo"8 if search root-info"<<N600" root<insert info-root-AhtRinc"8 else printf =duplicate .alue ignored@n="8 break8 case &+ if root<<N600" 7 printf =tree is empty="8 continue8 : printf =tree is @n="8

display root-("8 printf =@n@n="8 printf =inorder tra.ersals++="8 inorder root"8 printf =@n="8 break8 default+ printf =in.alid choiceDDD="8 e%it '"8 ::: display struct node9ptr-int le.el" 7 int i8 if ptrD<N600" 7 display ptr#5rchild-le.el B("8 printf =@n="8 for i<'8i4le.el8iBB" printf = ="8 printf =>d=-ptr#5info"8 display ptr#5lchild-le.el B("8 :return '8 :

inorder struct node9ptr" 7 if ptrD<N600" 7 inorder ptr#5lchild"8 printf =>d=-ptr#5info"8 inorder ptr#5rchild"8 return '8 ::

OUTPUT$ (.insert &.display *.e%it enter your choice+ ( enter the .alue to be inserted++(K enter your choice+( enter the .alue to be inserted++(& enter your choice+( enter the .alue to be inserted++&C enter your choice+( enter the .alue to be inserted++E enter your choice+& tree is &C (K (& E inorder tra.ersal++E(&(K&C enter your choice+*

RESULT$

E8.NO$08 DATE$
IMPLEMENT PRIORITY QUEUE USING BINARY HEAPS

AIM$ To de.elop a program to implement the priority 2ueue using Binary ]eaps.

ALGORITHM$ ()#51$ !tart. S)#52$ $efinition+ /n abstract data type to efficiently support finding the item with the highest priority across a series of operations. The basic operations are+ insert- find# minimum or ma%imum"and delete# minimum or ma%imum". !ome implementations also efficiently support ;oin two priority 2ueues meld"- delete an arbitrary item- and increase the priority of a item decrease#key". with a%iomatic semantics as S)#5:$ Formal $efinition+ The operations new "- insert .- GO"- find# minimum or min GO"and delete#minimum or dm GO" may be defined follows. S)#54$ new " returns a priority 2ueue S)#5=$ min insert .- new """ < . S)#56$ dm insert .- new """ < new " S)#5>$ min insert .- insert w- GO""" < if priority ." 4 priority min insert w- GO""" then . else min insert w- GO"" ()#58$ dm insert .- insert w- GO""" < if priority ." 4 priority min insert w- GO""" then insert w- GO" else insert .- dm insert w- GO""" where GO is a priority 2ueue- . and w are items- and priority ." is the priority ()#5?$ !top. of item ..

SOURCE CODE$ 3include4stdio.h5 3include4stdlib.h5 3include4conio.h5 3include4malloc.h5 typedef struct heapstruct9p2ueue8 struct heapstruct 7 int capacity8 int si?e8 int 9elements8 :8 .oid insert int-p2ueue"8 p2ueue initiali?e int"8 int deletemin p2ueue"8 int isfull p2ueue"8 int isempty p2ueue"8 .oid display p2ueue"8 .oid main " 7 p2ueue heap8 int i-ma%-ele-ch-t8

clrscr "8 printf =@n enter the ma%imum no. of elements in the priority 2ueue+="8 scanf =>d=-Ama%"8 heap<initiali?e ma%"8 do 7 printf =@n WEN6@n="8 printf =@n (.insertion@n="8 printf =@n &.deletion@n="8 printf =@n *.display@n="8 printf =@n C.e%it@n="8 printf =@n enter your choice+="8 scanf =>d=-Ach"8 switch ch" 7 case (+ printf =@n enter the element to be inserted+="8 scanf =>d=-Aele"8 insert ele-heap"8 printf =@n the element is inserted="8 break8 case &+ t<deletemin heap"8

printf =@n the minimum element >d is deleted@n=-t"8 break8 case *+ printf =@n the elements in the ]E/G are+="8 display heap"8 break8 case C+ e%it '"8 break8 : : while ch4C"8 getch "8 : p2ueue initiali?e int ma%" 7 p2ueue h8 if ma%4*" 7 printf =@n priority 2ueue si?e is too small@n="8 e%it '"8 : h< p2ueue"malloc si?eof struct heapstruct""8

if h<<N600" e%it '"8 h#5capacity<ma%8 h#5si?e<'8 return h8 : .oid insert int %-p2ueue h" 7 int i8 if isfull h"" 7 printf =@n prioroty 2ueue is full="8 return8 : if h#5si?e<<'" 7 h#5elementsX(Y<%8 h#5si?eBB8 : else 7 for i<BBh#5si?e8h#5elementsXiZ&Y5%8iZ<&" h#5elementsXiY<h#5elementsXiZ&Y8

h#5elementsXiY<%8 : : int deletemin p2ueue h" 7 int i-child-minelement-lastelement8 if isempty h"" 7 printf =@n priority 2ueue is empty="8 e%it '"8 : minelement<h#5elementsXiY8 lastelement<h#5elementsXh#5si?e##Y8 for i<(8i9&4<h#5si?e8i<child" 7 child<i9&8 if childD<h#5si?eAAh#5elementsXchildB(Y4h#5elementsXchildY" childBB8 if lastelement5h#5elementsXchildY" h#5elementsXiY<h#5elementsXchildY8 else break8 :

h#5elementsXiY<lastelement8 return minelement8 : .oid display p2ueue h" 7 int i8 for i<(8i4<h#5si?e8iBB" printf =@n >d=-h#5elementsXiY"8 : int isfull p2ueue h" 7 if h#5si?e<<h#5capacity" return (8 else return '8 : int isempty p2ueue h" 7 if h#5si?e<<'" return (8 else getch "8 return '8

OUTPUT$

enter the elements to be inserted+&C the element is inserted WEN6 (.insertion &.delete min *.display C.e%it enter your choice+( enter the elements to be inserted+*K the element is inserted WEN6 (.insertion &.delete min *.display C.e%it enter your choice+* the element in the ]E/G are+ #((* &C *K WEN6 (.insertion &.delete min *.display C.e%it enter your choice+&

RESULT$

E8.NO$0?
RESULT$

IMPLEMENT HASHING WITH OPEN

ADDRESSING DATE$

AIM$ To de.elop a program to implement ]ashing with open addressing. ALGORITHM$ Upen addressing hashing works on the same principle as other types of hashing8 that is- it uses a hashing function h and the key of a datum % to map % to hXkey %"Y- and the .alues are s tored in a table. The difference is in what is stored in the table. Instead of maintaining a pointer to a linked list- the table contains the actual .alues of key %".Implementation The table is TX'..m#(Y where m is the table si?e- and the input is %R(-...-%Rn. The has function h is gi.en. INSERT$ Me apply our hash function to the first .alue+ the last digit is Q- so (Q goes in slot Q. Ne%t is **+ this goes in slot *. Now we come to a problem+ the ne%t number- C* should also go into slot *. Me ha.e no linked lists- so where does it go^ In open addressing- a .alue that is slated for an occupied slot goes into the ne%t empty slot. C* then goes into slot C. K*+ slot * is filled- go to slot C. !lot C is filled- go to slot K. !lot K is empty- so K* goes into slot K.The table is usually made circular- so that a .alue meant to be inserted into the last slot which is occupied" is sent around to the front of the list. !o if we were to insert QQ into the table- we see that slot Q is occupied- and QQ is sent to slot '- which is empty. Note that if we ha.e more .alues entered than slots- we can run out of room in the table- and be unable to make any more insertions. For this reason- the load factor a in open addressing can ne.er e%ceed (.' . DELETE$ !ay we want to delete C*. First we go to slot *+ ItVs occupied- but not by C*. !o we go on to the ne%t occupied slot. Me continue to do this until we find either the number weVre looking for success"- an empty slot or we arri.e back where we started. In this case- the .ery ne%t slot is occupied by C*- so we remo.e it and mark the why in a moment." slot as deleted. MeVll see

SOURCE CODE$ 3include4stdio.h5 3include4conio.h5 3include4stdlib.h5 3define W/L (' .oid main " 7 int aXW/LY-num-key-i8 char ans8 int create int"8 .oid linearRprob intXY-int-int"-display intXY" 8 clrscr "8 printf =@n collision handling by linear probling="8 for i<'8i4W/L8iBB" aXiY<#(8 do 7 printf =@n enter the number="8 scanf =>d=-Anum"8 key<create num"8 linearRprob a-key-num"8

printf =@n do u wish to continue^ _ZN="8 ans<getch "8 : while ans<<VyV"8 display a"8 getch "8 : int create int num" 7 int key8 key<num>('8 return key8 : .oid linearRprob int aXW/LY-int num-int key" 7 int flag-i-count<'8 .oid display int aXY"8 flag<'8 if aXkeyY<<#(" aXkeyY<num8 else 7 i<'8

while i4W/L" 7 if aXiYD<#(" countBB8 iBB8 : if count<<W/L" 7 printf =@n@n hash table is Fu88="8 display a"8 getch "8 e%it ("8 : for i<keyB(8i4W/L8iBB" if aXiY<<#(" 7 aXiY<num8 flag<(8 break8 : for i<'8i4keyAAflag<<'8iBB" if aXiY<<#(" 7

aXiY<num8 flag<(8 break8 : : : .oid display int aXW/LY" 7 int i8 printf =@n@n the hash table is......@n="8 for i<'8i4W/L8iBB" printf =@n >d>d=-i-aXiY"8 :

OUTPUT$

collision handling by linear probling enter the number(*( do u wish to continue^ _ZN enter the number &( do u wish to continue^ _ZN enter the number * do u wish to continue^ _ZN the hash table is...... '( (( &#( ** C#( K#( E#( P#( \#( Q#(

RESULT$

E8.NO$10
IMPLEMENT PRIM'S ALGORITHM USING PRIORITY QUEUES DATE$ GRAPH TO FIND MST OF AN UNDIRECTED

AIM$ To de.elop a program to implement the GrimHs /lgorithm using Griority Oueues to find W!T of an 6ndirected ,raph.

ALGORITHM$ GrimVs algorithm is an algorithm in graph theory that finds a minimum spanning tree for a connected weighted graph. This means it finds a subset of the edges that forms a tree that includes e.ery .erte%- where the total weight of all the edges in the tree is minimi?ed. The algorithm was de.eloped in (Q*' by C?ech mathematician [o;t`ch aarnbk and later independently by computer scientist Robert C. Grim in (QKP and redisco.ered by Edsger $i;kstra in (QKQ. Therefore it is sometimes called the $aG algorithm- the aarnbk algorithm- or the Grim#aarnbk algorithm. The algorithm continuously increases the si?e of a tree starting with a single .erte% until it spans all the .ertices. ()#51$ I.5+)$ / connected weighted graph with .ertices [ and edges E. ()#52$ I.')'!6'B#$ new < 7%:- where % is an arbitrary node starting point" from [- Enew < 7: S)#5:$ R#5#!) +.)'6 V.#C D V$Choose edge u-." with minimal weight such that u is in [new and . is not if there are multiple edges with the same weight- choose arbitrarily but consistently" /dd . to [new- add u- ." to Enew. S)#54$ O+)5+)$ [new and Enew describe a minimal spanning tree

SOURCE CODE$ 3include4stdio.h5 3define W/L (' 3define TEWG (' 3define GERW ( 3define F/0!E ' 3define TR6E ( 3define infinity QQQQ struct node 7 int predecessor8 int dist8 int status8 :8 clrscr "8 struct edge 7 int u8 int .8 :8 int ad;XW/LYXW/LY8

int n8 main " 7 int i-;8 int pathXW/LY8 int wtRtree-count8 struct edge treeXW/LY8 createRgraph "8 printf =ad;ency matri% is+@n="8 display "8 count<maketree tree-AwtRtree" 8 printf =weight of spanning tree is+>d@n=-wtRtree"8 printf =edges to be included in spanning tree are+@n="8 for i<(8i4<count8iBB" 7 printf =>d#5=-treeXiY.u"8 printf =>d@n=-treeXiY.."8 : return '8 : createRgraph " 7 int i-ma%Redge-origin-destin-wt8

printf =enter number of .ertices+="8 scanf =>d=-An"8 ma%Redge<n9 n#("Z&8 for i<(8i4<ma%Redge8iBB" 7 printf =enter edges > ' ' to 2uit"+=-i" 8 scanf =>d=-Aorigin-Adestin"8 if origin<<'"AA destin<<'"" break8 printf =enter weight for this edge+="8 scanf =>d=-Awt"8 if origin5nSSdestin5nSSorigin4<'SSdestin4<'" 7 printf =in.alid edgeD@n="8 i##8 : else 7 ad;XoriginYXdestinY<wt8 ad;XdestinYXoriginY<wt8 : : if i4n#("

7 printf =spannong tree is not possiable@n="8 e%it ("8 : return '8 : display " 7 int i-;8 for i<(8i4<n8iBB" 7 for ;<(8;4<n8;BB" printf =>*d=-ad;XiYX;Y"8 printf =@n="8 : return '8 : int maketree struct edge treeXW/LY-int 9weight" 7 struct node stateXW/LY8 int i-min-k-count-current-newdist8 int m8 int u(-.(8

9weight<'8 for i<(8i4<n8iBB" 7 stateXiY.predecessor<'8 stateXiY.dist<infinity8 stateXiY.status<TEWG8 : stateX(Y.predecessor<'8 stateX(Y.dist<'8 stateX(Y.status<GERW8 current<(8 count<'8 while allRperm state"D<TR6E" 7 for i<(8i4n8iBB" 7 if ad;XcurrentYXiY5'AAstateXiY.status<<TEWG" 7 if ad;XcurrentYXiY4stateXiY.dist" 7 stateXiY.predecessor<current8 stateXiY.dist<ad;XcurrentYXiY8 :

: : min<infinity8 for i<(8i4<n8iBB" 7 if stateXiY.status<<TEWGAAstateXiY.dist4min" 7 min<stateXiY.dist8 current<i8 : : stateXcurrentY.status<GERW8 u(<stateXcurrentY.predecessor8 .(<current8 countBB8 treeXcountY.u<u(8 treeXcountY..<.(8 : return count"8 : int allRperm struct node stateXW/LY" 7 int i8

for i<(8i4<n8iBB" if stateXiY.status<<TEWG" return F/0!E8 return TR6E8 :

OUTPUT$ enter number of .ertices+K enter edges > ' ' to 2uit"+'( enter weight for this edge+(' in.alid edgeD enter edges > ' ' to 2uit"+(& enter weight for this edge+(' in.alid edgeD enter edges > ' ' to 2uit"+&* enter weight for this edge+( in.alid edgeD enter edges > ' ' to 2uit"+&C enter weight for this edge+E in.alid edgeD enter edges > ' ' to 2uit"+*C enter weight for this edge+ & in.alid edgeD enter edges > ' ' to 2uit"+ (& enter weight for this edge+&

in.alid edgeD enter edges > ' ' to 2uit"+

RESULT$

You might also like