Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <assert.h>
- typedef struct node node;
- typedef node *tree;
- struct node{
- int val ;
- tree left;
- tree right;
- };
- //exo1
- void print_abr(tree arbre){
- if (arbre!=NULL){
- print_abr(arbre->left);
- if (arbre ->left!=NULL) printf(";");
- printf("%d",arbre->val);
- if (arbre->right!=NULL) printf(";");
- print_abr(arbre->right);
- }
- }
- //exo2
- tree leaf(int val){
- tree t = malloc(sizeof(node));
- assert(t!=NULL);
- t->left=NULL;
- t->right=NULL;
- t->val=val;
- return t;
- }
- tree insert_abr(tree t, int val){
- if(t==NULL) return leaf(val);
- if(val<t->val){
- t->left=insert_abr(t->left,val);
- }
- if(val>t->val){
- t->right=insert_abr(t->right,val);
- }
- if(val==t->val) return t;
- }
- void free_tree(tree t){
- if (t==NULL) return;
- free_tree(t->left);
- free_tree(t->right);
- free(t);
- }
- //exo3
- tree max_abr(tree t){
- if (t==NULL)return NULL;
- if(t->right==NULL) return t;
- return max_abr(t->right);
- }
- tree min_abr(tree t){
- if (t==NULL)return NULL;
- if(t->left==NULL) return t;
- return min_abr(t->left);
- }
- tree search_abr(tree t, int val){
- if (t==NULL) return NULL;
- if (val==t->val)return t;
- if (val<t->val) return search_abr(t->left,val);
- if (val>t->val) return search_abr(t->right,val);
- return NULL;
- }
- //exo4
- int checkComp(tree t){
- int l=1,r=1;
- if(t->left==NULL && t->right==NULL)return 1;
- if(t->left!=NULL){
- if(t->val>=t->left->val){
- return 0;
- }else{
- l=checkComp(t->right);
- }
- }
- if(t->right!=NULL){
- if(t->val<=t->right->val){
- return 0;
- }
- else {
- r=checkComp(t->right);
- }
- }
- return l && r;
- }
- int check_abr(tree t){
- if (t==NULL)return 1;
- return checkComp(t);
- }
- int main (){
- printf("k");
- tree t=leaf(10);
- printf("m");
- int a[10]={1,4,6,8,11,19,2,3,9,20};
- printf("i"); }
- for (int i=0;i<10;i++){
- printf("%d",a[i]);
- insert_abr(t,a[i]);
- printf("%d",a[i]);
- }
- print_abr(t);
- printf("\n");
- printf("%d",min_abr(t)->val);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement