Upload files to "DS/C/Lab/Shortcodes"
This commit is contained in:
parent
4d3da88635
commit
5315639c58
2 changed files with 84 additions and 0 deletions
34
DS/C/Lab/Shortcodes/recurtree.c
Normal file
34
DS/C/Lab/Shortcodes/recurtree.c
Normal file
|
@ -0,0 +1,34 @@
|
|||
#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;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue