Find in-order Successor and Predecessor in a BST using C++ program
belongs to collection: Data Structure programs using C and C++ (Sorting Programs)
All Answers
total answers (1)
belongs to collection: Data Structure programs using C and C++ (Sorting Programs)
total answers (1)
Algorithm:
Step 1: start with root of the tree
Step 2: if the given node is equal to root, then for the in-order predecessor go to the right most node of the left child of the root and for the in-order successor go to the left most node of the right child of the root.
Step 3: if the given node is greater than the root, then update predecessor equal to root and root equal to its right child and search the subtree(current tree).
Step 4: if the given node is less than the root, then update successor equal to root and root equal to its left child and search the subtree(current tree).
Consider the given program:
Output