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)
- What does this part of code do? void addRecord(Node *& head, Node *& tail, string name, double balance){ Node *newNode = new Node; newNode->name = name; newNode->balance = balance; newNode->next = NULL; newNode->prev = NULL; // If list is empty, make newNode the first node if(head == NULL){ head = newNode; tail = newNode; } else{ Node *temp = head; // Loop through list until temp->next is NULL while(temp->next != NULL){ temp = temp->next; } temp->next = newNode; newNode->prev = temp; tail = newNode; }} /*arrow_forwardWrite 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_forward
- Task: Complete the function getMinDepth (Node *root), the function takes the root of a tree and returns the minimum depth of the tree. int getMinDepth(Node *root){ //write your code here } Constraints: The number of nodes in the tree is in the range [0, 100000]. -1000 <= Node.val <= 1000arrow_forwardDraw the Recursion Execution Sequence. void preorder (tree_ptr ptr) { if (ptr) { (visit node); preorder (ptr->left_child); preorder (ptr->right_child); } }arrow_forwardvoid 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_forward
- Python binary search tree: a function that takes in a root, p, and checks whether the tree rooted in p is a binary search tree or not. What is the time complexity of your function? def is_bst(self, p: Node):arrow_forwarddata structure-JAVAarrow_forwardJava / Trees: In a tree, no node is allowed more than one parent. Multiple chocie. True Falsearrow_forward
- In C++arrow_forwardA 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_forwardProgramming questions:typedef struct node { int data; struct node *left, *right;}BT;The node structure of the binary tree (BT) is shown above. There is a binary tree T, please complete the function: int degreeone(BT *T) to compute how many degree 1 node in the BT. The T is the root pointer, and the function shoule return the total number of degree 1 node.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