i need help to modify this Tree class to include a method leavesCount that returns the number of leaves in a binary tree. (NOTE: Complete leavesCount and leavesCountHelper methods in the Tree class). I already have the test class. java
class TreeNode< T extends Comparable< T > >
{
TreeNode< T > leftNode;
T data;
TreeNode< T > rightNode;
public TreeNode( T nodeData )
{ data = nodeData;
leftNode = rightNode = null; }
public void insert( T insertValue ) {
if ( insertValue.compareTo( data ) < 0 )
{
if ( leftNode == null )
leftNode = new TreeNode< T >( insertValue );
else
leftNode.insert( insertValue ); }
else if ( insertValue.compareTo( data ) > 0 )
{
if ( rightNode == null )
rightNode = new TreeNode< T >( insertValue );
else
rightNode.insert( insertValue );
}
}
}
public class Tree< T extends Comparable< T > >
{
private TreeNode< T > root;
public Tree()
{
root = null;
}
public void insertNode( T insertValue )
{
if ( root == null )
root = new TreeNode< T >( insertValue );
else
root.insert( insertValue );
}
public void preorderTraversal()
{
preorderHelper( root );
}
private void preorderHelper( TreeNode< T > node )
{
if ( node == null )
return;
System.out.printf( "%s ", node.data );
preorderHelper( node.leftNode );
preorderHelper( node.rightNode );
}
public void inorderTraversal()
{
inorderHelper( root );
}
private void inorderHelper( TreeNode< T > node )
{
if ( node == null )
return;
inorderHelper( node.leftNode );
System.out.printf( "%s ", node.data );
inorderHelper( node.rightNode );
}
public void postorderTraversal()
{
postorderHelper( root );
}
private void postorderHelper( TreeNode< T > node )
{
if ( node == null )
return;
postorderHelper( node.leftNode );
postorderHelper( node.rightNode );
System.out.printf( "%s ", node.data ); // output node data
}
public int nodeCount()
{
return nodeCountHelper( root );
}
private int nodeCountHelper(TreeNode< T > node)
{
if (node == null)
return 0;
else
return 1 + nodeCountHelper(node.leftNode) + nodeCountHelper(node.rightNode);
}
public int leavesCount()
{
//write code
}
private int leavesCountHelper(TreeNode< T > node)
{
//write code
}
} // end class Tree
I've added a main method to test count leaves method whose input is below
Input

Output

Code with comments
public class Test {
static class TreeNode<T extends Comparable<T>>{
TreeNode<T> leftNode;
T data;
TreeNode<T> rightNode;
public TreeNode(T nodeData)
{
data = nodeData;
leftNode = rightNode = null;
}
public void insert(T insertValue) {
if (insertValue.compareTo(data) < 0)
{
if (leftNode == null)
leftNode = new TreeNode<T>(insertValue);
else
leftNode.insert(insertValue);
}
else if (insertValue.compareTo(data) > 0)
{
if (rightNode == null)
rightNode = new TreeNode<T>(insertValue);
else
rightNode.insert(insertValue);
}
}
}
static public class Tree<T extends Comparable<T>>
{
private TreeNode<T> root;
public Tree()
{
root = null;
}
public void insertNode(T insertValue)
{
if (root == null)
root = new TreeNode<T>(insertValue);
else
root.insert(insertValue);
}
public void preorderTraversal()
{
preorderHelper(root);
}
private void preorderHelper(TreeNode<T> node)
{
if (node == null)
return;
System.out.printf("%s ", node.data);
preorderHelper(node.leftNode);
preorderHelper(node.rightNode);
}
public void inorderTraversal()
{
inorderHelper(root);
}
private void inorderHelper(TreeNode<T> node)
{
if (node == null)
return;
inorderHelper(node.leftNode);
System.out.printf("%s ", node.data);
inorderHelper(node.rightNode);
}
public void postorderTraversal()
{
postorderHelper(root);
}
private void postorderHelper(TreeNode<T> node)
{
if (node == null)
return;
postorderHelper(node.leftNode);
postorderHelper(node.rightNode);
System.out.printf("%s ", node.data); // output node data
}
public int nodeCount()
{
return nodeCountHelper(root);
}
private int nodeCountHelper(TreeNode<T> node)
{
if (node == null)
return 0;
else
return 1 + nodeCountHelper(node.leftNode) + nodeCountHelper(node.rightNode);
}
public int leavesCount()
{
return leavesCountHelper(root);
}
private int leavesCountHelper(TreeNode<T> node)
{
if(node==null) //node is null
return 0;
if(node.leftNode==null && node.rightNode==null) //node is leaf
return 1;
return leavesCountHelper((node.leftNode))+leavesCountHelper((node.rightNode)); //sum of leaf nodes in left and right subtree
}
} // end class Tree
public static void main(String[] args) {
Tree<Integer>tree=new Tree<>();
tree.insertNode(4);
tree.insertNode(3);
tree.insertNode(6);
tree.insertNode(5);
tree.insertNode(1);
tree.insertNode(2);
System.out.println("Preorder:");
tree.preorderTraversal();
System.out.println("\nPostorder:");
tree.postorderTraversal();
System.out.println("\nInorder:");
tree.inorderTraversal();
System.out.println("\nTotal leaf nodes = "+tree.leavesCount());
}
}
For indentation purpose

