Question: Implement a function that returns the successor of a node in a binary search tree (the BST stores integer keys). A successor of a node
Implement a function that returns the successor of a node in a binary search tree (the BST stores integer keys). A successor of a node n is defined as the smallest key x in the BST such that x is bigger than the value of n, or null if that does not exist. You may assume that the BST does not contain duplicate keys. The signature of the function you have to implement and the interface of the TreeNode class, which implements the BST, are given below. Note that getLeft(), getRight(), and getParent() return null if the node does not have a left, a right child, or is the root, respectively.
class TreeNode{
int value;
TreeNode *Left;
TreeNode *Right;
TreeNode *Parent;
public:
int getValue();
TreeNode* getLeft();
TreeNode* getRight();
TreeNode* getParent();
/* Returns -1 if no successor exists */
int successor(TreeNode x) ;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
