Tuesday, 3 November 2026

Write a Program PreOrder, PostOrder, InOrder Traversal

#include <iostream>

using namespace std;


struct Node

{

    int data;

    Node *left;

    Node *right;

};


// Create a new node

Node* createNode(int value)

{

    Node *newNode = new Node;


    newNode->data = value;

    newNode->left = NULL;

    newNode->right = NULL;


    return newNode;

}


// Preorder Traversal (Root, Left, Right)

void preorder(Node *root)

{

    if (root != NULL)

    {

        cout << root->data << " ";

        preorder(root->left);

        preorder(root->right);

    }

}


// Inorder Traversal (Left, Root, Right)

void inorder(Node *root)

{

    if (root != NULL)

    {

        inorder(root->left);

        cout << root->data << " ";

        inorder(root->right);

    }

}


// Postorder Traversal (Left, Right, Root)

void postorder(Node *root)

{

    if (root != NULL)

    {

        postorder(root->left);

        postorder(root->right);

        cout << root->data << " ";

    }

}


int main()

{

    // Create a tree

    Node *root = createNode(10);


    root->left = createNode(20);

    root->right = createNode(30);


    root->left->left = createNode(40);

    root->left->right = createNode(50);


    root->right->left = createNode(60);

    root->right->right = createNode(70);


    cout << "\nPreorder Traversal: ";

    preorder(root);


    cout << "\nInorder Traversal: ";

    inorder(root);


    cout << "\nPostorder Traversal: ";

    postorder(root);


    return 0;

}



Write a Program Implementation of a BST ,Add,Search ,Min,Max

 

#include <iostream>

using namespace std;


struct Node

{

    int data;

    Node *left;

    Node *right;

};


// Create a new node

Node* createNode(int value)

{

    Node *newNode = new Node;

    newNode->data = value;

    newNode->left = NULL;

    newNode->right = NULL;


    return newNode;

}


// Insert a node

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;

}


// Search an element

void search(Node *root, int key)

{

    if (root == NULL)

    {

        cout << "Element not found";

        return;

    }


    if (root->data == key)

    {

        cout << "Element found";

    }

    else if (key < root->data)

    {

        search(root->left, key);

    }

    else

    {

        search(root->right, key);

    }

}


// Find minimum value

int findMin(Node *root)

{

    while (root->left != NULL)

    {

        root = root->left;

    }


    return root->data;

}


// Find maximum value

int findMax(Node *root)

{

    while (root->right != NULL)

    {

        root = root->right;

    }


    return root->data;

}


// Display BST using Inorder

void inorder(Node *root)

{

    if (root != NULL)

    {

        inorder(root->left);

        cout << root->data << " ";

        inorder(root->right);

    }

}


int main()

{

    Node *root = NULL;

    int choice, value;


    do

    {

        cout << "\n\n--- BINARY SEARCH TREE ---";

        cout << "\n1. Insert";

        cout << "\n2. Search";

        cout << "\n3. Minimum";

        cout << "\n4. Maximum";

        cout << "\n5. Display";

        cout << "\n6. Exit";


        cout << "\nEnter your choice: ";

        cin >> choice;


        switch (choice)

        {

            case 1:

                cout << "Enter value: ";

                cin >> value;

                root = insert(root, value);

                cout << "Node inserted";

                break;


            case 2:

                cout << "Enter value to search: ";

                cin >> value;

                search(root, value);

                break;


            case 3:

                if (root == NULL)

                    cout << "Tree is empty";

                else

                    cout << "Minimum value = " << findMin(root);

                break;


            case 4:

                if (root == NULL)

                    cout << "Tree is empty";

                else

                    cout << "Maximum value = " << findMax(root);

                break;


            case 5:

                cout << "BST Elements: ";

                inorder(root);

                break;


            case 6:

                cout << "Program ended";

                break;


            default:

                cout << "Invalid choice";

        }


    } while (choice != 6);


    return 0;

}


Write a Program Implementation of a BST Add

 Program to Add (Insert) a Node in BST

#include <iostream>

using namespace std;

struct Node {

    int data;

    Node *left, *right;

 //Stores the address of the left child and right child

};


Node* createNode(int value) //Creates a function to make a new node.

{

    Node* newNode = new Node();// Allocates memory for a new node

    newNode->data = value;//Stores the given value.

    newNode->left = NULL;//Initially, the left child is empty.

    newNode->right = NULL;//Initially, the right child is empty.

    return newNode;//Returns the address of the new node

}


Node* add(Node* root, int value) {

    if (root == NULL) {

        return createNode(value);

    }

  • Node* – The function returns the address of a node.

  • add – Function name.

  • Node* root – Receives the address of the root node.

  • int value – Receives the value that we want to insert.

      It checks whether the current node is empty or not.

If root == NULL, it means there is no node at that position.

So, we can insert a new node there.

    if (value < root->data) {

        root->left = add(root->left, value);

    }

    else if (value > root->data) {

        root->right = add(root->right, value);

    }


    return root;

}


void inorder(Node* root) {

    if (root != NULL) {

        inorder(root->left);

        cout << root->data << " ";

        inorder(root->right);

    }

}

It checks whether the current node exists or not.

  • If root is not NULL, execute the statements inside.

  • If root is NULL, stop that function call.


Inorder = Left → Root → Right 

int main() {

    Node* root = NULL;

   

  root = add(root, 50);

    root = add(root, 30);

    root = add(root, 70);

    root = add(root, 20);

    root = add(root, 40);

     cout << "BST after insertion: ";

    inorder(root);

    return 0;

}


Thursday, 29 October 2026

To display the contents in reverse order of doubly linked list.

 #include <iostream>

using namespace std;


struct Node

{

    int data;

    Node *prev;

    Node *next;

};

int main()

{

    Node *head, *temp;


    // Creating nodes

    Node *n1 = new Node{10, NULL, NULL};

    Node *n2 = new Node{20, NULL, NULL};

    Node *n3 = new Node{30, NULL, NULL};

    Node *n4 = new Node{40, NULL, NULL};


    head = n1;


    n1->next = n2;

    n2->prev = n1;


    n2->next = n3;

    n3->prev = n2;


    n3->next = n4;

    n4->prev = n3;


    // Move to last node

    temp = head;


    while (temp->next != NULL)

    {

        temp = temp->next;

    }


    // Display in reverse order

    cout << "Reverse List: ";


    while (temp != NULL)

    {

        cout << temp->data << " ";

        temp = temp->prev;

    }


    return 0;

}

To delete a node from the end of a doubly linked list.

 #include <iostream>

using namespace std;


struct Node

{

    int data;

    Node *prev;

    Node *next;

};


int main()

{

    Node *head, *temp;


    // Creating nodes

    Node *n1 = new Node{10, NULL, NULL};

data – Stores the value.

prev – Stores the address of the previous node.

next – Stores the address of the next node.


    Node *n2 = new Node{20, NULL, NULL};

    Node *n3 = new Node{30, NULL, NULL};


    head = n1;


    n1->next = n2;

    n2->prev = n1;


    n2->next = n3;

    n3->prev = n2;


    // Display original list

    cout << "Before Deletion: ";

    temp = head;


    while (temp != NULL)

    {

        cout << temp->data << " ";

        temp = temp->next;

    }


    // Delete last node

    temp = head;


    while (temp->next != NULL)

    {

        temp = temp->next;

    }


    temp->prev->next = NULL;


    delete temp;


    // Display updated list

    cout << "\nAfter Deletion: ";

    temp = head;


    while (temp != NULL)

    {

        cout << temp->data << " ";

        temp = temp->next;

    }


    return 0;

}