в общем, условие такое:
Постройте дерево двоичного поиска для такого списка ключей: 6 17 21 35 46 51 59 66 78 83
я шарилась по сети и нашла одну программу которая по идее должна выводить дерево, вот ее код, НО ОНА ЭТОГО НЕ ДЕЛАЕТ! вредная Х_Х подскажите что в ней не так или дайте наводку на норм код
Код
#include<stdlib.h>
#include<stdio.h>
struct tree{
char info;
struct tree *left;
struct tree *right;
};
struct tree *root; //verhuna dereva pochatkova
struct tree *stree(struct tree *root, struct tree *r, char info);
void print_tree(struct tree *root, int l);
int main(void)
{
char s[10];
root=NULL;
do
{
printf("Enter the sumbol: ");
gets(s);
root=stree(root, root, *s);
}while(*s);
print_tree(root, 0);
return 0;
}
struct tree *stree(struct tree *root, struct tree *r, char 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;r->info);
print_tree(r->left, l+1);
}
#include<stdio.h>
struct tree{
char info;
struct tree *left;
struct tree *right;
};
struct tree *root; //verhuna dereva pochatkova
struct tree *stree(struct tree *root, struct tree *r, char info);
void print_tree(struct tree *root, int l);
int main(void)
{
char s[10];
root=NULL;
do
{
printf("Enter the sumbol: ");
gets(s);
root=stree(root, root, *s);
}while(*s);
print_tree(root, 0);
return 0;
}
struct tree *stree(struct tree *root, struct tree *r, char 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;r->info);
print_tree(r->left, l+1);
}