Searching in binary search tree(BST)

Searching in binary search tree(BST) :-

To search a value in BST, first we compare the value with root node. if the value is equal to root node then we return its position. otherwise
if value is smaller then root node we search this value in left sub tree.
if the value is greater then root node we search this value in right sub tree.
this process will continue until all nodes of tree will be compared with given value.
If the value is not available in tree search operation is assumed unsuccessful.     

BST में किसी नोड को खोजने के लिए नोड की वैल्यू की तुलना, ट्री के रूट नोड की वैल्यू से की जाती है यदि नोड प्राप्त हो जाता है तब उसकी पोजीशन प्रदान की जाती है अन्यथा यदि वह वैल्यू छोटी होती है तब लेफ्ट सब ट्री के नोड्स में उसे खोजा जाता है नहीं तो राईट सब ट्री के नोड्स में उसे खोजा जाता है यदि वह वैल्यू ट्री में उपलब्द्ध नहीं है तब सर्च को असफल माना जाता है।

//search function
struct node* search_tree(int info,struct node *T){
struct node *ptr;
ptr=T;
while(ptr!=NULL){
if(ptr->info==info)
printf("%d is found at address=%u\n",info,ptr);
getch();
}
else{
if(info>ptr->info)
ptr=ptr->rchild;
}
else{
ptr=ptr->lchild;
}
}
printf("Search Unsuccessful");
getch();
}

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...