Код
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
struct tree{
int info;
struct tree *left;
struct tree *right;
};
struct tree *root; //verhuna dereva pochatkova
struct tree *stree(struct tree *root, struct tree *r, int info);
void print_tree(struct tree *root, int l);
int main(void)
{
int s;
root=NULL;
printf("Enter the sumbol: ");
while(scanf("%d",&s))
{
printf("Enter the sumbol: ");
root=stree(root, root, s);
}
print_tree(root, 0);
getch();
return 0;
}
struct tree *stree(struct tree *root, struct tree *r, int info)
{
if(!r)
{
r=(struct tree *) malloc(sizeof(struct tree));
if(!r)
{
printf("ne vustachaye pamyati\n");
exit(0);
}
r->left=NULL;
r->right=NULL;
r->info=info;
if(!root) return r;
if(info<root->info) root->left=r;
else root->right=r;
return r;
}
if(info<r->info) stree(r, r->left, info);
else stree(r, r->right, info);
return root;
}
void print_tree(struct tree *r, int l)
{
int i;
if(!r) return;
print_tree(r->right, l+1);
for(i=0;i<l;i++) printf(" ");
printf("%d\n", r->info);
print_tree(r->left, l+1);
}
#include<stdio.h>
#include<conio.h>
struct tree{
int info;
struct tree *left;
struct tree *right;
};
struct tree *root; //verhuna dereva pochatkova
struct tree *stree(struct tree *root, struct tree *r, int info);
void print_tree(struct tree *root, int l);
int main(void)
{
int s;
root=NULL;
printf("Enter the sumbol: ");
while(scanf("%d",&s))
{
printf("Enter the sumbol: ");
root=stree(root, root, s);
}
print_tree(root, 0);
getch();
return 0;
}
struct tree *stree(struct tree *root, struct tree *r, int info)
{
if(!r)
{
r=(struct tree *) malloc(sizeof(struct tree));
if(!r)
{
printf("ne vustachaye pamyati\n");
exit(0);
}
r->left=NULL;
r->right=NULL;
r->info=info;
if(!root) return r;
if(info<root->info) root->left=r;
else root->right=r;
return r;
}
if(info<r->info) stree(r, r->left, info);
else stree(r, r->right, info);
return root;
}
void print_tree(struct tree *r, int l)
{
int i;
if(!r) return;
print_tree(r->right, l+1);
for(i=0;i<l;i++) printf(" ");
printf("%d\n", r->info);
print_tree(r->left, l+1);
}
getch(); - щоб чекав у кінці натиснення клавіши.
компілював у студії, так що чи не скомпілиться - пиши.