Suppose we have access to the BinNode class. Recall that a BinNode object has th
ID: 3801682 • Letter: S
Question
Suppose we have access to the BinNode class. Recall that a BinNode object has three fields: data, left and right. All the fields of a BinNode are public. For this problem assume that the type of the field data is int (no templates). You are to write a function that has parameters which are pointers to BinNode objects, root and p. The parameter root is a non-NULL pointer with target the root off a binary search tree T and that p is a non-NULL pointer to a node in T. You may assume there are no repeated data values in T. The function returns a pointer to the parent of p in T, or NULL if p has no parent. BinNode* parent(BinNode* root, BinNode* p)Explanation / Answer
//============================================================//
BinNode * getParent(BinNode *root, BinNode* p){
if (root == NULL) return NULL;
else if (root->right->data == p->data || root->left->data == p->data) return root;
else if (root->data > p->data) getParent(root->left, p);
else getParent(root->right, p);
return root;
}
//=============================================================//
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.