You need to enable JavaScript to run this app.

Skip to main content

Posting Freak
Trees
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;
}
[Resim: 114ld.jpg]



Ben göremem daha uzun boyunu
Ahret derler kısaltamam yolunu
Bugün Sahı Merdan sarsın oglunu
Yetis Ya Üseyin baban gidiyo

İçerik sağlayıcı paylaşım sitesi olarak hizmet veren Pir Zöhre Ana Forum sitemizde 5651 sayılı kanunun 8. maddesine ve T.C.K'nın 125. maddesine göre tüm üyelerimiz yaptıkları paylaşımlardan kendileri sorumludur. Sitemiz hakkında yapılacak tüm hukuksal şikayetleri İletişim bağlantısından bize ulaşıldıktan en geç 3 (üç) gün içerisinde ilgili kanunlar ve yönetmenlikler çerçevesinde tarafımızca incelenerek, gereken işlemler yapılacak ve site yöneticilerimiz tarafından bilgi verilecektir.