35 lines
819 B
C
35 lines
819 B
C
#include<stdio.h>
|
|
#include<stdlib.h>
|
|
#define M 100
|
|
|
|
typedef struct N *P;
|
|
typedef struct N{int d;P l,r;}N;P root;
|
|
|
|
int t=-1;P stk[M];
|
|
P cn(int v){P n=malloc(sizeof(N));n->d=v;n->l=n->r=NULL;return n;}
|
|
|
|
P cr(P r,int v){
|
|
if(!r)return cn(v);
|
|
if(v<r->d)r->l=cr(r->l,v);
|
|
else if(v>r->d)r->r=cr(r->r,v);
|
|
return r;
|
|
}
|
|
|
|
void po(P r){if(r){po(r->l);po(r->r);printf("%d ",r->d);}}
|
|
void pr(P r){if(r){printf("%d ",r->d);pr(r->l);pr(r->r);}}
|
|
void io(P r){if(r){io(r->l);printf("%d ",r->d);io(r->r);}}
|
|
|
|
int main(){
|
|
root=cr(root,6);
|
|
root=cr(root,4);
|
|
root=cr(root,83);
|
|
root=cr(root,25);
|
|
root=cr(root,42);
|
|
|
|
printf("Binary Tree (In-Order): ");io(root);puts("");
|
|
printf("Binary Tree (Pre-Order): ");pr(root);puts("");
|
|
printf("Binary Tree (Post-Order): ");po(root);puts("");
|
|
|
|
return 0;
|
|
}
|