fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct node{
  5. int val;
  6. struct node *left;
  7. struct node *right;
  8. };
  9.  
  10. void killtree(struct node *leaf){
  11. if(leaf->left!=NULL){
  12. killtree(leaf->left);
  13. }
  14. if(leaf->right!=NULL){
  15. killtree(leaf->right);
  16. }
  17. free(leaf);
  18. }
  19.  
  20. void getorder(struct node *leaf, int *f, int *i){
  21. if(leaf->left!=NULL){
  22. getorder(leaf->left, f, i);
  23. }
  24. f[*i]=leaf->val;
  25. *i++;
  26. if(leaf->right!=NULL){
  27. getorder(leaf->right, f, i);
  28. }
  29. }
  30.  
  31. void create(int co, struct node **leaf){
  32. if(*leaf==NULL){
  33. (*leaf)=malloc(sizeof(**leaf));
  34. (*leaf)->val=co;
  35. (*leaf)->left=NULL;
  36. (*leaf)->right=NULL;
  37. }
  38. else if(co<(*leaf)->val){
  39. create(co, &(*leaf)->left);
  40. }
  41. else if(co>=(*leaf)->val){
  42. create(co, &(*leaf)->right);
  43. }
  44. }
  45.  
  46. void tree(int *f, int c){
  47. int i, *j;
  48. struct node *root;
  49. root=NULL;
  50. for(i=0;i<c;i++){
  51. create(f[i], &root);
  52. }
  53. j=malloc(sizeof(*j));
  54. *j=0;
  55. getorder(root, f, j);
  56. killtree(root);
  57. }
  58.  
  59. int main(){
  60. int *pole, i, count=3;
  61. pole[0]=25;
  62. pole[1]=12;
  63. pole[2]=16;
  64. tree(pole, count);
  65. printf("Done!\n");
  66. printf("Zapis do souboru dokoncen!!");
  67. for(i=1;i<count;i++){
  68. printf("%d\t",pole[i]);
  69. }
  70. free(pole);
  71. return(0);
  72. }
Runtime error #stdin #stdout 0.01s 1672KB
stdin
Standard input is empty
stdout
Standard output is empty