ikili bir ağaçtaki tek elemana sahip düğümler yeni bir ağaca kopyalayan fonksiyon

#include <stdio.h>
#include<stdlib.h>

typedef struct BTREE{
int data;
struct node*left;
struct node*right;
}
TREE;


struct node* root=NULL;
struct node* root1=NULL;

TREE *new_node(int data){
TREE *p=(TREE*)malloc(sizeof(TREE));
p->data=data;
p->left=NULL;
p->right=NULL;
return p;
}
void inorder(TREE *root){
if(root!=NULL){
    inorder(root->left);
    printf("%d\t",root->data);
    inorder(root->right);
    }
}
TREE *insert(TREE*root,int data){
    if(root!=NULL){
    if(data<root->data)
        root->left=insert(root->left,data);
    else if(data>root->data)
        root->right=insert(root->right,data);
    }
    else
     root=new_node(data);
    return root;

}

TREE *copy(TREE*root,TREE*root1)
{
    if(root!=NULL){
        if(root->data%2==1)
        root1=insert(root1,root->data);
        root1=copy(root->left,root1);
        root1=copy(root->right,root1);

    }
    return root1;
}




int main(){
    int i;

int dizi[14]={27,3,5,4,6,8,7,9,14,11,10,13};
for(i=0; i<14; i++){
    root=insert(root,dizi[i]);
}
printf("agac yapisi inorder\n");
printf("\n");
inorder(root);
root1=copy(root,root1);
printf("agac yapisi inorder\n");

inorder(root1);




}

Yorumlar

Yorum Gönder