Skip to main content

Create Binary Search Tree(BST).Find height of the tree and print leaf nodes. Find mirror image, print original and mirror image using level-wise printing.

CODE:


 #include <iostream>

#include <queue>

using namespace std;


// Define the structure for a tree node

struct TreeNode {

    int data;

    TreeNode* left;

    TreeNode* right;

};


// Function to create a new node

TreeNode* createNode(int value) {

    TreeNode* newNode = new TreeNode();

    newNode->data = value;

    newNode->left = nullptr;

    newNode->right = nullptr;

    return newNode;

}


// Function to insert a node into the BST

TreeNode* insert(TreeNode* root, int value) {

    if (root == nullptr) {

        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;

}


// Function to find the height of the tree

int height(TreeNode* root) {

    if (root == nullptr) {

        return -1;

    }

    int leftHeight = height(root->left);

    int rightHeight = height(root->right);

    return 1 + max(leftHeight, rightHeight);

}


// Function to print leaf nodes

void printLeafNodes(TreeNode* root) {

    if (root == nullptr) {

        return;

    }

    if (root->left == nullptr && root->right == nullptr) {

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

    }

    printLeafNodes(root->left);

    printLeafNodes(root->right);

}


// Function to swap left and right children of a tree

void mirror(TreeNode* root) {

    if (root == nullptr) {

        return;

    }

    mirror(root->left);

    mirror(root->right);

    TreeNode* temp = root->left;

    root->left = root->right;

    root->right = temp;

}


// Function to print the tree level-wise

void printLevelOrder(TreeNode* root) {

    if (root == nullptr)

        return;

    queue<TreeNode*> q;

    q.push(root);

    while (!q.empty()) {

        int nodeCount = q.size();

        while (nodeCount > 0) {

            TreeNode* node = q.front();

            q.pop();

            cout << node->data << " ";

            if (node->left != nullptr)

                q.push(node->left);

            if (node->right != nullptr)

                q.push(node->right);

            nodeCount--;

        }

        cout << endl;

    }

}


int main() {

    TreeNode* root = nullptr;

    int n;

    cout << "Enter the number of nodes in the BST: ";

    cin >> n;

    cout << "Enter the values of nodes: ";

    for (int i = 0; i < n; ++i) {

        int value;

        cin >> value;

        root = insert(root, value);

    }

    cout << "Height of the BST: " << height(root) << endl;

    cout << "Leaf nodes: ";

    printLeafNodes(root);

    cout << endl;

    cout << "Original Tree:" << endl;

    printLevelOrder(root);

    mirror(root);

    cout << "Mirror Image:" << endl;

    printLevelOrder(root);

    return 0;

}






Popular posts from this blog

krushnaaa

  import java . awt .* ; import java . awt . event .* ; public class SimpleAWTExample {     public static void main ( String [] args ) {         // Create a Frame         Frame frame = new Frame ( "Simple AWT Example" );         // Create a Button         Button button = new Button ( "Click Me!" );         // Set layout for the Frame         frame . setLayout ( new FlowLayout ());         // Add the Button to the Frame         frame . add ( button );         // Set size of the Frame         frame . setSize ( 300 , 200 ); // Width: 300 pixels, Height: 200 pixels         // Make the Frame visible         frame . setVisible ( true );         // Add a WindowListener to handle closing event     ...

Write a program to create a class Student2 along with two method getData (), printData () to get the value through argument and display the data in printData. Create the two objects s1, s2 to declare and access the values from class STtest.

 CODE: import java.util.Scanner; class Student2 {     private String name;     private int age;          // Method to set data     public void getData(String name, int age) {         this.name = name;         this.age = age;     }          // Method to print data     public void printData() {         System.out.println("Name: " + name);         System.out.println("Age: " + age);     } } public class STtest {     public static void main(String[] args) {         Scanner scanner = new Scanner(System.in);                  // Creating objects of Student2 class         Student2 s1 = new Student2();         Student2 s2 = new Student2();              ...

Create an in-order threaded binary search tree and perform the traversals.(Double Thread)

 CODE: #include <iostream> using namespace std; struct Node {     int data;     Node* left;     Node* right;     bool leftThread;     bool rightThread; }; Node* createThreadedBST(int arr[], int n) {     Node* root = nullptr;     for (int i = 0; i < n; ++i) {         Node* newNode = new Node;         newNode->data = arr[i];         newNode->left = newNode->right = nullptr;         newNode->leftThread = newNode->rightThread = true;         if (root == nullptr) {             root = newNode;         } else {             Node* current = root;             Node* parent = nullptr;             while (true) {                ...