Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //todo: find depth of tree and then call traverse depth number of times
- // maxmin.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include<stdio.h>
- #include<stdlib.h>
- //#include<iostream.h>
- int a[7]={ 5,10,51,61,15,20,21};
- struct tree
- {
- int max,min,dmax,dmin;
- struct tree *left;
- struct tree *right;
- struct tree *baap;
- };
- typedef struct tree *ptr;
- ptr root= NULL;
- ptr present=NULL;
- ptr maketree()
- {
- ptr p;
- p=(ptr)malloc(sizeof(struct tree));
- p->left=NULL;
- p->right=NULL;
- p->dmax=0;
- p->dmin=0;
- if(p==NULL)
- {
- printf("cannot create\n");
- return NULL;
- }
- else
- return p;
- }
- void create(ptr q)
- {
- int mid;
- if((q->max-q->min) > 1)
- {
- mid= (q->max + q->min)/2;
- ptr x,y;
- x=maketree();
- y=maketree();
- x->min=q->min;
- x->max=mid;
- y->min=mid+1;
- y->max=q->max;
- q->left=x;
- q->right=y;
- create(q->left);
- create(q->right);
- }
- }
- void filldata(ptr q)
- {
- if(q->left==NULL && q->right== NULL)
- {
- q->dmax= a[q->max];
- q->dmin=a[q->min];
- if(q->dmax < q->dmin)
- {
- int t;
- t=q->dmax;
- q->dmax=q->dmin;
- q->dmin=t;
- }
- }
- else
- {
- int big, small;
- ptr x,y;
- x=q->left;
- y=q->right;
- if(x->dmax > y->dmax )
- {
- big=x->dmax;
- }
- else
- big=y->dmax;
- if(x->dmin < y->dmin )
- {
- small=x->dmin;
- }
- else
- small=y->dmin;
- q->dmax= big;
- q->dmin=small;
- }
- }
- void traverse(ptr q)
- {
- if(q!=NULL)
- {
- filldata(q);
- traverse(q->left);
- traverse(q->right);
- }
- }
- void display(ptr abc)
- {
- if(abc!=NULL)
- {
- //printf("%d ",abc->info);
- printf("\n\n%d \t%d", abc->max+1, abc->min+1);
- printf("\n%d\t%d", abc->dmax, abc->dmin);
- display(abc->left);
- display(abc->right);
- }
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- ptr root;
- root=maketree();
- root->max=6;
- root->min=0;
- create(root);
- // find depth of the tree and call traverse() depth number of times
- traverse(root);
- traverse(root);
- traverse(root);
- display(root);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment