Created
November 20, 2025 05:16
-
-
Save sortira/c06bf083e66c1f9586e1b83d81e09743 to your computer and use it in GitHub Desktop.
bst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| typedef struct node | |
| { | |
| int data; | |
| struct node *left; | |
| struct node *right; | |
| } Node; | |
| Node *createNode(int value) | |
| { | |
| Node *newNode = (Node *)malloc(sizeof(Node)); | |
| newNode->data = value; | |
| newNode->left = newNode->right = NULL; | |
| return newNode; | |
| } | |
| Node *findMinValueNode(Node *node) | |
| { | |
| Node *current = node; | |
| while (current && current->left != NULL) | |
| { | |
| current = current->left; | |
| } | |
| return current; | |
| } | |
| Node *insert(Node *root, int value) | |
| { | |
| if (root == NULL) | |
| { | |
| return createNode(value); | |
| } | |
| if (value < root->data) | |
| { | |
| root->left = insert(root->left, value); | |
| } | |
| else if (value > root->data) | |
| { | |
| root->right = insert(root->right, value); | |
| } | |
| return root; | |
| } | |
| Node *deleteNode(Node *root, int key) | |
| { | |
| if (root == NULL) | |
| { | |
| printf("Node with key %d not found.\n", key); | |
| return root; | |
| } | |
| if (key < root->data) | |
| { | |
| root->left = deleteNode(root->left, key); | |
| } | |
| else if (key > root->data) | |
| { | |
| root->right = deleteNode(root->right, key); | |
| } | |
| else | |
| { | |
| if (root->left == NULL) | |
| { | |
| Node *temp = root->right; | |
| free(root); | |
| return temp; | |
| } | |
| else if (root->right == NULL) | |
| { | |
| Node *temp = root->left; | |
| free(root); | |
| return temp; | |
| } | |
| Node *temp = findMinValueNode(root->right); | |
| root->data = temp->data; | |
| root->right = deleteNode(root->right, temp->data); | |
| } | |
| return root; | |
| } | |
| void inorderTraversal(Node *root) | |
| { | |
| if (root != NULL) | |
| { | |
| inorderTraversal(root->left); | |
| printf("%d ", root->data); | |
| inorderTraversal(root->right); | |
| } | |
| } | |
| int main() | |
| { | |
| Node *root = NULL; | |
| int choice, value; | |
| printf("--- Binary Search Tree Menu ---\n"); | |
| do | |
| { | |
| printf("\n1. Insert Node\n"); | |
| printf("2. Delete Node\n"); | |
| printf("3. Display (Inorder Traversal)\n"); | |
| printf("4. Exit\n"); | |
| printf("Enter your choice: "); | |
| if (scanf("%d", &choice) != 1) | |
| { | |
| while (getchar() != '\n') | |
| ; | |
| choice = -1; | |
| } | |
| switch (choice) | |
| { | |
| case 1: | |
| printf("Enter value to insert: "); | |
| scanf("%d", &value); | |
| root = insert(root, value); | |
| printf("Value %d inserted.\n", value); | |
| break; | |
| case 2: | |
| printf("Enter value to delete: "); | |
| scanf("%d", &value); | |
| root = deleteNode(root, value); | |
| break; | |
| case 3: | |
| printf("Inorder Traversal (Sorted): "); | |
| if (root == NULL) | |
| { | |
| printf("The tree is empty."); | |
| } | |
| else | |
| { | |
| inorderTraversal(root); | |
| } | |
| printf("\n"); | |
| break; | |
| case 4: | |
| printf("Exiting program. Goodbye!\n"); | |
| break; | |
| default: | |
| printf("Invalid choice. Please try again.\n"); | |
| } | |
| } while (choice != 4); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment