18.11.25

27. Binary Search Tree (BST) The One Where Rahul Thinks Trees Can Sort Themselves

  

hello everyone

Today’s coding session started with Rahul saying, “Bro, why do we even need sorting? The tree will do it for us.” Aarav rolled his eyes so hard he almost saw his brain. And I (Daksh)… well, I just quietly opened my laptop because I knew drama + data structures = perfect blog material. And that’s how we ended up learning Binary Search Trees.

What’s Going On Here?

A Binary Search Tree (BST) is that one organized friend who keeps everything in the right place.

Every node follows 3 golden rules:

  1. Left child has smaller value

  2. Right child has larger value

  3. And the tree stays sorted just by inserting things properly

Rahul kept saying, “Bro this is magical, it’s sorting itself!” Aarav corrected him: “No Rahul… YOU are sorting it by inserting it correctly.” Rahul didn’t listen.

How It Works

Insert Operation Rahul Style

Rahul was in charge of insertion.
Every time a number came in, he’d decide:

  • “Small? Go left.”

  • “Big? Go right.”

  • “Equal? Also right. No arguments.”

He behaved like the bouncer outside a club, sending numbers left or right without hesitation. Inorder Traversal  Aarav’s Favorite Moment Aarav proudly explained: “Inorder traversal (Left - Root - Right) gives sorted output. Because nature likes order. And Rahul doesn’t.” And that’s exactly what happened the moment we printed inorder traversal, the numbers appeared perfectly sorted. Rahul gasped like he saw a ghost. I (Daksh) Just Observed Everything I kept printing messages like: “Here’s your BST, sorted and peaceful unlike our group WhatsApp chat.” Honestly, the tree handled order better than we ever will.

Code:

 #define _CRT_SECURE_NO_WARNINGS
 #include <stdio.h>
 #include <stdlib.h>

 struct node {
     int dataValue;
     struct node *leftChild;
     struct node *rightChild;
 };

 struct node* createNode(int value) {
     struct node *newNode = (struct node*)malloc(sizeof(struct node));
     newNode->dataValue = value;
     newNode->leftChild = NULL;
     newNode->rightChild = NULL;
     return newNode;
 }

 struct node* insertElement(struct node *rootNode, int value) {
     if (rootNode == NULL) {
         return createNode(value);
     }
     if (value < rootNode->dataValue) {
         rootNode->leftChild = insertElement(rootNode->leftChild, value);
     } else {
         rootNode->rightChild = insertElement(rootNode->rightChild, value);
     }
     return rootNode;
 }

 void inorderTraversal(struct node *rootNode) {
     if (rootNode != NULL) {
         inorderTraversal(rootNode->leftChild);
         printf("%d ", rootNode->dataValue);
         inorderTraversal(rootNode->rightChild);
     }
 }

 int main() {
     struct node *rootNode = NULL;
     int userChoice, elementValue;
     char userDecision;

     do {
         printf("\n1. Insert\n2. Inorder Traversal\nEnter your choice: ");
         scanf("%d", &userChoice);

         switch (userChoice) {
             case 1:
                 printf("Enter value to insert: ");
                 scanf("%d", &elementValue);
                 rootNode = insertElement(rootNode, elementValue);
                 break;

             case 2:
                 printf("Inorder Traversal: ");
                 inorderTraversal(rootNode);
                 printf("\n");
                 break;

             default:
                 printf("Invalid choice.\n");
         }

         printf("Do you want to continue? (y/n): ");
         scanf(" %c", &userDecision);

     } while (userDecision == 'y' || userDecision == 'Y');

     return 0;
 }


Output:



  

← Previous 🏠 Homepage Next Chapter →

No comments:

Post a Comment

rating System

Loading...

A Friendship Story of Learning Data Structures with C

Sr. No. DSU BLOGS CHAPTERS 1 Array Operations in C, The Group of Friendship (Create, Insert, Delete ...