Advertisement
_andrea_

Untitled

Jun 23rd, 2022
686
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  //STRUCT
  4.  struct nodoalbero {
  5.      int data;
  6.      struct nodoalbero *leftPtr;
  7.      struct nodoalbero *rightPtr;
  8.  };
  9.  typedef struct nodoalbero* tree_pointer;
  10.  
  11.  
  12. //PROTOTIPI
  13. tree_pointer insert(tree_pointer root, int );
  14. void in_order (tree_pointer );
  15.  
  16.  
  17. //MAIN
  18. int main (){
  19.     int num_nodi, value;
  20.     tree_pointer root = NULL;
  21.     printf("quanti nodi volete inserire: ");
  22.     scanf ("%d", &num_nodi);
  23.     for (int i = 0; i <num_nodi; i++){
  24.         printf("inserisci il valore da aggiungere: \n");
  25.         scanf("%d",&value);
  26.         insert(root,value);
  27.     }
  28.     //STAMPA IN ORDINE
  29.     printf("STAMPA IN ORDINE\n");
  30.     in_order(root);
  31. }
  32.  
  33. //DEFINIZIONI DI FUNZIONI
  34. tree_pointer insert(tree_pointer root, int value ){
  35.     if (root == NULL) {
  36.         root = (tree_pointer) malloc (sizeof(tree_pointer));
  37.         if (root != NULL ){
  38.             root -> data = value;
  39.             root -> leftPtr = NULL;
  40.             root -> rightPtr = NULL;
  41.         }else{
  42.             printf("---errore---\nMEMORIA NON DISPONIBILE!");
  43.         }
  44.     }
  45.     if (value > root->data){
  46.         insert (root -> rightPtr , value);
  47.     } else if (value < root-> data){
  48.         insert (root -> leftPtr, value);
  49. }
  50.  }
  51.  
  52.  
  53. void in_order (tree_pointer T){
  54.     if (T != NULL){
  55.         in_order (T->leftPtr);
  56.         printf("%d| ", T->data);
  57.         in_order (T->rightPtr);
  58.     }
  59. }
Advertisement
RAW Paste Data Copied
Advertisement