JAVA  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. Note: You have to explain your method by means of inline comments or by including a pseudo-code version of this method in comments.   import test.BinaryNode; // 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 0; 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( ) { 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 = new BinaryNode( element, null, null ); if( left != null ) // If there's a left subtree root.left = left.duplicate( ); // Duplicate; attach if( right != null ) // If there's a right subtree root.right = right.duplicate( ); // Duplicate; attach return root; // Return resulting tree } public int getElement( ) { return element; } public BinaryNode getLeft( ) { return left; } public BinaryNode getRight( ) { return right; } public void setElement( int x ) { element = x; } public void setLeft( BinaryNode t ) { left = t; } public void setRight( BinaryNode t ) { right = t; } private int element; private BinaryNode left; private BinaryNode right; }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

JAVA 

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.
Note: You have to explain your method by means of inline comments or by including a
pseudo-code version of this method in comments.

 

import test.BinaryNode; // 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 0; 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( ) { 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 = new BinaryNode( element, null, null ); if( left != null ) // If there's a left subtree root.left = left.duplicate( ); // Duplicate; attach if( right != null ) // If there's a right subtree root.right = right.duplicate( ); // Duplicate; attach return root; // Return resulting tree } public int getElement( ) { return element; } public BinaryNode getLeft( ) { return left; } public BinaryNode getRight( ) { return right; } public void setElement( int x ) { element = x; } public void setLeft( BinaryNode t ) { left = t; } public void setRight( BinaryNode t ) { right = t; } private int element; private BinaryNode left; private BinaryNode right; }

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Binomial Heap
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education