একটি বাইনারি গাছে, একটি রুট নোড থাকে যা একটি গাছের সমস্ত নোডের মাস্টার নোড। একটি নোডে ডেটা অংশ, বাম পয়েন্টার রয়েছে যা আরও বাম সাবডিরেক্টরি এবং একটি ডান পয়েন্টার তৈরি করবে যা ডান সাবডিরেক্টরি তৈরি করতে সহায়তা করবে। তাই গাছটি অতিক্রম করার জন্য, আমরা একটি অস্থায়ী পয়েন্টার নিতে পারি যেটি বাম পয়েন্টারের সাথে বাম সাব-ডিরেক্টরি ট্রাভার্স করতে বা ডান সাব-ডিরেক্টরি অতিক্রম করতে ডান পয়েন্টারের সাথে যুক্ত হবে।
ইনপুট৷
আউটপুট
Nodes are-: 10, 20, 30, 40, 50, 60 Product = 10*20*30*40*50*60 = 72,00,00,000
পন্থা
-
নোড ডেটা ইনপুট করুন
-
রুট নোড থেকে শুরু করে সমস্ত নোড অতিক্রম করুন এবং ট্রাভার্সালের জন্য বাম সাব ডিরেক্টরি বা ডান সাবডিরেক্টরিতে যান৷
-
নোডের ডেটা সংরক্ষণ করুন এবং সঞ্চিত ডেটাকে নতুন ডেটা দিয়ে গুণ করতে থাকুন
-
গুণিত মান ধারণ করে একটি অস্থায়ী পরিবর্তনশীলের মান প্রিন্ট করুন।
অ্যালগরিদম
Start Step 1 → create structure of a node structure node struct node int data Create node *left, *right End Step 2 → declare function to insert a node in a tree node* new_node(int data) Set node* temp = new node() Set temp→data = data Set temp→left = temp→right = NULL return temp End Step 3 → Declare a function to multiply all the nodes void leaf(node* root, int &product) IF root = NULL return 1 End return (root→data * node_product(root→left) * node_product(root→right)) Step 4 → In main() Create node* root = new_node(10) Set root→left = new_node(20) Set root→left→left = new_node(30) Set int product = node_product(root) Display product Stop
উদাহরণ
#include <iostream> using namespace std; //structure of a node struct node{ int data; node *left, *right; }; //function for inserting a new node node* new_node(int data){ node* temp = new node(); temp→data = data; temp→left = temp→right = NULL; return temp; } //function for multiplying all the nodes int node_product(node* root){ if (root == NULL) return 1; return (root→data * node_product(root→left) * node_product(root→right)); } int main(){ node* root = new_node(10); root→left = new_node(20); root→right = new_node(30); root→left→left = new_node(40); root→left→right = new_node(50); root→right→left = new_node(60); int product = node_product(root); cout << "Product of all the nodes is: "<<product<< endl; return 0; }
আউটপুট
উপরের কোডটি চালালে এটি নিম্নলিখিত আউটপুট −
উৎপন্ন করবেProduct of all the nodes is: 720000000