Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define MAX 50
- struct node {
- int code;
- char name[MAX];
- int price;
- int amount;
- char dateRec[MAX];
- char dateExp[MAX];
- struct node *left, *right;
- };
- struct node* newNode(int code,char name[MAX],int price, int amount, char dateRec[MAX], char dateExp[MAX]);
- struct node* insert(struct node* node, int code,char name[MAX],int price, int amount, char dateRec[MAX], char dateExp[MAX]);
- struct node* minValueNode(struct node* node);
- struct node* deleteNode(struct node* root, int code);
- void printInOrder(struct node* root){
- int main(){
- int ch;
- struct node* root = NULL;
- int code;
- char name[MAX];
- int price;
- int amount;
- char dateRec[MAX];
- char dateExp[MAX];
- while (1){
- printf("\n\tMENU\n");
- printf("\n1. Insert into tree");
- printf("\n2. Delete from tree");
- printf("\n3. Print tree");
- printf("\n4. Exit\n>> ");
- scanf("%d", &ch);
- int val;
- switch (ch){
- case 1:
- printf("\nInput code: ");
- scanf("%d", &code);
- printf("\nInput name: ");
- scanf("%s", name);
- printf("\nInput price: ");
- scanf("%d", &price);
- printf("\nInput amount: ");
- scanf("%d", &amount);
- printf("\nInput Recieved Date: ");
- scanf("%s", dateRec);
- printf("\nInput Expiry Date: ");
- scanf("%s", dateExp);
- root = insert(root, code, name, price, amount, dateRec, dateExp);
- printf("\nInserted\n");
- printInOrder(root);
- break;
- case 2:
- printf("\nEnter value to delete: ");
- scanf("%d", &val);
- root = deleteNode(root, val);
- break;
- case 3:
- printInOrder(root);
- break;
- case 4:
- exit(0);
- default:
- printf("\nInvalid option\n");
- }
- }
- return 0;
- }
- // A utility function to create a new BST node
- struct node* newNode(int code,char name[MAX],int price, int amount, char dateRec[MAX], char dateExp[MAX])
- {
- struct node* temp
- = (struct node*)malloc(sizeof(struct node));
- temp->code = code;
- strcpy(temp->name, name);
- temp->price = price;
- temp->amount = amount;
- strcpy(temp->dateRec, dateRec);
- strcpy(temp->dateExp, dateExp);
- temp->left = temp->right = NULL;
- return temp;
- }
- /* A utility function to
- insert a new node with given code in
- * BST */
- struct node* insert(struct node* node, int code,char name[MAX],int price, int amount, char dateRec[MAX], char dateExp[MAX])
- {
- /* If the tree is empty, return a new node */
- if (node == NULL)
- return newNode(code, name, price, amount, dateRec, dateExp);
- /* Otherwise, recur down the tree */
- if (code < node->code)
- node->left = insert(node->left, code, name, price, amount, dateRec, dateExp);
- else
- node->right = insert(node->right, code, name, price, amount, dateRec, dateExp);
- /* return the (unchanged) node pointer */
- return node;
- }
- /* Given a non-empty binary search
- tree, return the node
- with minimum code value found in
- that tree. Note that the
- entire tree does not need to be searched. */
- struct node* minValueNode(struct node* node)
- {
- struct node* current = node;
- /* loop down to find the leftmost leaf */
- while (current && current->left != NULL)
- current = current->left;
- return current;
- }
- /* Given a binary search tree
- and a code, this function
- deletes the code and
- returns the new root */
- struct node* deleteNode(struct node* root, int code)
- {
- // base case
- if (root == NULL)
- return root;
- // If the code to be deleted
- // is smaller than the root's
- // code, then it lies in left subtree
- if (code < root->code)
- root->left = deleteNode(root->left, code);
- // If the code to be deleted
- // is greater than the root's
- // code, then it lies in right subtree
- else if (code > root->code)
- root->right = deleteNode(root->right, code);
- // if code is same as root's code,
- // then This is the node
- // to be deleted
- else {
- // node with only one child or no child
- if (root->left == NULL) {
- struct node* temp = root->right;
- free(root);
- return temp;
- }
- else if (root->right == NULL) {
- struct node* temp = root->left;
- free(root);
- return temp;
- }
- // node with two children:
- // Get the inorder successor
- // (smallest in the right subtree)
- struct node* temp = minValueNode(root->right);
- // Copy the inorder
- // successor's content to this node
- root->code = temp->code;
- // Delete the inorder successor
- root->right = deleteNode(root->right, temp->code);
- }
- return root;
- }
- void printInOrder(struct node* root){
- // left, root, right
- if (root != NULL){
- printInOrder(root->left);
- printf("%c ", root->code);
- printInOrder(root->right);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement