Advertisement
NB52053

NADUI_LAB9

Dec 18th, 2016
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.22 KB | None | 0 0
  1. // BFS & DFS Mixed! ------------- NB: Here are lots of spelling mistake,Because this whole bunch of LAB 9 codes collected from Toma!She also collected from someone who did all these shits!//
  2.  
  3. #include<stdio.h>
  4. #include<conio.h>
  5. #include<stdlib.h>
  6. #include<string.h>
  7.  
  8. struct node
  9. {
  10.     char name;
  11.     struct edge* edges;
  12. }*A,*B,*C,*D,*E,*F;
  13.  
  14. struct edge
  15. {
  16.     char name[10];
  17.     edge* next_edge;
  18.     node* dest_node;
  19. }*AB,*AC,*BD,*BE,*CF;
  20.  
  21. struct Queue
  22. {
  23.     struct node* name;
  24.     Queue *next;
  25. }*head=NULL,*tail=NULL;
  26.  
  27. //declared queue//
  28. const int size=10;
  29. int queue[size];
  30. int fornt=-1,rear=-1;
  31.  
  32. //declared functions//
  33. node* creat_node(char n);
  34. edge* createdges(char name[20], node *dNode);
  35. void creat_graph();
  36. void dfs(node* nd);
  37. void bfs(node* nd);
  38. void enqueue(node* n);
  39. node* dequeue();
  40.  
  41. int main()
  42. {
  43.     creat_graph();
  44.     printf("Depth First Search Is:");
  45.     dfs(A);
  46.     printf("\nBreath first search is:A");
  47.     bfs(A);
  48.     getch();
  49. }
  50. node* creat_node(char n)
  51. {
  52.     struct node *temp=(node*)malloc(sizeof(node));
  53.     temp->name=n;
  54.     temp->edges=NULL;
  55.     return temp;
  56. }
  57. edge* createdges(char name[20], node *dNode)
  58. {
  59.      edge *temp=(edge*)malloc(sizeof(edge));
  60.      strcpy(temp->name,name);
  61.      temp->dest_node=dNode;
  62.      temp->next_edge=NULL;
  63.      return temp;
  64. }
  65. void creat_graph()
  66. {
  67.     //creating Node//
  68.     A=creat_node('A');
  69.     B=creat_node('B');
  70.     C=creat_node('C');
  71.     D=creat_node('D');
  72.     E=creat_node('E');
  73.     F=creat_node('F');
  74.     //craetiing Edge//
  75.     AB=createdges("AB",B);
  76.     AC=createdges("AC",C);
  77.     BD=createdges("BD",D);
  78.     BE=createdges("BE",E);
  79.     CF=createdges("CF",F);
  80.     //creating Link between node and enge//
  81.     A->edges=AB;
  82.     AB->next_edge=AC;
  83.     B->edges=BD;
  84.     BD->next_edge=BE;
  85.     C->edges=CF;
  86. }
  87. void dfs(node* nd)
  88. {
  89.  
  90.     if(nd!=NULL)
  91.     {
  92.         printf("%c",*nd);
  93.         edge *temp;
  94.         node* ndc;
  95.         temp=nd->edges;
  96.         while (temp!=NULL)
  97.         {
  98.             ndc=temp->dest_node;
  99.             dfs(ndc);
  100.             temp=temp->next_edge;
  101.         }
  102.     }
  103. }
  104. void enqueue(node* n)
  105. {
  106.     Queue *std=(Queue*)malloc(sizeof(Queue));
  107.     std->name=n;
  108.     if (head==NULL)
  109.     {
  110.         std->next=NULL;
  111.         head=std;
  112.     }
  113.     else
  114.     {
  115.         std->next=NULL;
  116.         tail->next=std;
  117.     }
  118.     tail=std;
  119. }
  120. node* dequeue()
  121. {
  122.     node* p;
  123.     if (head!=NULL)
  124.     {
  125.         p=head->name;
  126.         head=head->next;
  127.         return p;
  128.     }
  129.     else
  130.     {
  131.         return NULL;
  132.     }
  133. }
  134. void bfs(node* nd)
  135. {
  136.  
  137.     if (nd!=NULL)
  138.     {
  139.         edge* temp;
  140.         edge* ch;
  141.         node*ndc;
  142.         temp=nd->edges;
  143.         while (temp!=NULL)
  144.         {
  145.             ndc=temp->dest_node;
  146.             ch=ndc->edges;
  147.             printf("%c",*ndc);
  148.             if (ch!=NULL)
  149.             {
  150.                 enqueue(ndc);
  151.             }
  152.             temp=temp->next_edge;
  153.         }
  154.     }
  155.     nd=dequeue();
  156.         if (nd!=NULL)
  157.         {
  158.             bfs(nd);
  159.         }
  160. }
  161.  
  162. //------------------------------------------------------------END----------------------------------------------------------------------
  163.  
  164.  
  165.  
  166. //--------------------------------------------------S--T--A--R---T--------------------------------------------------------------------
  167.  
  168.  
  169. /*BINARY SEARCH TREE*/
  170.  
  171. #include<stdio.h>
  172.  
  173.  
  174. void quicksort(int a[1000], int first, int last)
  175. {
  176.     int marker, i, j, temp;
  177.  
  178.     if (first<last)
  179.     {
  180.         marker = first;
  181.         i = first;
  182.         j = last;
  183.  
  184.         while (i<j)
  185.         {
  186.             while (a[i] <= a[marker] && i<last)
  187.                 i++;
  188.             while (a[j]>a[marker])
  189.                 j--;
  190.             if (i<j)
  191.             {
  192.                 temp = a[i];
  193.                 a[i] = a[j];
  194.                 a[j] = temp;
  195.             }
  196.         }
  197.  
  198.         temp = a[j];
  199.         a[j] = a[marker];
  200.         a[marker] = temp;
  201.  
  202.         quicksort(a, first, j - 1);
  203.         quicksort(a, j + 1, last);
  204.     }
  205. }
  206.  
  207.  
  208.  
  209. int main()
  210. {
  211.  
  212.     int n = 10, i, a[10] = { 5, 4, 8, 2, 1, 6, 8, 1, 31 };
  213.     quicksort(a, 0, n - 1);
  214.     for (i = 0; i<n; i++)
  215.     {
  216.         printf("%d ", a[i]);
  217.     }
  218.  
  219.     return 0;
  220. }
  221.  
  222.  
  223. ----------------------------------------------------END--------------------------------------------------------------------------------
  224.  
  225.  
  226. // Inorder, Postorder, Preorder!
  227.  
  228. #include<stdio.h>
  229. #include<stdlib.h>
  230.  
  231. struct tree
  232. {
  233.     int value;
  234.     tree *left;
  235.     tree *right;
  236. }*head;
  237.  
  238. void create(int x)
  239. {
  240.     struct  tree *temp,*temp1;
  241.     int flag=0;
  242.     if(head==NULL)
  243.     {
  244.         head=(tree*)malloc(sizeof(tree));
  245.         head->value=x;
  246.         head->left=NULL;
  247.         head->right=NULL;
  248.         return;
  249.     }
  250.     temp=head;
  251.     while(temp!=NULL)
  252.     {
  253.         if(x>temp->value)
  254.         {
  255.             temp1=temp;
  256.             temp=temp->right;
  257.             flag=0;
  258.         }
  259.         else if(x<temp->value)
  260.         {
  261.             temp1=temp;
  262.             temp=temp->left;
  263.             flag=1;
  264.         }
  265.         else
  266.         {
  267.             temp1=temp;
  268.             temp=temp->right;
  269.             flag=0;
  270.         }
  271.     }
  272.     temp=(tree*)malloc(sizeof(tree));
  273.     temp->value=x;
  274.     temp->left=NULL;
  275.     temp->right=NULL;
  276.     if(flag==0)
  277.     {
  278.         temp1->right=temp;
  279.     }
  280.     else
  281.     {
  282.         temp1->left=temp;
  283.     }
  284. }
  285. void in(tree *base)
  286. {
  287.     if(base!=NULL)
  288.     {
  289.         in(base->left);
  290.  
  291.         printf("%d ",base->value);
  292.         in(base->right);
  293.  
  294.     }
  295. }
  296. void pre(tree *base)
  297. {
  298.     if(base!=NULL)
  299.     {
  300.         printf("%d ",base->value);
  301.         in(base->left);
  302.         in(base->right);
  303.  
  304.     }
  305. }
  306. void post(tree *base)
  307. {
  308.     if(base!=NULL)
  309.     {
  310.  
  311.         in(base->left);
  312.         in(base->right);
  313.         printf("%d ",base->value);
  314.  
  315.     }
  316. }
  317.  
  318. void min()
  319. {
  320.     tree *temp;
  321.     temp=head;
  322.     while(temp->left!=NULL)
  323.     {
  324.         temp=temp->left;
  325.     }
  326.     printf("%d is minimum\n",temp->value);
  327.  
  328.  
  329. }
  330. void search(int x)
  331. {
  332.     tree *temp;
  333.     temp=head;
  334.     if(x==temp->value)
  335.     {
  336.         printf("found\n");
  337.         return;
  338.     }
  339.     if(x>temp->value)
  340.     {
  341.         while(temp!=NULL)
  342.         {
  343.             if(temp->value==x)
  344.             {
  345.                 printf("found\n");
  346.                 return;
  347.             }
  348.             temp=temp->right;
  349.         }
  350.         printf("not found\n");
  351.     }
  352.     else
  353.     {
  354.         while(temp!=NULL)
  355.         {
  356.             if(temp->value==x)
  357.             {
  358.                 printf("found\n");
  359.                 return;
  360.             }
  361.             temp=temp->left;
  362.         }
  363.         printf("not found\n");
  364.     }
  365. }
  366.  
  367.  
  368. void main()
  369. {
  370.     int value=0,choice=0;
  371.  
  372.     while(1)
  373.     {
  374.         printf("\n1->INSERT\n");
  375.         printf("2->INORDER\n");
  376.         printf("3->PREORDER\n");
  377.         printf("4->POSTORDER\n");
  378.         printf("5->MINIMUM\n");
  379.  
  380.  
  381.         scanf("%d",&choice);
  382.         switch (choice)
  383.         {
  384.         case 1:
  385.             printf("Enter the value\n");
  386.             scanf("%d", &value);
  387.             create(value);
  388.             printf("Value Inserted\n");
  389.             break;
  390.         case 2:
  391.             in(head);
  392.             break;
  393.         case 3:
  394.             pre(head);
  395.             break;
  396.         case 4:
  397.             post(head);
  398.             break;
  399.         case 5:
  400.             min();
  401.             break;
  402.         case 6:
  403.             printf("Enter the value\n");
  404.             scanf("%d", &value);
  405.             search(value);
  406.             break;
  407.         default:
  408.             break;
  409.         }
  410.  
  411.     }
  412.  
  413.  
  414. }
  415.  
  416.  
  417.  
  418. ---------------------------------------END---------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement