প্রদত্ত বাইনারি ট্রির ক্ষেত্রে, এটিকে বাইনারি সার্চ ট্রিতে এমনভাবে রূপান্তর করুন যাতে বাইনারি ট্রির মূল গঠন অক্ষুণ্ন থাকে।
C++ STL-এর সেটগুলি অ্যারে ভিত্তিক সমাধানের পরিবর্তে এই সমাধান দ্বারা ব্যবহার করা হবে।
উদাহরণ
উদাহরণ 1
ইনপুট
11 / \ 3 8 / \ 9 5
আউটপুট
9 / \ 5 11 / \ 3 8
উদাহরণ 2
ইনপুট
11 / \ 31 16 / \ 21 6
আউটপুট
16 / \ 11 21 / \ 6 31
সমাধান
-
ইনঅর্ডার ট্রাভার্সাল করার সময় আমাদের একটি সেটে বাইনারি গাছের আইটেমগুলি কপি করতে হবে। এটি O(n log n) সময় ব্যয় করে। উল্লেখ্য যে C++ STL(স্ট্যান্ডার্ড টেমপ্লেট লাইব্রেরি) এ সেটটি একটি স্ব-ভারসাম্যপূর্ণ বাইনারি অনুসন্ধান ট্রি যেমন রেড ব্ল্যাক ট্রি, এভিএল ট্রি ইত্যাদি ব্যবহার করে প্রয়োগ করা হয়।
-
সেট বাছাই করার কোন প্রয়োজন নেই কারণ C++-এ সেটগুলি ব্যবহার করা হয় স্ব-ভারসাম্য রক্ষাকারী বাইনারি সার্চ ট্রি বাস্তবায়নের জন্য যার কারণে প্রতিটি অপারেশন যেমন সন্নিবেশ, অনুসন্ধান, মুছে ফেলা ইত্যাদি O(log n) সময় ব্যয় করে।
-
এখন আমরা সহজেই গাছের ক্রমানুসারে ট্রাভার্সাল করার সময় ট্রি থেকে শুরু করে একের পর এক সেটের উপাদানগুলি কপি করতে পারি। সেটের প্রতিটি আইটেমকে শুরু থেকে কপি করার সময় সতর্কতা অবলম্বন করা উচিত, আমরা প্রথমে ইনঅর্ডার ট্রাভার্সাল করার সময় এটিকে গাছে কপি করি, তারপর সেট থেকেও মুছে ফেলি।
-
বর্তমানে উপরে উল্লিখিত সমাধানটি এখানে ব্যাখ্যা করা বাইনারি ট্রি থেকে বাইনারি সার্চ ট্রিতে অ্যারে ভিত্তিক রূপান্তরের চেয়ে সহজ এবং কার্যকর করা সহজ৷
সেট ব্যবহার করে একটি বাইনারি ট্রিকে বাইনারি সার্চ ট্রি (BST) তে রূপান্তর করার নিম্নলিখিত প্রোগ্রামটি এখানে ব্যাখ্যা করা হয়েছে৷
উদাহরণ
/* CPP program for converting a Binary tree to BST implementing sets as containers. */
#include <bits/stdc++.h>
using namespace std;
struct Node1 {
int data;
struct Node1 *left, *right;
};
// function for storing the nodes in set while performing inorder traversal.
void storeinorderInSet(Node1* root1, set<int>& s){
if (!root1)
return;
// Left subtree is visited first
storeinorderInSet(root1->left, s);
Order of O(logn) for sets is taken by insertion
s.insert(root1->data);
// We visit the right subtree
storeinorderInSet(root1->right, s);
} // Time complexity = O(nlogn)
// function for copying elements of set one by one to the tree while performing inorder traversal
void setToBST(set<int>& s, Node1* root1){
// base condition
if (!root1) return;
// We first move to the left subtree and update elements
setToBST(s, root1->left);
// iterator initially pointing to the starting of set
auto it = s.begin();
// We copy the element at sarting of set(sorted) to the tree.
root1->data = *it;
// now we erase the starting element from set.
s.erase(it);
// now we move to right subtree and update elements
setToBST(s, root1->right);
}
// T(n) = O(nlogn) time
// We convert Binary tree to BST.
void binaryTreeToBST(Node1* root1){
set<int> s;
// We populate the set with the tree's inorder traversal data
storeinorderInSet(root1, s);
// At present sets are by default sorted as they are used
implementing self-balancing BST
// We copy elements from set to the tree while inorder traversal
which makes a BST
setToBST(s, root1);
}
// Time complexity = O(nlogn),
// Auxiliary Space = O(n) for set.
// helper function for creating a node
Node1* newNode(int data){
// dynamically allocating memory
Node1* temp = new Node1();
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
// function for doing inorder traversal
void inorder(Node1* root1){
if (!root1)
return;
inorder(root1->left);
cout<< root1->data << " ";
inorder(root1->right);
}
int main(){
Node1* root1 = newNode(6);
root1->left = newNode(8);
root1->right = newNode(10);
root1->right->left = newNode(11);
root1->left->left = newNode(2);
root1->left->right = newNode(7);
root1->right->right = newNode(12);
/* Building tree given in the following figure
6
/ \
8 10
/\ / \
2 7 11 12 */
// We convert the above Binary tree to BST
binaryTreeToBST(root1);
cout<< "Inorder traversal of BST is: " << endl;
inorder(root1);
return 0;
} আউটপুট
Inorder traversal of BST is: 1 5 6 7 9 10 11
সময়ের জটিলতা হিসাবে চিহ্নিত করা হয় :O(n Log n)
সহায়ক স্থান হিসাবে চিহ্নিত করা হয় :(n)