Advertisement
tresonance

BINARY TREE FROM SCRATCH

May 4th, 2020
503
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.15 KB | None | 0 0
  1. /*********************************************************************************/
  2. /*    MATHS PHYSIC CODE
  3. /*    BINARY TREE FROM SCRATCH
  4. /*********************************************************************************/
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8.  
  9. #define MAX(x, y)  (  ((x) > (y))  ? (x) : (y) ) //my max function using ternaire
  10.  
  11. //STEP 0: CREATE TREE STRUCT AND HIS NODE
  12. //our binary tree 's node
  13. typedef struct s_node {
  14.     int val; //you can set any data type you want here, i will use int to make thing simple
  15.     struct s_node *left;
  16.     struct s_node *right;    
  17. }t_node;
  18.  
  19. //this is our binary tree struct
  20. typedef struct {
  21.     t_node *node_ptr; //tree is simply node pointer
  22. }t_tree;
  23.  
  24. //STEP 1: INSERT NODE INSIDE TREE
  25. void insert_inside_tree(  t_node **head, int dataToInsert){
  26.     if(head){ //because i will dereference this pointer , i must be sure it is not null
  27.         t_node *new_node = (t_node *)malloc(sizeof(t_node));
  28.         if(NULL == new_node)
  29.             return; //malloc failed, so we stop
  30.         new_node->left = new_node->right = NULL;
  31.         new_node->val = dataToInsert;
  32.         if(NULL == *head){ //if tree is empty we add our node here
  33.             *head = new_node;
  34.             return;
  35.         }
  36.         //if tree is not empty, let's serach the suitable current sub tree ad insert
  37.         if( dataToInsert < (*head)->val)
  38.             insert_inside_tree( & (*head)->left, dataToInsert); //left recursif call
  39.         else
  40.             insert_inside_tree(&(*head)->right, dataToInsert); //right recursif call
  41.     }
  42. }
  43.  
  44.  
  45.  
  46. //STEP 2: DISPLAY OUR TREE IN LEVEL ORDER (AKA LINE BY LINE)
  47. //we will display line by line so let's calculate the maxHheight aka line
  48. unsigned int getTreeMaxHeight(t_node **head){
  49.         if(NULL == *head)
  50.             return (0);
  51.         //let's find the max height in left or right sub tree
  52.         return MAX( 1 + getTreeMaxHeight( &(*head)->left ),1 + getTreeMaxHeight(  &(*head)->right));
  53. }
  54.  
  55. void _display_tree_utils(t_node **head, unsigned int level, int margin){
  56.     if(head && *head){ //if head is not null
  57.             if(level == 0){ //i simply print
  58.                 printf("%*d  ", margin, (*head)->val);
  59.                 return;
  60.             }
  61.             _display_tree_utils( &(*head)->left, level -1, 0.5 * margin);
  62.             _display_tree_utils(&(*head)->right, level - 1, 0.8 * margin);
  63.     }
  64. }
  65.  
  66. void displayTree(t_tree *tree){
  67.     unsigned int height = getTreeMaxHeight( &tree->node_ptr);
  68.     int margin =  26; //you can change margin to display correctly
  69.  
  70.     for(unsigned int level = 0; level < height; level++){
  71.         if( level & 1) //if level is odd number, i use bitwise cause it is fast than modulo
  72.             printf("\x1B[31m"); //print in green color
  73.         else
  74.             printf("\x1B[32m"); //print in red color
  75.  
  76.             _display_tree_utils( &tree->node_ptr, level, margin );
  77.             printf("\n\n");
  78.     }
  79. }
  80.  
  81. //STEP 3: DEALLOCATE MEMORY
  82. void destroyTree(t_node **head){
  83.         if(head && *head){ //if tree is not empty
  84.             //we must destroy from dow to up
  85.             destroyTree( &(*head)->left);
  86.             destroyTree(&(*head)->right);
  87.             free(*head);
  88.             head= NULL;//to avoid dangling pointer
  89.         }
  90. }
  91.  
  92. //STEP 4: MAIN FUNCTION TO TEST
  93. int main(){
  94.     t_tree *tree = (t_tree *)malloc(sizeof(t_tree));
  95.     if(tree){ //if malloc succeed
  96.         tree->node_ptr = NULL; //at the begining, no node inside tree so pointer is null
  97.         insert_inside_tree(  &tree->node_ptr, 12); //datas to insert to list
  98.         insert_inside_tree(&tree->node_ptr, 8);
  99.         insert_inside_tree(&tree->node_ptr, 17);
  100.         insert_inside_tree(&tree->node_ptr, 6);
  101.         insert_inside_tree(&tree->node_ptr, 10);
  102.         insert_inside_tree(&tree->node_ptr, 16);
  103.         insert_inside_tree(&tree->node_ptr, 18);
  104.         insert_inside_tree(&tree->node_ptr, 5);
  105.         insert_inside_tree(&tree->node_ptr, 7);
  106.  
  107.         //display result
  108.         displayTree(tree);
  109.  
  110.         //free memory
  111.         destroyTree(&tree->node_ptr); //destroy tree's t_node * datas
  112.         free(tree); //destroy tree;
  113.    }
  114.     return (0);
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement