upsidedown

Daa Expt2:Maxmin Tree

Feb 21st, 2012
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.71 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. //#include<iostream.h>
  5.  
  6.  
  7.  
  8. int a[7]={ 5,10,51,61,15,20,21};
  9.  
  10.  
  11. struct tree
  12. {
  13.     int max,min,dmax,dmin;
  14.     struct tree *left;
  15.     struct tree *right;
  16.     struct tree *baap;
  17. };
  18.  
  19. typedef struct tree *ptr;
  20. ptr root= NULL;
  21. ptr present=NULL;
  22.  
  23. int max(int a, int b)
  24. {
  25.     return a>b?a:b;
  26. }
  27.  
  28.  
  29. int depth(ptr node)
  30.     {
  31.         if(node == NULL)
  32.         {
  33.             return 0;
  34.         }
  35.  
  36.         return max(depth(node->left) +1,depth(node->right) +1);
  37.  
  38.     }
  39.  
  40.  
  41.  
  42. ptr maketree()
  43. {
  44.     ptr p;
  45.     p=(ptr)malloc(sizeof(struct tree));
  46.  
  47.     p->left=NULL;
  48.     p->right=NULL;
  49.     p->dmax=0;
  50.     p->dmin=0;
  51.  
  52.     if(p==NULL)
  53.     {
  54.         printf("cannot create\n");
  55.         return NULL;
  56.     }
  57.     else
  58.         return p;
  59. }
  60.  
  61.  
  62. void create(ptr q)
  63. {
  64.     int mid;
  65.     if((q->max-q->min) > 1)
  66.     {
  67.         mid= (q->max + q->min)/2;
  68.         ptr x,y;
  69.         x=maketree();
  70.         y=maketree();
  71.  
  72.         x->min=q->min;
  73.         x->max=mid;
  74.  
  75.         y->min=mid+1;
  76.         y->max=q->max;
  77.  
  78.         q->left=x;
  79.         q->right=y;
  80.         create(q->left);
  81.         create(q->right);
  82.     }
  83.  
  84. }
  85.  
  86.  
  87.  
  88.  
  89. void filldata(ptr q)
  90. {
  91.     if(q->left==NULL && q->right== NULL)
  92.     {
  93.         q->dmax= a[q->max];
  94.         q->dmin=a[q->min];
  95.  
  96.         if(q->dmax < q->dmin)
  97.         {
  98.             int t;
  99.             t=q->dmax;
  100.             q->dmax=q->dmin;
  101.             q->dmin=t;
  102.         }
  103.  
  104.     }
  105.  
  106.  
  107.  
  108.     else
  109.     {
  110.         int big, small;
  111.         ptr x,y;
  112.         x=q->left;
  113.         y=q->right;
  114.  
  115.  
  116.  
  117.         if(x->dmax > y->dmax )
  118.         {
  119.             big=x->dmax;
  120.         }
  121.         else
  122.             big=y->dmax;
  123.  
  124.  
  125.  
  126.         if(x->dmin < y->dmin )
  127.         {
  128.             small=x->dmin;
  129.         }
  130.         else
  131.             small=y->dmin;
  132.  
  133.         q->dmax= big;
  134.         q->dmin=small;
  135.     }
  136.  
  137.  
  138. }
  139.  
  140.  
  141.  
  142. void traverse(ptr q)
  143. {
  144.     if(q!=NULL)
  145.     {
  146.     filldata(q);
  147.     traverse(q->left);
  148.     traverse(q->right);
  149.     }
  150. }
  151.  
  152.  
  153.  
  154.  
  155. void display(ptr abc)
  156. {
  157.     if(abc!=NULL)
  158.     {
  159.         //printf("%d ",abc->info);
  160.         printf("\n\n%d \t%d", abc->max+1, abc->min+1);
  161.         printf("\n%d\t%d", abc->dmax, abc->dmin);
  162.  
  163.         display(abc->left);
  164.         display(abc->right);
  165.     }
  166.  
  167. }
  168.  
  169.  
  170.  
  171.  
  172. int _tmain(int argc, _TCHAR* argv[])
  173. {
  174.     int n,i;
  175.     ptr root;
  176.     printf("\nenter the number of elements: ");
  177.     scanf("%d",&n);
  178.     printf("\nEnter the array: ");
  179.     for(i=0;i<n;i++)
  180.       scanf("%d",&a[i]);
  181.  
  182.     root=maketree();
  183.     root->max=n-1;
  184.     root->min=0;
  185.  
  186.     create(root);
  187.     int deep;
  188.     deep= depth(root);
  189.     for(i=0;i<deep;i++)
  190.     {
  191.         traverse(root);
  192.     }
  193.     printf("\n\nThe tree in preorder is \nMax\t\tMin\nData[MAX]\tData[min]");
  194.     display(root);
  195.     system("pause");
  196.  
  197.  
  198.     return 0;
  199. }
  200.  
  201.  
  202. /*
  203.  
  204. enter the number of elements: 7
  205.  
  206. Enter the array: 5 10 51 61 15 20 21
  207.  
  208.  
  209. The tree in preorder is
  210. Max            Min
  211. Data[MAX]      Data[MIN]
  212.  
  213.  
  214. 7       1
  215. 61      5
  216.  
  217. 4       1
  218. 61      5
  219.  
  220. 2       1
  221. 10      5
  222.  
  223. 4       3
  224. 61      51
  225.  
  226. 7       5
  227. 21      15
  228.  
  229. 6       5
  230. 20      15
  231.  
  232. 7       7
  233. 21      21Press any key to continue . . .
  234. */
Advertisement
Add Comment
Please, Sign In to add comment