Abdulg

Binary Tree test WIP [C]

Jan 31st, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.42 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct node
  5. {
  6.     int payload;
  7.     struct node *left, *right;
  8. };
  9.  
  10. struct node* createNode(int payload)
  11. {
  12.     struct node* returnMe = malloc(sizeof(struct node));
  13.     returnMe->payload = payload;
  14.     returnMe->left = NULL;
  15.     returnMe->right = NULL;
  16.     return returnMe;
  17. };
  18.  
  19. void insert(struct node* currentNode, int value)
  20. {
  21.     if(value > currentNode->payload)
  22.     {
  23.         if (currentNode->right == NULL)
  24.         {
  25.             currentNode->right = createNode(value);
  26.             return;
  27.         }
  28.        
  29.         insert(currentNode->right, value);
  30.     }
  31.    
  32.     else
  33.     {
  34.         if (currentNode->left == NULL)
  35.         {
  36.             currentNode->left = createNode(value);
  37.             return;
  38.         }
  39.        
  40.         insert(currentNode->left, value);
  41.     }
  42. }
  43.  
  44. void inOrder(struct node* currentNode)
  45. {
  46.     if (currentNode == NULL) return;
  47.    
  48.     inOrder(currentNode->left);
  49.     printf("%d, ", currentNode->payload);
  50.     inOrder(currentNode->right);
  51. }
  52.  
  53. int delete(struct node* currentNode, int value)
  54. {
  55.     if (currentNode->payload == value);
  56.    
  57.     else if(value > currentNode->payload)
  58.     {
  59.         if (currentNode->right == NULL)
  60.             return 0;
  61.         delete(currentNode->right, value);
  62.     }
  63.    
  64.     else
  65.     {
  66.         if (currentNode->left == NULL)
  67.             return 0;
  68.         delete(currentNode->left, value);
  69.     }
  70.    
  71.     return 0;
  72. }
  73.  
  74. int main()
  75. {
  76.     struct node *root = createNode(5);
  77.     insert(root, 11);
  78.     insert(root, 32);
  79.     insert(root, 4);
  80.     insert(root, 5);
  81.     insert(root, 71);
  82.     insert(root, 1);
  83.    
  84.     inOrder(root);
  85.     return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment