How to search an element in bst????? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to search an element in bst?????

28th Oct 2018, 6:50 PM
Abhinav jha
Abhinav jha - avatar
2 Answers
+ 1
Please tag you question properly. Preferably with programming language and search tree in your case at least. To search a given key in Binary Search Tree, we first compare it with root, if the key is present at root, we return root. If key is greater than root’s key, we recur for right subtree of root node. Otherwise we recur for left subtree C implementation struct node* search(struct node* root, int key) {     // Base Cases: root is null or key is present at root     if (root == NULL || root->key == key)        return root;     // Key is greater than root's key     if (root->key < key)        return search(root->right, key);      // Key is smaller than root's key     return search(root->left, key); }
28th Oct 2018, 9:00 PM
Megatron
Megatron - avatar
0
thank u
29th Oct 2018, 2:32 AM
Abhinav jha
Abhinav jha - avatar