In: Computer Science
CODE IN JAVA**
I(a). Given a pointer to the root of a binary tree write a routine that will mark (use a negative number like -999 for the info field) every node in the tree that currently has only a left son. You can assume the root of the tree has both a right and left son. When finished tell me how many nodes had only a left son as well as how many nodes are in the tree in total.
1(b). After doing the above write a second routine that will erase the entire tree. You may assume that a routine called Delete exists which when called will delete the node currently being pointed to.
`Hey,
Note: If you have any queries related to the answer please do comment. I would be very happy to resolve all your queries.
Hi, please do comment if you want anything to be changed. I am a senior expert in java. I will help in less than 10 minutes.
a)
void EditTree(Node node)
{
if (node == null)
return;
/* first recur on left child */
EditTree(node.left);
static int ct=0,total=0;
if(node.left!=null&&node.right==null)
{
node.key=-999;
ct=ct+1;
}
total=total+1;
/* now recur on right child */
EditTree(node.right);
if(node==root)
{
System.out.println("Total nodes are "+total);
System.out.println("Nodes with only left son is "+ct);
}
}
b)
void DeleteTree(Node node)
{
if (node == null)
return;
/* first recur on left child */
DeleteTree(node.left);
/* now recur on right child */
DeleteTree(node.right);
Delete(node);
node=null;
}
Kindly revert for any queries
Thanks.