upsidedown

maxmin hardcoded

Feb 9th, 2012
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.12 KB | None | 0 0
  1. //todo: find depth of tree and then call traverse depth number of times
  2.  
  3. // maxmin.cpp : Defines the entry point for the console application.
  4. //
  5.  
  6. #include "stdafx.h"
  7. #include<stdio.h>
  8. #include<stdlib.h>
  9. //#include<iostream.h>
  10.  
  11.  
  12. int a[7]={ 5,10,51,61,15,20,21};
  13.  
  14.  
  15. struct tree
  16. {
  17.     int max,min,dmax,dmin;
  18.     struct tree *left;
  19.     struct tree *right;
  20.     struct tree *baap;
  21. };
  22.  
  23. typedef struct tree *ptr;
  24. ptr root= NULL;
  25. ptr present=NULL;
  26.  
  27. ptr maketree()
  28. {
  29.     ptr p;
  30.     p=(ptr)malloc(sizeof(struct tree));
  31.  
  32.     p->left=NULL;
  33.     p->right=NULL;
  34.     p->dmax=0;
  35.     p->dmin=0;
  36.  
  37.     if(p==NULL)
  38.     {
  39.         printf("cannot create\n");
  40.         return NULL;
  41.     }
  42.     else
  43.         return p;
  44. }
  45.  
  46.  
  47. void create(ptr q)
  48. {
  49.     int mid;
  50.     if((q->max-q->min) > 1)
  51.     {
  52.         mid= (q->max + q->min)/2;
  53.         ptr x,y;
  54.         x=maketree();
  55.         y=maketree();
  56.  
  57.         x->min=q->min;
  58.         x->max=mid;
  59.  
  60.         y->min=mid+1;
  61.         y->max=q->max;
  62.  
  63.         q->left=x;
  64.         q->right=y;
  65.         create(q->left);
  66.         create(q->right);      
  67.     }
  68.  
  69. }
  70.  
  71.  
  72.  
  73.  
  74. void filldata(ptr q)
  75. {
  76.     if(q->left==NULL && q->right== NULL)
  77.     {
  78.         q->dmax= a[q->max];
  79.         q->dmin=a[q->min];
  80.  
  81.         if(q->dmax < q->dmin)
  82.         {
  83.             int t;
  84.             t=q->dmax;
  85.             q->dmax=q->dmin;
  86.             q->dmin=t;
  87.         }
  88.  
  89.     }
  90.  
  91.  
  92.  
  93.     else
  94.     {
  95.         int big, small;
  96.         ptr x,y;
  97.         x=q->left;
  98.         y=q->right;
  99.  
  100.            
  101.  
  102.         if(x->dmax > y->dmax )
  103.         {
  104.             big=x->dmax;
  105.         }
  106.         else
  107.             big=y->dmax;
  108.  
  109.  
  110.  
  111.         if(x->dmin < y->dmin )
  112.         {
  113.             small=x->dmin;
  114.         }
  115.         else
  116.             small=y->dmin;
  117.  
  118.         q->dmax= big;
  119.         q->dmin=small;
  120.     }
  121.    
  122.  
  123. }
  124.  
  125.  
  126.  
  127. void traverse(ptr q)
  128. {
  129.     if(q!=NULL)
  130.     {  
  131.     filldata(q);
  132.     traverse(q->left);
  133.     traverse(q->right);
  134.     }
  135. }
  136.    
  137.  
  138.  
  139.  
  140. void display(ptr abc)
  141. {
  142.     if(abc!=NULL)
  143.     {
  144.         //printf("%d ",abc->info);
  145.         printf("\n\n%d \t%d", abc->max+1, abc->min+1);
  146.         printf("\n%d\t%d", abc->dmax, abc->dmin);
  147.        
  148.         display(abc->left);
  149.         display(abc->right);
  150.     }
  151.  
  152. }
  153.  
  154.  
  155.  
  156.  
  157. int _tmain(int argc, _TCHAR* argv[])
  158. {
  159.     ptr root;
  160.     root=maketree();
  161.     root->max=6;
  162.     root->min=0;
  163.  
  164.     create(root);
  165. // find depth of the tree and call traverse() depth number of times
  166.  
  167.     traverse(root);
  168.     traverse(root);
  169.         traverse(root);
  170.  
  171.  
  172.     display(root);
  173.     system("pause");
  174.  
  175.  
  176.     return 0;
  177. }
Advertisement
Add Comment
Please, Sign In to add comment