Advertisement
Vladpepe

Untitled

Feb 10th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.99 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include "stdio.h"
  4. #include "stdlib.h"
  5.  
  6. struct Node {
  7.     struct Node *left;
  8.     double data;
  9.     struct Node *right;
  10. };
  11.  
  12. void InOrderPrdouble(struct Node* root) {
  13.     if (root == NULL) {
  14.         return;
  15.     }
  16.     if (root->left == NULL && root->right == NULL) {
  17.         printf("%f  ", root->data);
  18.         return;
  19.     }
  20.     if (root->left != NULL) {
  21.         InOrderPrdouble(root->left);
  22.     }
  23.     printf("%f  ", root->data);
  24.     if (root->right != NULL) {
  25.         InOrderPrdouble(root->right);
  26.     }
  27. }
  28.  
  29. struct Node *GetNewNode(double data) {
  30.     struct Node* newNode = malloc(sizeof(struct Node));
  31.     newNode->data = data;
  32.     newNode->left = NULL;
  33.     newNode->right = NULL;
  34.     return newNode;
  35. }
  36.  
  37. struct Node* Insert(struct Node *root, double data) {
  38.     if (root == NULL) {
  39.         root = GetNewNode(data);
  40.         return root;
  41.     }
  42.     else if (data <= root->data) {
  43.         root->left = Insert(root->left, data);
  44.     }
  45.     else {
  46.         root->right = Insert(root->right, data);
  47.     }
  48.     return root;
  49. }
  50.  
  51. void funzione(struct Node *root,double altezza_attuale,double *count_foglie, double *altezze_tot) {
  52.     if ((root->left == NULL) && (root->right == NULL)) {  //raggiunto foglia
  53.         (*count_foglie)++;
  54.         (*altezze_tot) += altezza_attuale;
  55.         return;
  56.     }
  57.  
  58.     if (root->left != NULL) {
  59.         funzione(root->left, altezza_attuale + 1, count_foglie, altezze_tot);
  60.     }
  61.  
  62.     if (root->right != NULL) {
  63.         funzione(root->right, altezza_attuale + 1, count_foglie, altezze_tot);
  64.     }
  65.  
  66. }
  67.  
  68.  
  69. int main() {
  70.  
  71.     struct  Node *root = NULL;
  72.     root = Insert(root, 15);
  73.     root = Insert(root, 10);
  74.     root = Insert(root, 20);
  75.     root = Insert(root, 25);
  76.     root = Insert(root, 8);
  77.     root = Insert(root, 12);
  78.     root = Insert(root, 35);
  79.     root = Insert(root, 45);
  80.     printf("albero = ");
  81.     InOrderPrdouble(root);
  82.  
  83.     double count_foglie = 0;
  84.     double altezze_tot = 0;
  85.     double altezza_attuale = 0;
  86.  
  87.     funzione(root, altezza_attuale, &count_foglie, &altezze_tot);
  88.  
  89.     printf("\n\n%f  ", count_foglie);
  90.     printf("%f", altezze_tot);
  91.  
  92.     printf("\ncammino medio = %f", altezze_tot/count_foglie);
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement