Data Structure Slip No_6A

#include <stdio.h>

#include <stdlib.h>

 

// Definition of a Node

struct Node

{

    int data;

    struct Node* left;

    struct Node* right;

};

 

// Function to create a new node

struct Node* createNode(int value)

{

    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

    newNode->data = value;

    newNode->left = NULL;

    newNode->right = NULL;

    return newNode;

}

 

// Function to insert a value into the BST

struct Node* insert(struct 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;

}

 

// Preorder traversal

void preorder(struct Node* root)

 {

    if (root != NULL)

    {

        printf("%d ", root->data);

        preorder(root->left);

        preorder(root->right);

    }

}

 

// Postorder traversal

void postorder(struct Node* root)

{

    if (root != NULL)

    {

        postorder(root->left);

        postorder(root->right);

        printf("%d ", root->data);

    }

}

 

void main()

{

    struct Node* root = NULL;

    int choice, value;

 

    while (1) {

        printf("\nMenu:\n");

        printf("1. Create a Binary Search Tree\n");

        printf("2. Traverse using Preorder\n");

        printf("3. Traverse using Postorder\n");

        printf("4. Exit\n");

        printf("Enter your choice: ");

        scanf("%d", &choice);

 

        switch (choice)

{

            case 1:

                printf("Enter the value to insert: ");

                scanf("%d", &value);

                root = insert(root, value);

                break;

            case 2:

                printf("Preorder Traversal: ");

                preorder(root);

                printf("\n");

                break;

            case 3:

                printf("Postorder Traversal: ");

                postorder(root);

                printf("\n");

                break;

            case 4:

                exit(0);

            default:

                printf("Invalid choice! Please select again.\n");

        }

    }

} 


Output:

Menu:

1. Create a Binary Search Tree

2. Traverse using Preorder

3. Traverse using Postorder

4. Exit

Enter your choice: 1

Enter the value to insert: 12

Menu:

1. Create a Binary Search Tree

2. Traverse using Preorder

3. Traverse using Postorder

4. Exit

Enter your choice: 1

Enter the value to insert: 52

Menu:

1. Create a Binary Search Tree

2. Traverse using Preorder

3. Traverse using Postorder

4. Exit

Enter your choice: 1

Enter the value to insert: 4

Menu:

1. Create a Binary Search Tree

2. Traverse using Preorder

3. Traverse using Postorder

4. Exit

Enter your choice: 2

Preorder Traversal: 12 4 52 

Menu:

1. Create a Binary Search Tree

2. Traverse using Preorder

3. Traverse using Postorder

4. Exit

Enter your choice: 3

Postorder Traversal: 4 52 12 

No comments:

Post a Comment