20.11.25

A Friendship Story of Learning Data Structures with C



Sr. No. DSU BLOGS CHAPTERS
1Array Operations in C, The Group of Friendship (Create, Insert, Delete & Display)
2Searching Through the Squad, Linear Search in C 
3Linear Search in C (with Yes/No Continue Option)
4Linear Search on Words in C
5Binary Search, When Friends Divide and Conquer!
6Binary Search on Strings, When Friends Go Alphabetical!
7Bubble Sort, The One Where Friends Make Numbers “Bubble Up” 
8Bubble Sort (Strings), The One Where Friends Alphabetize the Fun Way 
9Selection Sort, The One Where Friends Pick the Smallest Numbers First 
10Selection Sort (Strings), The One Where Friends Compete to Find the Smallest Word 
11Insertion Sort, The One Where Friends Arrange Cards Like Pros 
12Insertion Sort (Strings), The One Where Friends Alphabetize Like Card Players 
13Linked List Operations, The One Where Friends Build a Chain Together 
14Linked List Operations, The One Where Friends Build and Break the Chain 
15Polynomial Representation, The One Where Friends Turn Math into Linked Lists 
16Adding Two Polynomials, The One Where Friends Combine Their Math Powers 
17Stack Using Array  The One Where Friends Follow the “Last In, First Out” Rule 
18Stack Using Linked List The One Where Friends Keep Pushing and Popping Everything 
19Multiplication Using Recursion The One Where Rahul Thinks He Invented Maths
20Reverse String Using Recursion The One Where Aarav Thinks He’s a DJ
21Reverse Traversal of Linked List The One Where Aarav Wants a “Back Button” in Real Life
22Linear Queue Using Array The One Where Aarav Keeps Jumping the Line
23Linear Queue Using Linked List The One Where Rahul Says “Dynamic Problems Need Dynamic Solutions”
24Circular Queue Using Array The One Where Rahul Says “Dude, Stop Wasting Memory!”
25Circular Queue Using Linked List The One Where Our Queue Turns Into a Never-Ending Circle
26Priority Queue Using Linked List The One Where Friends Let “Priority Decide Everything” 
27Binary Search Tree (BST) The One Where Rahul Thinks Trees Can Sort Themselves
28BST Traversals The One Where We Explore the Tree in Every Possible Direction 

18.11.25

28. BST Traversals The One Where We Explore the Tree in Every Possible Direction


hello everyone 

So after we finished building the Binary Search Tree, Aarav confidently said,
“Traversal? Easy. It’s just printing stuff.” Rahul looked at him like he just failed recursion 101 and replied, “Bro… there are THREE types of tree traversals. THREE. Try not to break the tree.” And that’s how today’s adventure of Preorder, Inorder, and Postorder began.

What’s Happening Here?

We’re still working with the same BST, but now we’re exploring it in different ways:

Preorder (Root - Left - Right)

Rahul took this one because he likes being first everywhere . He literally starts by printing the node itself before checking anything else like entering a room and announcing, “Rahul has arrived!”

Inorder (Left - Root - Right)

Aarav said this one is “the most peaceful” because it gives sorted output. He liked it because he doesn’t need to think, just go left then root then right. Simple enough for him .

Postorder (Left - Right - Root)

I (Daksh) handled this one. Why? Because I always clean up last and Postorder prints the node at the end. Left subtree, right subtree, and THEN the node. Perfect for someone who finishes everything “later” .

How We Divided the Work

Rahul: Did Preorder instantly
“ROOT first, everything else later. Just like my priorities.”

Aarav: Showed off Inorder 
“Sorted output, thank you very much.”

Me (Daksh): Printed Postorder 
“This feels like rewriting an exam last minute.

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 preorderTraversal(struct node *rootNode) {
     if (rootNode != NULL) {
         printf("%d ", rootNode->dataValue);
         preorderTraversal(rootNode->leftChild);
         preorderTraversal(rootNode->rightChild);
     }
 }

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

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

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

     do {
         printf("\n1. Insert\n2. Preorder\n3. Inorder\n4. Postorder\nEnter choice: ");
         scanf("%d", &userChoice);

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

             case 2:
                 printf("Preorder Traversal: ");
                 preorderTraversal(rootNode);
                 printf("\n");
                 break;

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

             case 4:
                 printf("Postorder Traversal: ");
                 postorderTraversal(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

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 →

26. Priority Queue Using Linked List The One Where Friends Let “Priority Decide Everything”

 

hello everyone

So today’s coding session wasn’t normal because Aarav wanted everything to be first, Rahul wanted only high-priority items, and I (Daksh) just watched them fight while writing the program  And that’s exactly how we ended up learning Priority Queue using Linked List.

What’s Going On Here?

A Priority Queue is like real life the most important (or smallest numbered priority) thing gets done first. Not like Aarav, who thinks he should always go first even when his priority is 100 .

We use a linked list so that:

  • each node stores a value and its priority,

  • new elements are inserted in sorted priority order,

  • deletion always removes the highest priority element,

  • and the list grows easily using malloc.

How It Works

Rahul took over insertions he made sure every new element was placed exactly at the right priority spot, and honestly he was too strict about it “Low priority? Back of the line!” he kept saying. Aarav handled deletions and of course, he only removed the one with the highest priority, bragging every time he “deleted responsibly” I (Daksh) kept displaying the queue after each operation, like: “Here’s your Priority Queue, sorted and drama-free unlike our group decisions.” With all three of us, the priority queue behaved perfectly more perfectly than our plans ever do 

Code:

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

 struct node {
     int dataValue;
     int priorityValue;
     struct node *nextNode;
 };

 struct node *frontNode = NULL;

 void insertElement(int value, int priority) {
     struct node *temporaryNode = (struct node*)malloc(sizeof(struct node));
     temporaryNode->dataValue = value;
     temporaryNode->priorityValue = priority;
     temporaryNode->nextNode = NULL;

     if (frontNode == NULL || priority < frontNode->priorityValue) {
         temporaryNode->nextNode = frontNode;
         frontNode = temporaryNode;
     } else {
         struct node *currentNode = frontNode;
         while (currentNode->nextNode != NULL &&
                currentNode->nextNode->priorityValue <= priority) {
             currentNode = currentNode->nextNode;
         }
         temporaryNode->nextNode = currentNode->nextNode;
         currentNode->nextNode = temporaryNode;
     }
     printf("Inserted %d with priority %d\n", value, priority);
 }

 void deleteElement() {
     if (frontNode == NULL) {
         printf("Queue Underflow! No element to delete.\n");
         return;
     }
     struct node *temporaryNode = frontNode;
     printf("Deleted %d (Priority %d)\n",
            temporaryNode->dataValue,
            temporaryNode->priorityValue);
     frontNode = frontNode->nextNode;
     free(temporaryNode);
 }

 void displayQueue() {
     if (frontNode == NULL) {
         printf("Queue is empty.\n");
         return;
     }
     struct node *currentNode = frontNode;
     printf("Priority Queue: ");
     while (currentNode != NULL) {
         printf("(%d, P=%d) ",
                currentNode->dataValue,
                currentNode->priorityValue);
         currentNode = currentNode->nextNode;
     }
     printf("\n");
 }

 int main() {
     int userChoice;
     int elementValue;
     int elementPriority;
     char userDecision;

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

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

             case 2:
                 deleteElement();
                 break;

             case 3:
                 displayQueue();
                 break;

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

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

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

     printf("Program ended.\n");
     return 0;
    }

Output:



  

← Previous 🏠 Homepage Next Chapter →

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 ...