主题:[讨论]c语言二叉树问题
#include<iostream.h>
#include<stdio.h>
struct tree {
char info;
struct tree *left,*right;
};
struct tree *create_btree(struct tree *root,struct tree *r,char info);
struct tree *search_btree(struct tree *root,char key);
void print_btree(struct tree *r,int l);
void main ()
{ char s[100],c,key=' ' ;
struct tree *root=0 ;
do {
printf("Enter a letter:");
gets(s);
if (!root)
root=create_btree(root,root,*s);
else
create_btree(root,root,*s);
} while (*s) ;
print_btree(root,0);
key='1';
while ( key)
{
printf("Enter a key to find:");
scanf("%s",&c);
root=search_btree(root,c);
printf("press to continue\n");
}
} /* Btree.C 结束 */
struct tree *create_btree(struct tree *root,struct tree *r,char info)
{ if (r ==0 )
{ r=new (struct tree);// same as function: malloc(sizeof())
if ( r == 0)
{ printf("Out of memory\n"); return 0 ; }
r->left= 0; r->right=0; r->info=info;
if (root)
{ if(info<root->info) root -> left=r;
else root -> right=r;
}
else
{ r->right=0; r->left = 0; }
return r;
} /* if = = 0 接下页 */
if (info < r->info)
create_btree(r,r->left,info);
if(info>=r->info)
create_btree(r,r->right,info);
} /* create_btree(root,r,info) */
struct tree *search_btree(struct tree *root,char key)
{ if (!root)
{ printf("Emptu btree\n"); return root; }
while(root->info!=key)
{ if(key<root->info)
root=root->left;
else
root=root->right;
if(root==0)
{ printf("Search Failure\n");
break ;
}
} /* while(root->info!=key) */
if (root !=0)
printf("Successful search\n key=%c\n",root->info);
return root ;
} /* *search_btree(root,key) */
void print_btree(struct tree *r,int l)
{ int i;
if (r == 0) return ;
print_btree(r->left,l+1);
for(i=0;i<l;i++)
printf(" ");
printf("%c\n",r->info);
print_btree(r->right,l+1);
}
为程序写出注释,并画出程序的框图(流程图)
#include<stdio.h>
struct tree {
char info;
struct tree *left,*right;
};
struct tree *create_btree(struct tree *root,struct tree *r,char info);
struct tree *search_btree(struct tree *root,char key);
void print_btree(struct tree *r,int l);
void main ()
{ char s[100],c,key=' ' ;
struct tree *root=0 ;
do {
printf("Enter a letter:");
gets(s);
if (!root)
root=create_btree(root,root,*s);
else
create_btree(root,root,*s);
} while (*s) ;
print_btree(root,0);
key='1';
while ( key)
{
printf("Enter a key to find:");
scanf("%s",&c);
root=search_btree(root,c);
printf("press to continue\n");
}
} /* Btree.C 结束 */
struct tree *create_btree(struct tree *root,struct tree *r,char info)
{ if (r ==0 )
{ r=new (struct tree);// same as function: malloc(sizeof())
if ( r == 0)
{ printf("Out of memory\n"); return 0 ; }
r->left= 0; r->right=0; r->info=info;
if (root)
{ if(info<root->info) root -> left=r;
else root -> right=r;
}
else
{ r->right=0; r->left = 0; }
return r;
} /* if = = 0 接下页 */
if (info < r->info)
create_btree(r,r->left,info);
if(info>=r->info)
create_btree(r,r->right,info);
} /* create_btree(root,r,info) */
struct tree *search_btree(struct tree *root,char key)
{ if (!root)
{ printf("Emptu btree\n"); return root; }
while(root->info!=key)
{ if(key<root->info)
root=root->left;
else
root=root->right;
if(root==0)
{ printf("Search Failure\n");
break ;
}
} /* while(root->info!=key) */
if (root !=0)
printf("Successful search\n key=%c\n",root->info);
return root ;
} /* *search_btree(root,key) */
void print_btree(struct tree *r,int l)
{ int i;
if (r == 0) return ;
print_btree(r->left,l+1);
for(i=0;i<l;i++)
printf(" ");
printf("%c\n",r->info);
print_btree(r->right,l+1);
}
为程序写出注释,并画出程序的框图(流程图)