Write a recursive buildBinaryTree method that builds a new binary tree from the contents of an array that contains integers. Use the following class definition for a node in a binary tree: class BinaryNode { int element; BinaryNode left; BinaryNode right; } Hint: Take one item from the array and insert it as the root of the tree. Divide the remaining items in half and insert one half in the right subtree and the other half in the left subtree of the root node by calling the method recursively with the relevant array indices.Write a method oddEntries that returns the number of odd integers contained in a binary tree where the element type is int. This method is called with a link to the root node of the tree.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Write a recursive buildBinaryTree method that builds a new binary tree from the contents of an array that contains integers. Use the following class definition for a node in a binary tree: class BinaryNode { int element; BinaryNode left; BinaryNode right; } Hint: Take one item from the array and insert it as the root of the tree. Divide the remaining items in half and insert one half in the right subtree and the other half in the left subtree of the root node by calling the method recursively with the relevant array indices.Write a method oddEntries that returns the number of odd integers contained in a binary tree where the element type is int. This method is called with a link to the root node of the tree.

import test.BinaryNode;
// BinaryNode class; stores a node in a
tree.
//
// CONSTRUCTION: with (a) no parameters,
or (b) an 0bject,
//
or (c) an Object, left child, and
right child.
//
// *****
***PUBLIC
OPERATIONS***
// int size( )
of subtree at node
// int height( )
height of subtree at node
// void printPostOrder( )
postorder tree traversal
// void printInorder( )
inorder tree traversal
t*****
--> Return size
--> Return
--> Print a
--> Print an
// void printPreOrder( )
preorder tree traversal
// BinaryNode duplicate( )--> Return a
duplicate tree
--> Print a
/**
* 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;
lt;
= rt;
%3D
left
%3D
right
}
%3D
/**
* Return the size of the binary tree
rooted at t.
치/
public static int size( BinaryNode t)
{
if( t ==
return 0;
null )
else
return 1 + size( t.left ) +
size( t.right );
}
/**
* Return the height from a node to
the root node of the binary tree.
* /
public static int height( /** CODE
HERE **/ )
{
/** CODE HERE **/
}
// Print tree rooted at current node
using preorder traversal.
public void printPreOrder( )
{
System.out.println( element );
// Node
if( left != null )
left.printPreOrder( );
// Left
if( right != null )
right.printPreOrder( );
// Right|
}
// Print tree rooted at current node
using postorder traversal.
public void printPostorder( )
{
if( left != null )
left.printPostorder( );
// Left
if( right != null )
right.printPostOrder( );
// Right|
System.out.println( element );
// Node
}
// Print tree rooted at current node
using inorder traversal.
public void printInorder (O
{
if( left != null )
left.printInOrder( );
// Left
System.out.println( element );
// Node
if( right != null )
right.printInOrder( );
// Right|
}
/**
* Return a reference to a node that
is the root of a
* duplicate of the binary tree rooted
at the current node.
*/
public BinaryNode duplicate( )
{
BinaryNode root =
element, null, null );
new BinaryNode(
if( left != null )
If there's a left subtree
//
root.left = left.duplicate( );
%3D
// Duplicate; attach
if( right != null )
there's a right subtree
// If
root.right = right.duplicate(
// Duplicate; attach
return root;
);
// Return resulting tree
}
public int getElement( )
{
return element;
}
public BinaryNode getLeft( )
{
return left;
}
public BinaryNode getRight(O
{
return right;
}
public
void setElement( int x )
{
element = x;
}
public void setleft( BinaryNode t )
{
left
t;
%3D
}
public void setRight( BinaryNode t )
{
right
}
= t;
private int
private BinaryNode left;
private BinaryNode right;
}
element;
Transcribed Image Text:import test.BinaryNode; // BinaryNode class; stores a node in a tree. // // CONSTRUCTION: with (a) no parameters, or (b) an 0bject, // or (c) an Object, left child, and right child. // // ***** ***PUBLIC OPERATIONS*** // int size( ) of subtree at node // int height( ) height of subtree at node // void printPostOrder( ) postorder tree traversal // void printInorder( ) inorder tree traversal t***** --> Return size --> Return --> Print a --> Print an // void printPreOrder( ) preorder tree traversal // BinaryNode duplicate( )--> Return a duplicate tree --> Print a /** * 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; lt; = rt; %3D left %3D right } %3D /** * Return the size of the binary tree rooted at t. 치/ public static int size( BinaryNode t) { if( t == return 0; null ) else return 1 + size( t.left ) + size( t.right ); } /** * Return the height from a node to the root node of the binary tree. * / public static int height( /** CODE HERE **/ ) { /** CODE HERE **/ } // Print tree rooted at current node using preorder traversal. public void printPreOrder( ) { System.out.println( element ); // Node if( left != null ) left.printPreOrder( ); // Left if( right != null ) right.printPreOrder( ); // Right| } // Print tree rooted at current node using postorder traversal. public void printPostorder( ) { if( left != null ) left.printPostorder( ); // Left if( right != null ) right.printPostOrder( ); // Right| System.out.println( element ); // Node } // Print tree rooted at current node using inorder traversal. public void printInorder (O { if( left != null ) left.printInOrder( ); // Left System.out.println( element ); // Node if( right != null ) right.printInOrder( ); // Right| } /** * Return a reference to a node that is the root of a * duplicate of the binary tree rooted at the current node. */ public BinaryNode duplicate( ) { BinaryNode root = element, null, null ); new BinaryNode( if( left != null ) If there's a left subtree // root.left = left.duplicate( ); %3D // Duplicate; attach if( right != null ) there's a right subtree // If root.right = right.duplicate( // Duplicate; attach return root; ); // Return resulting tree } public int getElement( ) { return element; } public BinaryNode getLeft( ) { return left; } public BinaryNode getRight(O { return right; } public void setElement( int x ) { element = x; } public void setleft( BinaryNode t ) { left t; %3D } public void setRight( BinaryNode t ) { right } = t; private int private BinaryNode left; private BinaryNode right; } element;
Expert Solution
steps

Step by step

Solved in 5 steps with 2 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY