Question: #include using namespace std; struct Node { int data; Node * left; Node * right; } ; Node * search ( Node * root ,
#include
using namespace std;
struct Node
int data;
Node left;
Node right;
;
Node searchNode root int k
if root NULL rootdata k
return root;
if rootdata k
return searchrootright, k;
else
return searchrootleft, k;
Node insertNode node int k
if node NULL
Node newNode new Node;
newNodedata k;
newNodeleft NULL;
newNoderight NULL;
return newNode;
else if k nodedata
noderight insertnoderight, k;
else
nodeleft insertnodeleft, k;
return node;
Find the inorder successor
Node minValueNodeNode node
Node current node;
Find the leftmost leaf
while current NULL && currentleft NULL
current currentleft;
return current;
Node maxValueNodeNode node
Node current node;
Find the rightmost leaf
while current NULL && currentright NULL
current currentright;
return current;
Node deleteNodeNode root int k
if root NULL
return root;
if k rootdata
rootleft deleteNoderootleft, k;
else if k rootdata
rootright deleteNoderootright, k;
else
if rootleft NULL
Node temp rootright;
delete root;
return temp;
else if rootright NULL
Node temp rootleft;
delete root;
return temp;
Node temp minValueNoderootright;
rootdata tempdata;
rootright deleteNoderootright, tempdata;
return root;
void inorderNode n
if n NULL
inordernleft;
cout ndata ;
inordernright;
void preorderNode n
if n NULL
cout ndata ;
preordernleft;
preordernright;
void postorderNode n
if n NULL
postordernleft;
postordernright;
cout ndata ;
int main
Node root NULL;
root insertroot;
insertroot;
insertroot;
insertroot;
insertroot;
insertroot;
inorderroot;
cout endl;
Node searchresult searchroot;
if searchresult NULL
cout "the value is not found" endl;
else
cout "the value is found" endl;
root deleteNoderoot;
inorderroot;
cout endl;
root deleteNoderoot;
inorderroot;
cout endl;
root deleteNoderoot;
inorderroot;
cout endl;
return ;
using the following code solve this Q
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
