Explanation of Solution
Given
//Return the number of nodes in a binary tree
int Nodecount (Node tree) //Line 1
{ //Line 2
//Declare a variable to get the number of nodes
int count = 0; //Line 3
//Check if tree is not equal to null
if (tree != null) //Line 4
{ //Line 5
//Increment the value of count
count++; //Line 6
//Call the function recursively by passing the left sub tree
NodeCount(tree.left); //Line 7
//Call the function recursively by passing the right sub tree
NodeCount(tree.right); //Line 8
} //Line 9
//Return the value of count
return count;//Line 10
}//Line 11
The above program code snippet is used to find the number of nodes present in the binary tree.
Error in the program code:
The statements inside the “if” condition does not use the returned values from the recursive calls. The lines from “Line 6” to “Line 10” should be changed and the modified snippet is given below...
Want to see the full answer?
Check out a sample textbook solutionChapter 21 Solutions
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
- Write a function in C that returns the second minimum value in a binary search tree at root. struct node { int value; struct node * left; struct node * right; }; int second_min(struct node * root) { return -1; }arrow_forwardGiven the following struct that represents a binary tree: struct Node { int key: Node "parent; Node "left; Node "right; Nodelint k) : key(k), parent(nullptr), left(nullptr), right(nullptr) (I: 1: Write a recursive function that prints out the nodes in a tree stored using the above structure in order to cout. The function prints the depth (root depth is at 0) and key of that node separated by a colon (Example "O: 10\n" for root with key 10). Your function CAN NOT create any local variables and can only use what is passed to the function. Use the below function signature (NOTE: this is not a class method). void inorderAndDepth(Node "node, int depth)arrow_forwardDraw the Recursion Execution Sequence. void preorder (tree_ptr ptr) { if (ptr) { (visit node); preorder (ptr->left_child); preorder (ptr->right_child); } }arrow_forward
- void doo(node<int>*root){ if(root !=0) { node<int>*p=root; while(root->next!=0) root=root->next; p->data=root->data; } What is this code do? a. doesn't do anything because the root parameter is passed by value b. change the root item in the binary tree with the farthest leaf item c. set the first item in the linked list as the last item d. swap the first item with the last item in the linked listarrow_forwarddata structure-JAVAarrow_forwardJava / Trees: In a tree, no node is allowed more than one parent. Multiple chocie. True Falsearrow_forward
- A tree is implemented using a node structure defined as: struct node{ int data; struct node *left; strict node *right; }; Write a function whose prototype is: int smallest(struct node *); which accepts a tree (pointer to the root) and returns the smallest node in the right sub-tree of the root. If root is NULL, or if there is no right sub-tree, returm the number -999.arrow_forwardMake an array of linked list for the following C code: // Node structure containing power and coefficient of// variablestruct Node {int coeff;int pow;struct Node* next;}; // Function to create new nodevoid create_node(int x, int y, struct Node** temp){struct Node *r, *z;z = *temp;if (z == NULL) {r = (struct Node*)malloc(sizeof(struct Node));r->coeff = x;r->pow = y;*temp = r;r->next = (struct Node*)malloc(sizeof(struct Node));r = r->next;r->next = NULL;}else {r->coeff = x;r->pow = y;r->next = (struct Node*)malloc(sizeof(struct Node));r = r->next;r->next = NULL;}} // Function Adding two polynomial numbersvoid polyadd(struct Node* poly1, struct Node* poly2,struct Node* poly){while (poly1->next && poly2->next) {// If power of 1st polynomial is greater then 2nd,// then store 1st as it is and move its pointerif (poly1->pow > poly2->pow) {poly->pow = poly1->pow;poly->coeff = poly1->coeff;poly1 = poly1->next;} // If power of…arrow_forwardclass Node{// attributesint ID;String name; Node left; // left child Node right; // right child}class BinarySearchTree{// attributesNode root;// methodssearch(int key, Node root);void insert(int key, Node root);void delete(int key, Node root);}Extend the BinarySearchTree class:1 Write code for the method search(int key, Node root)2 Write code for the method insert(int key, Node root)3 Write code for the method delete(int key, Node root)4 Write code for a main method to build a BSTTest your code with the input in the worked example.Input:// id, name41, notes11, personal61, work30, shopping5, recipes55, proposal70, thesis10, muffins43, draftarrow_forward
- please convert to C languange #include<bits/stdc++.h>using namespace std; class tree{ //tree node public: int data; tree *left; tree *right;}; bool hasRootToLeafSum(tree *root, int s){ bool path=false; //declare boolean variable path //base condition checking if(root==NULL && s==0) return true; s-=root->data; //subtract current root value //checking whether leaf node reached and remaining sum =0 if(s==0 && root->left==NULL && root->right==NULL) return true; //recursively done for both subtrees if(root->left){//for left subtree path=path||hasRootToLeafSum(root->left, s); } if(root->right){//for right subtree path=path||hasRootToLeafSum(root->right, s); } return path;} tree* newnode(int data){ //creating new nodes tree* node = (tree*)malloc(sizeof(tree)); node->data = data; node->left = NULL; node->right = NULL;…arrow_forwardUsing discriminated union in F#, create a tree type that can be either an empty tree or a tree node of float * tree * treearrow_forwardBreadth-first search must be done without recursion. (with iterative)arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education