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)
- The tree pointer is the first node of the binary tree and has both data and two reference links. True Falsearrow_forwardJava / Trees: *Please refer to attached image* What is the inorder of this tree? Multiple chocie. G X C A N V F Q L W G X C A N V F L W E A C V N X G F L W E A C V N VG F L W Earrow_forwardTo copy a tree, traverse the tree using an ___ traversal and add each data item visited to the new tree. inorder postorder preorder reorderarrow_forward
- In Java, True or False: Every binary tree has at least one node. TRUE FALSE Every non-empty tree has exactly one root node. TRUE FALSE Every node in a binary tree has at most one child. TRUE FALSE Every non-root node in a binary tree has exactly two parents. TRUE FALSEarrow_forwardIn java: binary tree Write a method called findTotalleaves (), this method will find all the nodes that considered to be leaves, which is they have no BNode dependencies. The public method shall not take any parameters, but the private method takes a BNode as a parameter, representing the current root.arrow_forwardWrite a code to delete a node from a Binary search tree. The format of the function is void deleteNode(int key). Note - Do not use any other return form for the function. Do not return any node pointersarrow_forward
- Complete the findMin() method. This method should return the minimum of the values in the tree. If findMin was called with the tree pictured before, it should return 1. class Main { public static void main(String[] args) { BinaryTree.main(args); } } public class BinaryTree { public BinaryTree() { root = null; } public BinaryTree(Node node) { root = node; } static class Node { Node(int d, Node l, Node r) { data = d; left = l; right = r; } public int data; public Node left; public Node right; } public Node root; public int findMin() { return -1; } public static void main(String[] args) { BinaryTree myTree = new BinaryTree(); myTree.root = new Node(10, new Node(5, new Node(20, null, null), new Node(25, new Node(15, new Node(30, null, null), new Node(35, null, null)), null)), new Node(40, null, null)); System.out.println(myTree.findMin()); } }arrow_forwardonly c here is "BinaryNode.java" // BinaryNode class; stores a node in a tree. // // CONSTRUCTION: with (a) no parameters, or (b) an Object, // or (c) an Object, left child, and right child. // // *******************PUBLIC OPERATIONS********************** // int size( ) --> Return size of subtree at node // int height( ) --> Return height of subtree at node // void printPostOrder( ) --> Print a postorder tree traversal // void printInOrder( ) --> Print an inorder tree traversal // void printPreOrder( ) --> Print a preorder tree traversal // BinaryNode duplicate( )--> Return a duplicate tree /** * Binary node class with recursive routines to * compute size and height. */ class BinaryNode { public BinaryNode( ) { this( 0, null, null ); } public BinaryNode( int theElement, BinaryNode lt, BinaryNode rt ) { element = theElement; left = lt; right = rt; } /** * Return the size of the binary tree rooted at t. */ public static int size( BinaryNode t ) { if( t == null ) return…arrow_forwardimport java.util.LinkedList; public class IntTree { private Node root; private static class Node { public int key; public Node left, right; public Node(int key) { this.key = key; } } public void printInOrder() { printInOrder(root); } private void printInOrder(Node n) { if (n == null) return; printInOrder(n.left); System.out.println(n.key); printInOrder(n.right); } /* * Returns the number of nodes in the tree, at exactly depth k. For example, * given BST with level order traversal 50 25 100 12 37 150 127 the following * values should be returned t.sizeAtDepth(0) == 1 [50] t.sizeAtDepth(1) == 2 * [25, 100] t.sizeAtDepth(2) == 3 [12, 37, 150] t.sizeAtDepth(3) == 1 [127] * t.sizeAtDepth(k) == 0 for k >= 4 */ public int sizeAtDepth(int k) { // TODO throw new RuntimeException("Not implemented"); } • You are not allowed to use any kind of…arrow_forward
- Java Programming Language Create a class that accepts id numbers ranging from 1 to 29. The id numbers are nodes of a binary tree. Traverse the tree in inorder, preorder, and postorder and display the traversed values.arrow_forwardTo insert an element into a binary search tree (BST), you need to locate where to insert it in the tree. The key idea is to locate the parent for the new node. True Falsearrow_forwardImplement a tree using a doubly linked list. Each node in the linked list would be of type“struct/class” and has the following declaration: struct/class Student {Student * left;Student * right;int marks;string name;}; You have to implement the following functions for the tree:1) InsertThis function will add a new student within the tree structure. Ask the user for studentname and marks.2) DisplayThis function will print data of all students in the tree structure (use any traversal method).3)SortingThis function will sort the students within the tree structure based on marks. NOTE:DO IN C++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