Programda verilen dizideki sayıları üç farklı şekilde ağaçlama kodudur.
#include<stdio.h>
#include<stdlib.h>
struct tree{
struct tree* lptr;
struct tree* rptr;
int info;
};
typedef struct tree* TREE;
TREE getnode();
TREE make(int);
void inorder(TREE);
void postorder(TREE);
void preorder(TREE);
int height(TREE);
int main()
{
TREE root,p,q;
int arr[]={-12,14,15,4,9,7,18,3,5,16,4,20,17,9,14,5};
int size=sizeof(arr)/sizeof(int);
root=make(arr[0]);
for(int i=1;i<size;i++){
p=q=root;
while(arr[i]!=p->info && q!=NULL){
p=q;
if(arr[i]<p->info)
q=p->lptr;
else q=p->rptr;
}
q=make(arr[i]);
if(arr[i]==p->info)
printf("NUMBER %d IS DUBLICATED\n",arr[i]);
else if(arr[i]<p->info)
p->lptr=q;
else p->rptr=q;
}
printf("TREE IS DONE\n");
inorder(root);
printf("\n\n");
postorder(root);
printf("\n\n");
preorder(root);
printf("\n\n");
printf("THE HEIGHT==%d",height(root));
return 0;
}
TREE getnode(){
TREE p;
p=(TREE)malloc(sizeof(struct tree));
return p;
}
TREE make(int x){
TREE p;
p=getnode();
p->info=x;
p->lptr=NULL;
p->rptr=NULL;
printf("%d\n",p->info);
return p;
}
void inorder(TREE root){
if(root!=NULL){
inorder(root->lptr);
printf("%d\n",root->info);
inorder(root->rptr);
}
}
void postorder(TREE root){
if(root!=NULL){
postorder(root->lptr);
postorder(root->rptr);
printf("%d\n",root->info);
}
}
void preorder(TREE root){
if(root!=NULL){
printf("%d\n",root->info);
preorder(root->lptr);
preorder(root->rptr);
}
}
int height(TREE root){
int leftheight,rightheight;
if(!root)
return 0;
leftheight=height(root->lptr);
rightheight=height(root->rptr);
if(leftheight<rightheight)
return ++rightheight;
else return ++leftheight;
}
Trees
Trees
Konuyu Okuyanlar: 1 Ziyaretçi
![[Resim: 114ld.jpg]](http://b1112.hizliresim.com/s/c/114ld.jpg)