এই টিউটোরিয়ালে, আমরা প্রদত্ত বাইনারি ট্রি প্রতিফলিত করতে যাচ্ছি।
আসুন সমস্যা সমাধানের পদক্ষেপগুলি দেখি৷
৷-
একটি স্ট্রাকট নোড লিখুন।
-
ডামি ডেটা দিয়ে বাইনারি ট্রি তৈরি করুন।
-
প্রদত্ত বাইনারি গাছের আয়না খুঁজে বের করার জন্য একটি পুনরাবৃত্ত ফাংশন লিখুন।
-
বারবার বাম এবং ডান নোড সহ ফাংশনটি কল করুন৷
-
ডান নোড ডেটা দিয়ে বাম নোড ডেটা অদলবদল করুন৷
৷
-
-
গাছ প্রিন্ট করুন।
উদাহরণ
আসুন কোডটি দেখি।
#include<bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* left;
struct Node* right;
};
struct Node* newNode(int data) {
struct Node* node = new Node;
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
}
void convertTreeToItsMirror(struct Node* node) {
if (node == NULL) {
return;
}
else {
struct Node* temp;
convertTreeToItsMirror(node->left);
convertTreeToItsMirror(node->right);
temp = node->left;
node->left = node->right;
node->right = temp;
}
}
void printTree(struct Node* node) {
if (node == NULL) {
return;
}
printTree(node->left);
cout << node->data << " ";
printTree(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);
cout << "Tree: ";
printTree(root);
cout << endl;
convertTreeToItsMirror(root);
cout << "Mirror of the Tree: ";
printTree(root);
cout << endl;
return 0;
} আউটপুট
আপনি যদি উপরের কোডটি চালান, তাহলে আপনি নিম্নলিখিত ফলাফল পাবেন।
Tree: 4 2 5 1 3 Mirror of the Tree: 3 1 5 2 4
উপসংহার
টিউটোরিয়ালে আপনার কোন প্রশ্ন থাকলে মন্তব্য বিভাগে উল্লেখ করুন।