Develope a nonrecursive and recursive algorithmin pseudocode
Develope a nonrecursive and recursive algorithm(in pseudocode) for “Find Samallest Node in a BST”
Solution
In BST we know that, the value of the right subtree has value always greater the the parent node and the left subtree have the ode value always less than the current node.
Now, to recursive Pseudocode to find the smallest element is as follows:-
Algorithm findSmall(Root)
Here root is the pointer to the non-empty BST or the subtree.
1. if(leftsubtree is empty)
2. return root
3. else
4. findSmall(root)
end findSmall.
Now let us see the Non-recursive approach to find the smallest element.
Please note that the non-recursive approach is similar to the Preorder traversal of the node. Because to find the smallest node we have to traverse the left subtree, which in our case will provide the smallest element in the BST.
The Pseudocode is as follows:-
Algorithm Preorder(Root)
Here root is the pointer to the non-empty BST or the subtree.
1. if(leftsubtree is empty)
2. return root
3. else preorder(root) // i.e node->left
4. end Preorder.
