Sunday, August 21, 2016

ডাটা স্ট্রাকচার - ৮ঃ বাইনারি ট্রি ইমপ্লিমেন্টেশন

এই পর্বে আমরা দেখবো কিভাবে ট্রি আকারে ডেটা স্টোর করা যায়। ইমপ্লিমেন্টেশনের সুবিধার্থে আমরা কোন নোডেই দুইটার বেশী চাইল্ড রাখবোনা। মানে বাইনারি ট্রি নিয়ে কাজ করবো। আর ডেটা হিসেবে রাখবো ইন্টেজার। 


 Algorithm:   
 1. Create node data type which is able to hold two the addresses of another two nodes and an integer   
 2. Create a node variable and put the necessary data in it.   
 3. Add left child of a particular node   
 4. Add right child of a particular node   
 5. Do 2, 3, 4 as much as you want to insert data   

Pseudo code
 //Create node data type 
struct Node  
 {  
   int data;  
   struct Node *left;  
   struct Node *right;  
 };  

 Node *createNode(int item)  
 {  
   Node *newNode = (Node*)malloc(sizeof(Node));  
   if(newNode == NULL)  
   {  
     printf("Error! Couldn't create new node\n");  
     exit(1);  
   }  
   newNode->data = item;  
   newNode->left = NULL;  
   newNode->right = NULL;  
   return newNode;  
 }  

 void addLeftChild(Node *node, Node *child)  
 {   
   node->left = child;  
 }  

 void addRightChild(Node *node, Node *child)  
 {  
   node->right = child;  
 }  

No comments:

Post a Comment