Insertion in binary search tree

Insertion in Binary Search Tree(BST):-

To insert a new node in binary search tree (BST) is called insertion operation. To insert a new node in BST, we compare that 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. 

Suppose we have a binary search tree in memory and root nodes address is stored in external pointer variable T.

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

माना कि मेमोरी में एक बाइनरी सर्च ट्री उपस्थित है एवं जिसके रूट नोड का एड्रेस एक्सटर्नल पॉइंटर वेरिएबल T में रखा गया है। 

//insertion of a new node in BST

NODE insert_btree(int info,NODE *T){
if(info<T->info){
T->lchild=insert_btree(info,T->lchild);
}
else{
T->rchild=insert_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...