কম্পিউটার টিউটোরিয়াল

C++-এ বাইনারি ট্রিতে আডজেসেন্ট নোড পেয়ার য xog XOR বিজোড় হয় তা গণনা


এই টিউটোরিয়ালে আমরা বাইনারি ট্রিতে আডজেসেন্ট নোড পেয়ার যাদের XOR বিজোড় সংখ্যা হয় সেগুলোর সংখ্যা গণনা করার একটি প্রোগ্রাম আলোচনা করব।

এ জন্য আমাদের একটি বাইনারি ট্রি দেওয়া থাকবে। আমাদের টাস্ক হলো আডজেসেন্ট এলিমেন্ট পেয়ার যাদের XOR বিজোড় সংখ্যা হয় সেগুলোর সংখ্যা গণনা করা।

উদাহরণ

#include <iostream> using namespace std; //node structure of tree struct Node {    int data;    struct Node *left, *right; }; //finding the pairs whose XOR //is odd int count_pair(Node* root, Node *parent=NULL){    if (root == NULL)       return 0;    //checking pair of XOR is odd or not    int res = 0;    if (parent != NULL && (parent->data ^ root->data) % 2)       res++;    return res + count_pair(root->left, root) + count_pair(root->right, root); } //creation of new node Node* newNode(int data){    Node* temp = new Node;    temp->data = data;    temp->left = NULL;    temp->right = NULL;    return temp; } int main(){    struct Node* root = NULL;    root = newNode(15);    root->left = newNode(13);    root->left->left = newNode(12);    root->left->right = newNode(14);    root->right = newNode(18);    root->right->left = newNode(17);    root->right->right = newNode(21);    printf("%d ", count_pair(root));    return 0; }

আউটপুট

5

  1. C++-এ বিটওয়াইজ OR অড্ড হয় এমন জোড়ার সংখ্যা বের করা

  2. C++-এ যোগফল ২-এর ঘাত হয় এমন জোড়ার সংখ্যা বের করা

  3. অ্যারেতে সব জোড়ার যোগফলের XOR-এর যোগফল C++-তে

  4. ফিবোনাচ্চি সংখ্যা যেন করা নোডের ওজন এবং X-এর যোগফল C++-এ