Advertisement
coffeebeforecode

q1

Jun 20th, 2021
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.37 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define MAX 50
  5.  
  6. struct node {
  7.     int code;
  8.     char name[MAX];
  9.     int price;
  10.     int amount;
  11.     char dateRec[MAX];
  12.     char dateExp[MAX];
  13.     struct node *left, *right;
  14. };
  15.  
  16. struct node* newNode(int code,char name[MAX],int price, int amount, char dateRec[MAX], char dateExp[MAX]);
  17. struct node* insert(struct node* node, int code,char name[MAX],int price, int amount, char dateRec[MAX], char dateExp[MAX]);
  18.  
  19. struct node* minValueNode(struct node* node);
  20. struct node* deleteNode(struct node* root, int code);
  21.  
  22. void printInOrder(struct node* root){
  23.  
  24. int main(){
  25.     int ch;
  26.     struct node* root = NULL;
  27.     int code;
  28.     char name[MAX];
  29.     int price;
  30.     int amount;
  31.     char dateRec[MAX];
  32.     char dateExp[MAX];
  33.        
  34.     while (1){
  35.         printf("\n\tMENU\n");
  36.         printf("\n1. Insert into tree");
  37.         printf("\n2. Delete from tree");
  38.         printf("\n3. Print tree");
  39.         printf("\n4. Exit\n>> ");
  40.         scanf("%d", &ch);
  41.         int val;
  42.         switch (ch){
  43.             case 1:
  44.                 printf("\nInput code: ");
  45.                 scanf("%d", &code);
  46.                 printf("\nInput name: ");
  47.                 scanf("%s", name);
  48.                 printf("\nInput price: ");
  49.                 scanf("%d", &price);
  50.                 printf("\nInput amount: ");
  51.                 scanf("%d", &amount);
  52.                 printf("\nInput Recieved Date: ");
  53.                 scanf("%s", dateRec);
  54.                 printf("\nInput Expiry Date: ");
  55.                 scanf("%s", dateExp);
  56.                 root = insert(root, code, name, price, amount, dateRec, dateExp);
  57.                 printf("\nInserted\n");
  58.                 printInOrder(root);
  59.                 break;
  60.             case 2:
  61.                 printf("\nEnter value to delete: ");
  62.                 scanf("%d", &val);
  63.                 root = deleteNode(root, val);
  64.                 break;
  65.             case 3:
  66.                 printInOrder(root);
  67.                 break;
  68.             case 4:
  69.                 exit(0);
  70.             default:
  71.                 printf("\nInvalid option\n");
  72.         }
  73.     }
  74.  
  75.     return 0;
  76. }
  77. // A utility function to create a new BST node
  78. struct node* newNode(int code,char name[MAX],int price, int amount, char dateRec[MAX], char dateExp[MAX])
  79. {
  80.     struct node* temp
  81.         = (struct node*)malloc(sizeof(struct node));
  82.     temp->code = code;
  83.     strcpy(temp->name, name);
  84.     temp->price = price;
  85.     temp->amount = amount;
  86.     strcpy(temp->dateRec, dateRec);
  87.     strcpy(temp->dateExp, dateExp);
  88.     temp->left = temp->right = NULL;
  89.     return temp;
  90. }
  91.  
  92.  
  93. /* A utility function to
  94.    insert a new node with given code in
  95.  * BST */
  96. struct node* insert(struct node* node, int code,char name[MAX],int price, int amount, char dateRec[MAX], char dateExp[MAX])
  97. {
  98.     /* If the tree is empty, return a new node */
  99.     if (node == NULL)
  100.         return newNode(code, name, price, amount, dateRec, dateExp);
  101.  
  102.     /* Otherwise, recur down the tree */
  103.     if (code < node->code)
  104.         node->left =  insert(node->left, code, name, price, amount, dateRec, dateExp);
  105.     else
  106.         node->right = insert(node->right, code, name, price, amount, dateRec, dateExp);
  107.  
  108.     /* return the (unchanged) node pointer */
  109.     return node;
  110. }
  111.  
  112. /* Given a non-empty binary search
  113.    tree, return the node
  114.    with minimum code value found in
  115.    that tree. Note that the
  116.    entire tree does not need to be searched. */
  117. struct node* minValueNode(struct node* node)
  118. {
  119.     struct node* current = node;
  120.  
  121.     /* loop down to find the leftmost leaf */
  122.     while (current && current->left != NULL)
  123.         current = current->left;
  124.  
  125.     return current;
  126. }
  127.  
  128. /* Given a binary search tree
  129.    and a code, this function
  130.    deletes the code and
  131.    returns the new root */
  132. struct node* deleteNode(struct node* root, int code)
  133. {
  134.     // base case
  135.     if (root == NULL)
  136.         return root;
  137.  
  138.     // If the code to be deleted
  139.     // is smaller than the root's
  140.     // code, then it lies in left subtree
  141.     if (code < root->code)
  142.         root->left = deleteNode(root->left, code);
  143.  
  144.     // If the code to be deleted
  145.     // is greater than the root's
  146.     // code, then it lies in right subtree
  147.     else if (code > root->code)
  148.         root->right = deleteNode(root->right, code);
  149.  
  150.     // if code is same as root's code,
  151.     // then This is the node
  152.     // to be deleted
  153.     else {
  154.         // node with only one child or no child
  155.         if (root->left == NULL) {
  156.             struct node* temp = root->right;
  157.             free(root);
  158.             return temp;
  159.         }
  160.         else if (root->right == NULL) {
  161.             struct node* temp = root->left;
  162.             free(root);
  163.             return temp;
  164.         }
  165.  
  166.         // node with two children:
  167.         // Get the inorder successor
  168.         // (smallest in the right subtree)
  169.         struct node* temp = minValueNode(root->right);
  170.  
  171.         // Copy the inorder
  172.         // successor's content to this node
  173.         root->code = temp->code;
  174.  
  175.         // Delete the inorder successor
  176.         root->right = deleteNode(root->right, temp->code);
  177.     }
  178.     return root;
  179. }
  180.  
  181. void printInOrder(struct node* root){
  182.     // left, root, right
  183.     if (root != NULL){
  184.         printInOrder(root->left);
  185.         printf("%c ", root->code);
  186.         printInOrder(root->right);
  187.     }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement