void main() # int data,ch,choice$KyK& node "root$;U::& printf(35n@asic operations in an <G: Free...3)& printf(35n2.Insert a node in the <G: Free3)& printf(35nB.Delete a node in the <G: Free3)& printf(35nC.Gie. the <G: Free3)& printf(35nD.4Eit3)& .hile((choice$$KyK)++(choice$$KLK)) # printf(35n3)& Mush(stdin)& scanf(3%d3,9ch)& s.itch(ch) # case 28 printf(35n4nter the value to 0e inserted83)& scanf(3%d3,9data)& if(searchnode(root,data)$$;U::) root$insertnode(data,root)& else printf(35nData already eEists3)& 0rea!& case B8 printf(34nter the value to 0e deleted83)& scanf(3%d3,9data)& if(searchnode(root,data)/$;U::) deletenode(data,9root)& else printf(35n4lement to 0e deleted is not found3)& 0rea!& case C8 if(root$$;U::) # printf(35n<G: Free is 4moty5n3)& continue& - printf(35nInorder Fraversal of the <G: Free83)& inorder(root)& 0rea!& default8 printf(35n4nd of run of your program...3)& releasenode(root)& return& - - - OUTPUT: @asic operations in an <G: Free... 2.Insert a node in the <G: Free B.Delete a node in the <G: Free C.Gie. the <G: Free D.4Eit 2 4nter the value to 0e inserted8BC 2 4nter the value to 0e inserted826 C Inorder Fraversal of the <G: Free8 26 BC 4nter the value to 0e inserted82C :eft to left <G: =otation C Inorder Fraversal of the <G: Free8 2C 26 BC 4nter the value to 0e inserted8BN 2 4nter the value to 0e inserted8B( 2 4nter the value to 0e inserted82O =ight to :eft <G: =otation 4nter the value to 0e inserted8CB =ight to =ight <G: =otation C Inorder Fraversal of the <G: Free8 2C 26 2O B( BC BN CB 4nter the value to 0e inserted8P 2 4nter the value to 0e inserted82B :eft to =ight <G: =otation C Inorder Fraversal of the <G: Free8 P 2B 2C 26 2O B( BC BN CB AIM: Fo perform various operations i.e., insertions and deletions on B1C trees. PROGRAM: J">rogram of insertion and deletion in @ tree"J #include<stdio.h #include <conio.h #deHne M 6 struct node# int n& J" n < M ;o. of !eys in node .ill al.ays less than order of @ tree "J int !eys)M12*& J"array of !eys"J struct node "p)M*& J" (n,2 pointers .ill 0e in use) "J -"root$;U::& enum QeyRtatus # Duplicate,RearchSailure,Ruccess,InsertIt,:essQeys -& void insert(int !ey)& void display(struct node "root,int)& void Del;ode(int E)& void search(int E)& enum QeyRtatus ins(struct node "r, int E, int" y, struct node "" u)& int search>os(int E,int "!eyTarr, int n)& enum QeyRtatus del(struct node "r, int E)& int main() # int !ey& int choice& printf(3Ireation of @ tree for node %d5n3,M)& .hile(2) # printf(32.Insert5n3)& printf(3B.Delete5n3)& printf(3C.Rearch5n3)& printf(3D.Display5n3)& printf(36.Uuit5n3)& printf(34nter your choice 8 3)& scanf(3%d3,9choice)& s.itch(choice) # case 28 printf(34nter the !ey 8 3)& scanf(3%d3,9!ey)& insert(!ey)& 0rea!& case B8 printf(34nter the !ey 8 3)& scanf(3%d3,9!ey)& Del;ode(!ey)& 0rea!& case C8 printf(34nter the !ey 8 3)& scanf(3%d3,9!ey)& search(!ey)& 0rea!& case D8 printf(3@tree is 85n3)& display(root,()& 0rea!& case 68 eEit(2)& default8 printf(3Vrong choice5n3)& 0rea!& -J"4nd of s.itch"J -J"4nd of .hile"J JJreturn (& -J"4nd of main()"J void insert(int !ey) # struct node "ne.node& int upQey& enum QeyRtatus value& value $ ins(root, !ey, 9upQey, 9ne.node)& if (value $$ Duplicate) printf(3Qey already availa0le5n3)& if (value $$ InsertIt) # struct node " uproot $ root& root$(struct node ")malloc(si7eof(struct node))& root1n $ 2& root1!eys)(* $ upQey& root1p)(* $ uproot& root1p)2* $ ne.node& -J"4nd of if "J -J"4nd of insert()"J enum QeyRtatus ins(struct node "ptr, int !ey, int "upQey,struct node ""ne.node) # struct node "ne.>tr, "last>tr& int pos, i, n,split>os& int ne.Qey, lastQey& enum QeyRtatus value& if (ptr $$ ;U::) # "ne.node $ ;U::& "upQey $ !ey& return InsertIt& - n $ ptr1n& pos $ search>os(!ey, ptr1!eys, n)& if (pos < n 99 !ey $$ ptr1!eys)pos*) return Duplicate& value $ ins(ptr1p)pos*, !ey, 9ne.Qey, 9ne.>tr)& if (value /$ InsertIt) return value& J"If !eys in node is less than M12 .here M is order of @ tree"J if (n < M 1 2) # pos $ search>os(ne.Qey, ptr1!eys, n)& J"Rhifting the !ey and pointer right for inserting the ne. !ey"J for (i$n& ipos& i11) # ptr1!eys)i* $ ptr1!eys)i12*& ptr1p)i,2* $ ptr1p)i*& - J"Qey is inserted at eEact location"J ptr1!eys)pos* $ ne.Qey& ptr1p)pos,2* $ ne.>tr& ,,ptr1n& J"incrementing the num0er of !eys in node"J return Ruccess& -J"4nd of if "J J"If !eys in nodes are maEimum and position of node to 0e inserted is last"J if (pos $$ M 1 2) # lastQey $ ne.Qey& last>tr $ ne.>tr& - else J"If !eys in node are maEimum and position of node to 0e inserted is not last"J # lastQey $ ptr1!eys)M1B*& last>tr $ ptr1p)M12*& for (i$M1B& ipos& i11) # ptr1!eys)i* $ ptr1!eys)i12*& ptr1p)i,2* $ ptr1p)i*& - ptr1!eys)pos* $ ne.Qey& ptr1p)pos,2* $ ne.>tr& - split>os $ (M 1 2)JB& ("upQey) $ ptr1!eys)split>os*& ("ne.node)$(struct node ")malloc(si7eof(struct node))&J"=ight node after split"J ptr1n $ split>os& J";o. of !eys for left splitted node"J ("ne.node)1n $ M121split>os&J";o. of !eys for right splitted node"J for (i$(& i < ("ne.node)1n& i,,) # ("ne.node)1p)i* $ ptr1p)i , split>os , 2*& if(i < ("ne.node)1n 1 2) ("ne.node)1!eys)i* $ ptr1!eys)i , split>os , 2*& else ("ne.node)1!eys)i* $ lastQey& - ("ne.node)1p)("ne.node)1n* $ last>tr& return InsertIt& -J"4nd of ins()"J void display(struct node "ptr, int 0lan!s) # if (ptr) # int i& for(i$2&i<$0lan!s&i,,) printf(3 3)& for (i$(& i < ptr1n& i,,) printf(3%d 3,ptr1!eys)i*)& printf(35n3)& for (i$(& i <$ ptr1n& i,,) display(ptr1p)i*, 0lan!s,2()& -J"4nd of if"J -J"4nd of display()"J void search(int !ey) # int pos, i, n& struct node "ptr $ root& printf(3Rearch path85n3)& .hile (ptr) # n $ ptr1n& for (i$(& i < ptr1n& i,,) printf(3 %d3,ptr1!eys)i*)& printf(35n3)& pos $ search>os(!ey, ptr1!eys, n)& if (pos < n 99 !ey $$ ptr1!eys)pos*) # printf(3Qey %d found in position %d of last dispalyednode5n3,!ey,i)& return& - ptr $ ptr1p)pos*& - printf(3Qey %d is not availa0le5n3,!ey)& -J"4nd of search()"J int search>os(int !ey, int "!eyTarr, int n) # int pos$(& .hile (pos < n 99 !ey !eyTarr)pos*) pos,,& return pos& -J"4nd of search>os()"J void Del;ode(int !ey) # struct node "uproot& enum QeyRtatus value& value $ del(root,!ey)& s.itch (value) # case RearchSailure8 printf(3Qey %d is not availa0le5n3,!ey)& 0rea!& case :essQeys8 uproot $ root& root $ root1p)(*& free(uproot)& 0rea!& -J"4nd of s.itch"J -J"4nd of delnode()"J enum QeyRtatus del(struct node "ptr, int !ey) # int pos, i, pivot, n ,min& int "!eyTarr& enum QeyRtatus value& struct node ""p,"lptr,"rptr& if (ptr $$ ;U::) return RearchSailure& J"<ssigns values of node"J n$ptr1n& !eyTarr $ ptr1!eys& p $ ptr1p& min $ (M 1 2)JB&J"Minimum num0er of !eys"J pos $ search>os(!ey, !eyTarr, n)& if (p)(* $$ ;U::) # if (pos $$ n ++ !ey < !eyTarr)pos*) return RearchSailure& J"Rhift !eys and pointers left"J for (i$pos,2& i < n& i,,) # !eyTarr)i12* $ !eyTarr)i*& p)i* $ p)i,2*& - return 11ptr1n $ (ptr$$root W 2 8 min) W Ruccess 8 :essQeys& -J"4nd of if "J if (pos < n 99 !ey $$ !eyTarr)pos*) # struct node "Xp $ p)pos*, "Xp2& int n!ey& .hile(2) # n!ey $ Xp1n& Xp2 $ Xp1p)n!ey*& if (Xp2 $$ ;U::) 0rea!& Xp $ Xp2& -J"4nd of .hile"J !eyTarr)pos* $ Xp1!eys)n!ey12*& Xp1!eys)n!ey 1 2* $ !ey& -J"4nd of if "J value $ del(p)pos*, !ey)& if (value /$ :essQeys) return value& if (pos ( 99 p)pos12*1n min) # pivot $ pos 1 2& J"pivot for left and right node"J lptr $ p)pivot*& rptr $ p)pos*& J"<ssigns values for right node"J rptr1p)rptr1n , 2* $ rptr1p)rptr1n*& for (i$rptr1n& i(& i11) # rptr1!eys)i* $ rptr1!eys)i12*& rptr1p)i* $ rptr1p)i12*& - rptr1n,,& rptr1!eys)(* $ !eyTarr)pivot*& rptr1p)(* $ lptr1p)lptr1n*& !eyTarr)pivot* $ lptr1!eys)11lptr1n*& return Ruccess& -J"4nd of if "J if (pos min) # pivot $ pos& J"pivot for left and right node"J lptr $ p)pivot*& rptr $ p)pivot,2*& J"<ssigns values for left node"J lptr1!eys)lptr1n* $ !eyTarr)pivot*& lptr1p)lptr1n , 2* $ rptr1p)(*& !eyTarr)pivot* $ rptr1!eys)(*& lptr1n,,& rptr1n11& for (i$(& i < rptr1n& i,,) # rptr1!eys)i* $ rptr1!eys)i,2*& rptr1p)i* $ rptr1p)i,2*& -J"4nd of for"J rptr1p)rptr1n* $ rptr1p)rptr1n , 2*& return Ruccess& -J"4nd of if "J if(pos $$ n) pivot $ pos12& else pivot $ pos& lptr $ p)pivot*& rptr $ p)pivot,2*& J"merge right node .ith left node"J lptr1!eys)lptr1n* $ !eyTarr)pivot*& lptr1p)lptr1n , 2* $ rptr1p)(*& for (i$(& i < rptr1n& i,,) # lptr1!eys)lptr1n , 2 , i* $ rptr1!eys)i*& lptr1p)lptr1n , B , i* $ rptr1p)i,2*& - lptr1n $ lptr1n , rptr1n ,2& free(rptr)& J"=emove right node"J for (i$pos,2& i < n& i,,) # !eyTarr)i12* $ !eyTarr)i*& p)i* $ p)i,2*& - return 11ptr1n $ (ptr $$ root W 2 8 min) W Ruccess 8 :essQeys& -J"4nd of del()"J OUTPUT: Ireation of @ tree for node 6 2.Insert B.Delete C.Rearch D.Display 6.Uuit 4nter your choice 8 2 4nter the !ey 8 2( 2.Insert B.Delete C.Rearch D.Display 6.Uuit 4nter your choice 8 2 4nter the !ey 8 B( 2.Insert B.Delete C.Rearch D.Display 6.Uuit 4nter your choice 8 D @tree is 8 2( B( 2.Insert B.Delete C.Rearch D.Display 6.Uuit 4nter your choice 8 B 4nter the !ey 8 2( 2.Insert B.Delete C.Rearch D.Display 6.Uuit 4nter your choice 8 D @tree is 8 B( 2.Insert B.Delete C.Rearch D.Display 6.Uuit 4nter your choice 86 AIM: Fo implement operations on 0inary heap PROGRAM: #include<stdio.h #include<conio.h #include<process.h #deHne arraylength B( int heapsi7e$(,heap)arraylength*& void enXueue(int element) # int currentnode& if(heapsi7e$$arraylength12) # printf(3<rray is full3)& return& - currentnode$,,heapsi7e& .hile(currentnode/$(99heap)currentnodeJB*<element) # heap)currentnode*$heap)currentnodeJB*& currentnodeJ$B& - heap)currentnode*$element& - void deXueue() # int lastelement,currentnode,child& if(heapsi7e$$() # printf(3>riority Xueue is empty3)& return& - heap)(*$(& lastelement$heap)heapsi7e11*& currentnode$2& child$B& .hile(child<$heapsi7e) # if(child<heapsi7e99heap)child*<heap)child,2*) child,,& if(lastelement$heap)child*) 0rea!& heap)currentnode*$heap)child*& currentnode$child& child"$B& - heap)currentnode*$lastelement& - void display() # int i& for(i$(&i<arraylength&i,,) # printf(3%6d3,heap)i*)& - - void main() # int opt,ele& .hile(2) # clrscr()& printf(35n.............3)& printf(35n2.4nXueue3)& printf(35nB.DeXueue3)& printf(35nC.Display3)& printf(35nD.4Eit3)& printf(35n............3)& printf(35n4nter ur option83)& scanf(3%d3,9opt)& s.itch(opt) # case 28 printf(34nter the element to insert83)& scanf(3%d3,9ele)& enXueue(ele)& 0rea!& case B8 deXueue()& printf(34lement is deleted3)& 0rea!& case C8 display()& 0rea!& case D8 eEit(()& - getch()& - - OUTPUT:
............. 2.4nXueue B.DeXueue C.Display D.4Eit ............ 4nter ur option82 4nter the element to insert8 2( ............. 2.4nXueue B.DeXueue C.Display D.4Eit ............ 4nter ur option82 4nter the element to insert8 6 ............. 2.4nXueue B.DeXueue C.Display D.4Eit ............ 4nter ur option82 4nter the element to insert8 2O ............. 2.4nXueue B.DeXueue C.Display D.4Eit ............ 4nter ur option82 4nter the element to insert8 BD ............. 2.4nXueue B.DeXueue C.Display D.4Eit ............ 4nter ur option8C BD 2O 2( 6 ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( AIM: Fo implement operations on graphs i) verteE insertion ii) GerteE deletion iii) Hnding verteE iv)4dge addition and deletion PROGRAM: J">rogram to create,insert,delete and vie. the ad'ecency matriE"J #include<stdio.h #include<conio.h #deHne GRIY4 B( int chec!Vt()& int chec!Dir()& void insertGerteE ()& void deleteGerteE(int vDel)& void insert4dge(int vRtart,int v4nd)& void delete4dge(int vRtart, int v4nd)& void createAraph()& void vie.Araph()& void displayTmenu()& int nGerteE,ad'Mat)GRIY4*)GRIY4*& void main() # char choice$KyK& int ch,vs,ve,vd& clrscr()& displayTmenu()& .hile((choice$$KyK)++(choice$$KyK)) # printf(35nW3)& Mush(stdin)& scanf(3%d3,9ch)& s.itch(ch) # case ( 8displayTmenu()& 0rea!& case 2 8createAraph()& 0rea!& case B 8insertGerteE()& 0rea!& case C 8printf(35n enter the starting 9 ending verteE to insert an edge 83)& scanf(3%d %d3,9vs,9ve)& insert4dge(vs,ve)& 0rea!& case D 8printf(35n enter the verteE to delete 83)& scanf(3%d3,9vd)& deleteGerteE(vd)& 0rea!& case 6 8printf(35n enter the starting 9 ending verteE to delete an edge 83)& scanf(3%d%d3,9vs,9ve)& 0rea!& case Z 8vie.Araph()& 0rea!& case N 8printf(35n end of run of your program...........3)& eEit(()& - - - void insertGerteE() # int rc& nGerteE,,& for(rc$(&rc<nGerteE&rc,,) ad'Mat)rc* )nGerteE12*$ad'Mat)nGerteE12* )rc*$(& - void insert4dge(int vRtart,int v4nd) # int ie& if(vRtartnGerteE++vRtart<2++v4ndnGerteE++v4nd<2) return& printf(3enter .eight of the 4dge from v%d to v%d 83, vRtart ,v4nd)& scanf(3%d3,9ad'Mat )vRtart12* )v4nd12*)& - void deleteGerteE(int vDel) # int r,c& if(vDelnGerteE ++ vDel<2) return& for(r$vDel12&r<nGerteE& r,,) for(c$(&c<nGerteE&c,,) ad'Mat)r*)c*$ad'Mat)r,2*)c*& for(c$vDel12&r<nGerteE&c,,) for(r$(&r<nGerteE&r,,) ad'Mat)r*)c*$ad'Mat)r*)c,2*& nGerteE11& - void delete4dge(int vRtart,int v4nd) # if(vRtartnGerteE ++ vRtart<2 ++ v4ndnGerteE++ v4nd<2) return& if(/chec!Dir()) ad'Mat )vRtart12* )v4nd12* $(& - int chec!Dir() # int r,c& for(r$(&r<nGerteE&r,,) for(c$(&c<nGerteE&c,,) if(ad'Mat)r*)c*/$ad'Mat)c*)r*) return 2& return (& - int chec!Vt() # int r,c& for(r$(&r<nGerteE&r,,) for(c$(&c<nGerteE&c,,) if(ad'Mat)r*)c*2) return 2& return (& - void createAraph() # int r,c& printf(35n enter the no. of vertices 8 3)& scanf(3%d3,9nGerteE)& for(r$(&r<nGerteE&r,,) for(c$(&c<nGerteE&c,,) # ad'Mat)r*)c*$(& if(r/$c) insert4dge(r,2,c,2)& - - void vie.Araph() # int v,r,c,edge,inDeg)GRIY4*,outDeg)GRIY4*& for(v$(&v<nGerteE&v,,) printf(3 v%d3,v,2)& for(r$(&r<nGerteE&r,,) # printf(35nv%1Bd 3,r,2)& for(c$(&c<nGerteE&c,,) printf(3%1Bd 3,ad'Mat)r*)c*)& - for(v$(&v<nGerteE&v,,) inDeg)v*$outDeg)v*$(& edge$(& for(r$(&r<nGerteE&r,,) for(c$(&c<nGerteE&c,,) if(ad'Mat)r*)c*/$() # edge,,& outDeg)r*,,& inDeg)c*,,& - if(/chec!Dir()) edge$edgeJB& printf(3n %s Araph3,(chec!Dir())W3DI=4IF4D3 8 3U;DI=4IF4D3)& printf(3n %s Araph3,(chec!Dir())W3Veighted3 8 3U;Veighted3)& printf(3total no. of vertices 8%d3,nGerteE)& printf(35ntotal no.of 4dges 8 %d3,edge)& printf(3GerteE Indegree ?utdegree3)& for(r$(&r<nGerteE&r,,) # printf(35n v%1Bd %1Pd %1Pd3,r,2,inDeg)r*,outDeg)r*)& if(inDeg)r*$$( 99 outDeg)r*/$() printf(38 %s3,3R?U=43)& if(inDeg)r*/$( 99 outDeg)r*$$() printf(38 %s3,3RQI;3)& if(inDeg)r*$$2 99 outDeg)r*$$() printf(38 %s3,3>4;D<;F3)& if(inDeg)r*$$( 99 outDeg)r*$$() printf(38 %s3,3IR?:<F4D3)& - - void displayTmenu() # printf(35n5n 0asic operation in an ad'acency matriE..........3)& printf(35n5t (. Display Menu3)& printf(35n5t 2.Ireation of Araph3)& printf(35n5t B. Insert a GerteE3)& printf(35n5t C. Insert an 4dge3)& printf(35n5t D. Delete aGerteE3)& printf(35n5t 6. Delete an 4dge 3)& printf(35n5t Z. Gei. the Araph3)& printf(35n5t N. 4Eit3)& - OUTPUT: 0asic operation in an ad'acency matriE.......... (. Display Menu 2.Ireation of Araph B. Insert a GerteE C. Insert an 4dge D. Delete aGerteE 6. Delete an 4dge Z. Gei. the Araph N. 4Eit W2 enter the no. of vertices 8 6 enter .eight of the 4dge from v2 to vB 82 enter .eight of the 4dge from v2 to vC 8( enter .eight of the 4dge from v2 to vD 82 enter .eight of the 4dge from v2 to v6 82 enter .eight of the 4dge from vB to v2 82 enter .eight of the 4dge from vB to vC 82 enter .eight of the 4dge from vB to vD 82 enter .eight of the 4dge from vB to v6 8( enter .eight of the 4dge from vC to v2 8( enter .eight of the 4dge from vC to vB 82 enter .eight of the 4dge from vC to vD 82 enter .eight of the 4dge from vC to v6 82 enter .eight of the 4dge from vD to v2 82 enter .eight of the 4dge from vD to vB 82 enter .eight of the 4dge from vD to vC 82 enter .eight of the 4dge from vD to v6 82 enter .eight of the 4dge from v6 to v2 82 enter .eight of the 4dge from v6 to vB 8( enter .eight of the 4dge from v6 to vC 82 enter .eight of the 4dge from v6 to vD 82 WZ v2 vB vC vD v6 v2 ( 2 ( 2 2 vB 2 ( 2 2 ( vC ( 2 ( 2 2 vD 2 2 2 ( 2 v6 2 ( 2 2 ( n U;DI=4IF4D Araphn U;Veighted A raphtotal no. of vertices 86 total no.of 4dges 8 OGerteE Indegree ?utdegree v2 C C vB C C vC C C vD D D v6 C C W N