কম্পিউটার

C++ এ একটি বাইনারি ট্রিকে এর মিরর ট্রিতে রূপান্তর করুন


এই টিউটোরিয়ালে, আমরা একটি বাইনারি গাছকে তার মিরর ট্রিতে রূপান্তর করার একটি প্রোগ্রাম নিয়ে আলোচনা করব।

এই জন্য, আমাদের একটি বাইনারি গাছ প্রদান করা হবে. আমাদের কাজ হবে প্রদত্ত বাইনারি ট্রি থেকে একটি মিরর ট্রি তৈরি করা বাম এবং ডান প্রান্তের মানগুলিকে অদলবদল করা৷

উদাহরণ

#include<bits/stdc++.h>
using namespace std;
//binary tree node structure
struct Node{
   int data;
   struct Node* left;
   struct Node* right;
};
//creation of a new node with no child nodes
struct Node* newNode(int data){
   struct Node* node = (struct Node*)malloc(sizeof(struct Node));
   node->data = data;
   node->left = NULL;
   node->right = NULL;
   return(node);
}
void mirror(struct Node* node){
   if (node == NULL)
      return;
   else{
      struct Node* temp;
      //swapping the subtrees
      mirror(node->left);
      mirror(node->right);
      temp = node->left;
      node->left = node->right;
      node->right = temp;
   }
}
//printing the inorder traversal
void print_tree(struct Node* node){
   if (node == NULL)
      return;
   print_tree(node->left);
   cout << node->data << " ";
   print_tree(node->right);
}
int main(){
   struct Node *root = newNode(1);
   root->left = newNode(2);
   root->right = newNode(3);
   root->left->left = newNode(4);
   root->left->right = newNode(5);
   //printing the initial tree
   cout << "Inorder traversal of the constructed" << endl;
   print_tree(root);
   mirror(root);
   //printing the mirror tree
   cout << "\nInorder traversal of the mirror tree" << endl;
   print_tree(root);
   return 0;
}

আউটপুট

Inorder traversal of the constructed
4 2 5 1 3
Inorder traversal of the mirror tree
3 1 5 2 4

  1. C++ এ বাইনারি ট্রি প্রিন্ট করুন

  2. সাজানো তালিকাকে C++ এ বাইনারি সার্চ ট্রিতে রূপান্তর করুন

  3. C++ এ বাইনারি ট্রি প্রুনিং

  4. C++ এ একটি বাইনারি সার্চ ট্রিতে ঢোকান