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

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