i need help to modify this Tree class to include a method leavesCount that returns the...
I need to do a tree sort method but the treesortMethod is not working /****Binarytree class****\ package Tree; public class BinaryTree { private TreeNode root; // head of the list //constructor - create an empty binary tree public BinaryTree() { root = null; } //isEmpty() - return true if tree is empty, false otherwise public boolean isEmpty() { return (root == null); } //deleteTree() - remove all items from tree public void deleteList() { root =...
In need of JAVA CODE FOR THIS OBJECTIVE: Modify the BST class I gave you as follows: In the case in which the deleted node has two children, write the code in two ways. a) always replace the deleted node with the largest node on the left. b) count how many deletions have been done, and for even deletions replace the deleted node with the largest node on the left, for odd deletions, replace the deleted node with the smallest...
BST JAVA FILE import java.util.*; public class BST <E extends Comparable <E>> { private TreeNode<E> overallRoot; public BST() { overallRoot = null; } // ************ ADD ************ // public void add(E addThis) { if (overallRoot == null) { overallRoot = new TreeNode<>(addThis); } else { add(overallRoot, addThis); } } private TreeNode<E> add(TreeNode<E> node, E addThis) { if...
Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to completely test every method in the BST class to ensure the class meets all its requirements. You should read the Listing 25.5: TestBST.java for an idea of what your program should look like. Listing 25.4 BST.java public class BST> extends AbstractTree { protected TreeNode...
Java binary search tree Add the following print method to the binary search tree class created in class (on D2L). This method should print all the nodes in the tree in level order (root first, then all children of root, then all children of those). Ensure your method runs in O(N), include comments to show how it conforms to this rule. Method header: public void printInLevelOrder() public class BinarySearchTree<E extends Comparable<? super E>> { private Node root; public BinarySearchTree() {...
Given the following code: #ifndef TREE_H #define TREE_H #include <iostream> #include "TreeNode.h" template< typename NODETYPE > class Tree { public: Tree() : rootPtr( nullptr ) {} void insertNode( const NODETYPE &value ) { insertNodeHelper( &rootPtr, value ); } void preOrderTraversal() const { preOrderHelper( rootPtr ); } void inOrderTraversal() const { inOrderHelper( rootPtr ); } private: TreeNode< NODETYPE > *rootPtr; void insertNodeHelper( TreeNode< NODETYPE > **ptr, const NODETYPE &value ) { if ( *ptr == nullptr ) * ptr = new...
1. Write a function in Tree class which returns true if and only if the tree satisfies the binary search tree property. The function’s header line is public boolean isValidBST() And in the attached code, you just need to finish the function after the comment: “//Instructor hint: please write your code here:” Make sure you execute your code, and the result in the main function after calling your function should be same as the prompt message I write. Clearly you...
1) Extend the Binary Search Tree ADT to include
a public method leafCount that returns the number of leaf nodes in
the tree.
2) Extend the Binary Search Tree ADT to include a
public method singleParent-Count that returns the number of nodes
in the tree that have only one child.
3) The Binary search tree ADT is extended to
include a boolean method similarTrees that receives references to
two binary trees and determines whether the shapes of the trees are...
Consider the class specifications for the Binary Tree class and Binary Search Tree class in the attached files // BinaryTree.h #include <iostream> using namespace std; //Definition of the Node template <class elemType> struct TreeNode { elemType data; TreeNode<elemType> *left; TreeNode<elemType> *right; }; //Definition of class Binary Tree template <class elemType> class BinaryTree { protected: TreeNode<elemType> *root; public: BinaryTree(); BinaryTreel const BinaryTree<elemType>& otherTree); BinaryTree(); bool is Empty() const; virtual boot search(const elemType& searchItem) const = 0; virtual void insert(const elemType& insertItem)...
Consider the class specifications for the Binary Tree class and BinarySearch Tree class below: // Binary Tree.h #include <iostream> using namespace std; //Definition of the Node template <class elemType struct TreeNode { elemType data; TreeNode<elemType> *left; TreeNode<elemType *right; }; //Definition of class Binary Tree template <class elemType> class Binary Tree { protected: TreeNode<elemType> *root; public: BinaryTree(); BinaryTreel const BinaryTree<elemType>& otherTree); -Binary Tree(): bool is Empty() const; virtual bool search(const elemTypes searchItem) const = 0; virtual void insert(const elemTypek insertItem) =...