Creation of Binary Tree OR Binary Search Tree(BST)

Definition of node(नोड की परिभाषा):-

A node is a special structure which stores information related with node. In addition, it also stores pointer to left and right child node or Null value. 
यह एक विशेष प्रकार का स्ट्रक्चर है जो नोड से सम्बंधित जानकारी रखता है। साथ ही लेफ्ट चाइल्ड एवं राइट चाइल्ड नोड को पॉइंट करने हेतु पॉइंटर रखता है या नल वैल्यू रखता है। 
//Node structure (नोड का स्ट्रक्चर)

struct node{
struct node *lchild;
int info;
struct node *rchild;
};
typedef struct node NODE;
NODE *T=NULL;

Here, T is an external pointer variable which is use to store address of root node of tree.
यहाँ T एक एक्सटर्नल पॉइंटर वेरिएबल है जो ट्री के रूट नोड का एड्रेस रखता है। 

Creation of Binary Tree or Binary Search Tree(BST):-

Creation of a binary tree or binary search tree (BST) with given key values is called creation operation.
To create BST, first of all we need to create a root node and stores its address in pointer T after that we compare a new node with root node if its key value is lesser than root node than it will be added in left sub tree otherwise it will be added in right sub tree and this process will continue until we find right place for the node in tree.   

किन्ही दी गयी की वैल्यूज की सहायता से बाइनरी ट्री या बाइनरी सर्च ट्री को तैयार करना, क्रिएशन कहलाता है।   BST को तैयार करने के लिए सर्वप्रथम रूट नोड को तैयार किया जाता है एवं इसका एड्रेस पॉइंटर T को प्रदान किया जाता है इसके पश्चात् किसी नयी नोड को ट्री में इन्सर्ट करने के लिए सबसे पहले नयी नोड की तुलना, रूट नोड से की जाती है यदि नयी नोड की वैल्यू कम है तो उसे लेफ्ट सब ट्री में इन्सर्ट किया जायेगा, यदि नयी नोड की वैल्यू ज्यादा या बराबर है तो उसको रूट नोड के राईट सब ट्री में इन्सर्ट किया जायेगा। इसी प्रकार सभी पूर्व परिभाषित नोड से नयी node की तुलना की जाती है जब तक की नयी नोड को उसकी सही स्थिति पर ना जोड़ा जाये।

//creation of binary tree or BST

NODE create_btree(int info,NODE *T){
if(T==NULL){
T=malloc(sizeof(NODE));
T->info=info;
T->lchild=NULL;
T->rchild=NULL;
return(T);
}
else{
if(info<T->info){
T->lchild=create_btree(info,T->lchild);
}
else{
T->rchild=create_btree(info,T->rchild);
}}}

No comments:

Post a Comment

Stack Data Structure, Push, Pop and Peek Operations , Applications of Stack

Stack is a linear data structure. It is collection of ordered data elements. It is also known as LIFO system (last in first out). It means i...