SlideShare a Scribd company logo
1 of 7
Download to read offline
Need Help with this Java Assignment. Program should be done in JAVA please
Complete required scripts based on eclipse and troubleshoot the scripts until the tasks are done.
1. Implement a binary search tree (write the code for it), for a height of 2 or higher. Please show
the output if you can Thank you in advance!
Solution
I have compiled this code and checked for errors, worked fine.
Also after the code I have pasted the Output transcript to show what User Inputs I gave and what
all outputs I got.
// Java Program to Implement Binary Tree
import java.util.Scanner;
/* Define Class BinaryTreeNode */
class BinaryTreeNode
{
BinaryTreeNode left, right;
int data;
/* Constructor */
public BinaryTreeNode()
{
left = null;
right = null;
data = 0;
}
/* Constructor */
public BinaryTreeNode(int n)
{
left = null;
right = null;
data = n;
}
/* Function to set left node */
public void setLeft(BinaryTreeNode n)
{
left = n;
}
/* Function to set right node */
public void setRight(BinaryTreeNode n)
{
right = n;
}
/* Function to get left node */
public BinaryTreeNode getLeft()
{
return left;
}
/* Function to get right node */
public BinaryTreeNode getRight()
{
return right;
}
/* Function to set data to node */
public void setData(int d)
{
data = d;
}
/* Function to get data from node */
public int getData()
{
return data;
}
}
/* Define anothe Class Operations to perform required operationns on Binary Tree*/
class Operations
{
private BinaryTreeNode root;
/* Constructor */
public Operations()
{
root = null;
}
/* Function to check if tree is empty */
public boolean isEmpty()
{
return root == null;
}
/* Functions to insert data */
public void insert(int data)
{
root = insert(root, data);
}
/* Function to insert data recursively */
private BinaryTreeNode insert(BinaryTreeNode node, int data)
{
if (node == null)
node = new BinaryTreeNode(data);
else
{
if (node.getRight() == null)
node.right = insert(node.right, data);
else
node.left = insert(node.left, data);
}
return node;
}
/* Function to count number of nodes */
public int countNodes()
{
return countNodes(root);
}
/* Function to count number of nodes recursively */
private int countNodes(BinaryTreeNode r)
{
if (r == null)
return 0;
else
{
int l = 1;
l += countNodes(r.getLeft());
l += countNodes(r.getRight());
return l;
}
}
/* Function to search for an element */
public boolean search(int val)
{
return search(root, val);
}
/* Function to search for an element recursively */
private boolean search(BinaryTreeNode r, int val)
{
if (r.getData() == val)
return true;
if (r.getLeft() != null)
if (search(r.getLeft(), val))
return true;
if (r.getRight() != null)
if (search(r.getRight(), val))
return true;
return false;
}
/* Function for inorder traversal */
public void inorder()
{
inorder(root);
}
private void inorder(BinaryTreeNode r)
{
if (r != null)
{
inorder(r.getLeft());
System.out.print(r.getData() +" ");
inorder(r.getRight());
}
}
/* Function for preorder traversal */
public void preorder()
{
preorder(root);
}
private void preorder(BinaryTreeNode r)
{
if (r != null)
{
System.out.print(r.getData() +" ");
preorder(r.getLeft());
preorder(r.getRight());
}
}
/* Function for postorder traversal */
public void postorder()
{
postorder(root);
}
private void postorder(BinaryTreeNode r)
{
if (r != null)
{
postorder(r.getLeft());
postorder(r.getRight());
System.out.print(r.getData() +" ");
}
}
}
/* Class Main */
public class Main
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
/* Creating object of Operations */
Operations op = new Operations();
/* Perform tree operations */
System.out.println("Binary Tree Test ");
char ch;
do
{
System.out.println(" Operation to be performed on Binary Tree ");
System.out.println("1.Insert ");
System.out.println("2.Search");
System.out.println("3.Count Nodes");
System.out.println("4.Check Empty");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter integer element to insert");
op.insert( scan.nextInt() );
break;
case 2 :
System.out.println("Enter integer element to search");
System.out.println("Search result : "+ op.search( scan.nextInt() ));
break;
case 3 :
System.out.println("Nodes = "+ op.countNodes());
break;
case 4 :
System.out.println("Empty status = "+ op.isEmpty());
break;
default :
System.out.println("Wrong Entry  ");
break;
}
/* Display tree */
System.out.print(" Post order : ");
op.postorder();
System.out.print(" Pre order : ");
op.preorder();
System.out.print(" In order : ");
op.inorder();
System.out.println("  Do you want to continue (Type y or n)  ");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');
}
}
Output transcript

More Related Content

Similar to Need Help with this Java Assignment. Program should be done in JAVA .pdf

Given BinaryNode.javapackage util;import java.util.;T.pdf
Given BinaryNode.javapackage util;import java.util.;T.pdfGiven BinaryNode.javapackage util;import java.util.;T.pdf
Given BinaryNode.javapackage util;import java.util.;T.pdfConint29
 
Please write in C++ and should be able to compile and debug.Thank yo.pdf
Please write in C++ and should be able to compile and debug.Thank yo.pdfPlease write in C++ and should be able to compile and debug.Thank yo.pdf
Please write in C++ and should be able to compile and debug.Thank yo.pdfajaycosmeticslg
 
Given the following codepackage data1;import java.util.;p.pdf
Given the following codepackage data1;import java.util.;p.pdfGiven the following codepackage data1;import java.util.;p.pdf
Given the following codepackage data1;import java.util.;p.pdfillyasraja7
 
Please i need help on following program using C++ Language.Add the.pdf
Please i need help on following program using C++ Language.Add the.pdfPlease i need help on following program using C++ Language.Add the.pdf
Please i need help on following program using C++ Language.Add the.pdfezzi552
 
Please read the comment ins codeExpressionTree.java-------------.pdf
Please read the comment ins codeExpressionTree.java-------------.pdfPlease read the comment ins codeExpressionTree.java-------------.pdf
Please read the comment ins codeExpressionTree.java-------------.pdfshanki7
 
Please help me to make a programming project I have to sue them today- (1).pdf
Please help me to make a programming project I have to sue them today- (1).pdfPlease help me to make a programming project I have to sue them today- (1).pdf
Please help me to make a programming project I have to sue them today- (1).pdfseoagam1
 
Help please!!(Include your modified DList.java source code file in.pdf
Help please!!(Include your modified DList.java source code file in.pdfHelp please!!(Include your modified DList.java source code file in.pdf
Help please!!(Include your modified DList.java source code file in.pdfjyothimuppasani1
 
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdfHere is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdfarrowmobile
 
package singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdfpackage singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdfamazing2001
 
Lecture 5: Functional Programming
Lecture 5: Functional ProgrammingLecture 5: Functional Programming
Lecture 5: Functional ProgrammingEelco Visser
 
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdfJAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdffantasiatheoutofthef
 
C++ BinaryTree Help Creating main function for Trees...Here are .pdf
C++ BinaryTree Help  Creating main function for Trees...Here are .pdfC++ BinaryTree Help  Creating main function for Trees...Here are .pdf
C++ BinaryTree Help Creating main function for Trees...Here are .pdfforecastfashions
 
Please finish everything marked TODO BinaryNode-java public class Bi.docx
Please finish everything marked TODO   BinaryNode-java public class Bi.docxPlease finish everything marked TODO   BinaryNode-java public class Bi.docx
Please finish everything marked TODO BinaryNode-java public class Bi.docxJakeT2gGrayp
 
C++ code, please help! RESPOND W COMPLETED CODE PLEASE, am using V.pdf
C++ code, please help! RESPOND W COMPLETED CODE PLEASE,  am using V.pdfC++ code, please help! RESPOND W COMPLETED CODE PLEASE,  am using V.pdf
C++ code, please help! RESPOND W COMPLETED CODE PLEASE, am using V.pdfrahulfancycorner21
 
Swift Programming Language
Swift Programming LanguageSwift Programming Language
Swift Programming LanguageAnıl Sözeri
 
can you add a delete button and a add button to the below program. j.pdf
can you add a delete button and a add button to the below program. j.pdfcan you add a delete button and a add button to the below program. j.pdf
can you add a delete button and a add button to the below program. j.pdfsales88
 
Create an implementation of a binary tree using the recursive appr.pdf
Create an implementation of a binary tree using the recursive appr.pdfCreate an implementation of a binary tree using the recursive appr.pdf
Create an implementation of a binary tree using the recursive appr.pdffederaleyecare
 
Introduction to Dart
Introduction to DartIntroduction to Dart
Introduction to DartRamesh Nair
 
Assg 12 Binary Search Trees COSC 2336assg-12.cppAssg 12 Binary .docx
Assg 12 Binary Search Trees COSC 2336assg-12.cppAssg 12 Binary .docxAssg 12 Binary Search Trees COSC 2336assg-12.cppAssg 12 Binary .docx
Assg 12 Binary Search Trees COSC 2336assg-12.cppAssg 12 Binary .docxfestockton
 

Similar to Need Help with this Java Assignment. Program should be done in JAVA .pdf (20)

Given BinaryNode.javapackage util;import java.util.;T.pdf
Given BinaryNode.javapackage util;import java.util.;T.pdfGiven BinaryNode.javapackage util;import java.util.;T.pdf
Given BinaryNode.javapackage util;import java.util.;T.pdf
 
Please write in C++ and should be able to compile and debug.Thank yo.pdf
Please write in C++ and should be able to compile and debug.Thank yo.pdfPlease write in C++ and should be able to compile and debug.Thank yo.pdf
Please write in C++ and should be able to compile and debug.Thank yo.pdf
 
Given the following codepackage data1;import java.util.;p.pdf
Given the following codepackage data1;import java.util.;p.pdfGiven the following codepackage data1;import java.util.;p.pdf
Given the following codepackage data1;import java.util.;p.pdf
 
Please i need help on following program using C++ Language.Add the.pdf
Please i need help on following program using C++ Language.Add the.pdfPlease i need help on following program using C++ Language.Add the.pdf
Please i need help on following program using C++ Language.Add the.pdf
 
Please read the comment ins codeExpressionTree.java-------------.pdf
Please read the comment ins codeExpressionTree.java-------------.pdfPlease read the comment ins codeExpressionTree.java-------------.pdf
Please read the comment ins codeExpressionTree.java-------------.pdf
 
Please help me to make a programming project I have to sue them today- (1).pdf
Please help me to make a programming project I have to sue them today- (1).pdfPlease help me to make a programming project I have to sue them today- (1).pdf
Please help me to make a programming project I have to sue them today- (1).pdf
 
Help please!!(Include your modified DList.java source code file in.pdf
Help please!!(Include your modified DList.java source code file in.pdfHelp please!!(Include your modified DList.java source code file in.pdf
Help please!!(Include your modified DList.java source code file in.pdf
 
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdfHere is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
 
package singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdfpackage singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdf
 
Lecture 5: Functional Programming
Lecture 5: Functional ProgrammingLecture 5: Functional Programming
Lecture 5: Functional Programming
 
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdfJAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
 
C++ BinaryTree Help Creating main function for Trees...Here are .pdf
C++ BinaryTree Help  Creating main function for Trees...Here are .pdfC++ BinaryTree Help  Creating main function for Trees...Here are .pdf
C++ BinaryTree Help Creating main function for Trees...Here are .pdf
 
Please finish everything marked TODO BinaryNode-java public class Bi.docx
Please finish everything marked TODO   BinaryNode-java public class Bi.docxPlease finish everything marked TODO   BinaryNode-java public class Bi.docx
Please finish everything marked TODO BinaryNode-java public class Bi.docx
 
C++ code, please help! RESPOND W COMPLETED CODE PLEASE, am using V.pdf
C++ code, please help! RESPOND W COMPLETED CODE PLEASE,  am using V.pdfC++ code, please help! RESPOND W COMPLETED CODE PLEASE,  am using V.pdf
C++ code, please help! RESPOND W COMPLETED CODE PLEASE, am using V.pdf
 
Swift Programming Language
Swift Programming LanguageSwift Programming Language
Swift Programming Language
 
can you add a delete button and a add button to the below program. j.pdf
can you add a delete button and a add button to the below program. j.pdfcan you add a delete button and a add button to the below program. j.pdf
can you add a delete button and a add button to the below program. j.pdf
 
Create an implementation of a binary tree using the recursive appr.pdf
Create an implementation of a binary tree using the recursive appr.pdfCreate an implementation of a binary tree using the recursive appr.pdf
Create an implementation of a binary tree using the recursive appr.pdf
 
Introduction to Dart
Introduction to DartIntroduction to Dart
Introduction to Dart
 
Assg 12 Binary Search Trees COSC 2336assg-12.cppAssg 12 Binary .docx
Assg 12 Binary Search Trees COSC 2336assg-12.cppAssg 12 Binary .docxAssg 12 Binary Search Trees COSC 2336assg-12.cppAssg 12 Binary .docx
Assg 12 Binary Search Trees COSC 2336assg-12.cppAssg 12 Binary .docx
 
Binary tree in java
Binary tree in javaBinary tree in java
Binary tree in java
 

More from archiesgallery

Is it possible to provide health care without rationing In 1948 eve.pdf
Is it possible to provide health care without rationing In 1948 eve.pdfIs it possible to provide health care without rationing In 1948 eve.pdf
Is it possible to provide health care without rationing In 1948 eve.pdfarchiesgallery
 
In what structure does meiosis occur in the bryophytesSolution.pdf
In what structure does meiosis occur in the bryophytesSolution.pdfIn what structure does meiosis occur in the bryophytesSolution.pdf
In what structure does meiosis occur in the bryophytesSolution.pdfarchiesgallery
 
How was it established that particular phenotypes are inherited as a.pdf
How was it established that particular phenotypes are inherited as a.pdfHow was it established that particular phenotypes are inherited as a.pdf
How was it established that particular phenotypes are inherited as a.pdfarchiesgallery
 
Hello. This program has to be done in Eclipse(Program used to write .pdf
Hello. This program has to be done in Eclipse(Program used to write .pdfHello. This program has to be done in Eclipse(Program used to write .pdf
Hello. This program has to be done in Eclipse(Program used to write .pdfarchiesgallery
 
Given the following homogeneous ODE 2y + 12y + 68y = 0 with initial .pdf
Given the following homogeneous ODE  2y + 12y + 68y = 0 with initial .pdfGiven the following homogeneous ODE  2y + 12y + 68y = 0 with initial .pdf
Given the following homogeneous ODE 2y + 12y + 68y = 0 with initial .pdfarchiesgallery
 
For an artist to be considered an outsider, he or she must first be.pdf
For an artist to be considered an outsider, he or she must first be.pdfFor an artist to be considered an outsider, he or she must first be.pdf
For an artist to be considered an outsider, he or she must first be.pdfarchiesgallery
 
Find a article or news about Biological ScienceIdentify source (in.pdf
Find a article or news about Biological ScienceIdentify source (in.pdfFind a article or news about Biological ScienceIdentify source (in.pdf
Find a article or news about Biological ScienceIdentify source (in.pdfarchiesgallery
 
Discuss in detail the importance of either ageneral standardstan.pdf
Discuss in detail the importance of either ageneral standardstan.pdfDiscuss in detail the importance of either ageneral standardstan.pdf
Discuss in detail the importance of either ageneral standardstan.pdfarchiesgallery
 
Distinguish between a blastula, a blastomere, a blastopore, and a bla.pdf
Distinguish between a blastula, a blastomere, a blastopore, and a bla.pdfDistinguish between a blastula, a blastomere, a blastopore, and a bla.pdf
Distinguish between a blastula, a blastomere, a blastopore, and a bla.pdfarchiesgallery
 
Compare and contrast an information systems development project in a.pdf
Compare and contrast an information systems development project in a.pdfCompare and contrast an information systems development project in a.pdf
Compare and contrast an information systems development project in a.pdfarchiesgallery
 
can high level language can use an assembler.SolutionHigh-l.pdf
can high level language can use an assembler.SolutionHigh-l.pdfcan high level language can use an assembler.SolutionHigh-l.pdf
can high level language can use an assembler.SolutionHigh-l.pdfarchiesgallery
 
Briefly describe a mechanim of virus entry into the cells and give o.pdf
Briefly describe a mechanim of virus entry into the cells and give o.pdfBriefly describe a mechanim of virus entry into the cells and give o.pdf
Briefly describe a mechanim of virus entry into the cells and give o.pdfarchiesgallery
 
Why are traditional definitions of “interception” problematic when a.pdf
Why are traditional definitions of “interception” problematic when a.pdfWhy are traditional definitions of “interception” problematic when a.pdf
Why are traditional definitions of “interception” problematic when a.pdfarchiesgallery
 
why is it important for macromolecular cellular structures to be fle.pdf
why is it important for macromolecular cellular structures to be fle.pdfwhy is it important for macromolecular cellular structures to be fle.pdf
why is it important for macromolecular cellular structures to be fle.pdfarchiesgallery
 
Widgets ’R Us (WRU) is a medium-sized firm specializing in the desig.pdf
Widgets ’R Us (WRU) is a medium-sized firm specializing in the desig.pdfWidgets ’R Us (WRU) is a medium-sized firm specializing in the desig.pdf
Widgets ’R Us (WRU) is a medium-sized firm specializing in the desig.pdfarchiesgallery
 
Which of the following entities can be homozygous (there may be mor.pdf
Which of the following entities can be homozygous (there may be mor.pdfWhich of the following entities can be homozygous (there may be mor.pdf
Which of the following entities can be homozygous (there may be mor.pdfarchiesgallery
 
Where is the lysosomes locales in the cell Where is the lysosom.pdf
Where is the lysosomes locales in the cell Where is the lysosom.pdfWhere is the lysosomes locales in the cell Where is the lysosom.pdf
Where is the lysosomes locales in the cell Where is the lysosom.pdfarchiesgallery
 
Which of the following is the most specific of security documents.pdf
Which of the following is the most specific of security documents.pdfWhich of the following is the most specific of security documents.pdf
Which of the following is the most specific of security documents.pdfarchiesgallery
 
What is the consequence when a chromosome loses its telomeresSol.pdf
What is the consequence when a chromosome loses its telomeresSol.pdfWhat is the consequence when a chromosome loses its telomeresSol.pdf
What is the consequence when a chromosome loses its telomeresSol.pdfarchiesgallery
 
We usually use when having a conversation with others. left hemisph.pdf
We usually use  when having a conversation with others.  left hemisph.pdfWe usually use  when having a conversation with others.  left hemisph.pdf
We usually use when having a conversation with others. left hemisph.pdfarchiesgallery
 

More from archiesgallery (20)

Is it possible to provide health care without rationing In 1948 eve.pdf
Is it possible to provide health care without rationing In 1948 eve.pdfIs it possible to provide health care without rationing In 1948 eve.pdf
Is it possible to provide health care without rationing In 1948 eve.pdf
 
In what structure does meiosis occur in the bryophytesSolution.pdf
In what structure does meiosis occur in the bryophytesSolution.pdfIn what structure does meiosis occur in the bryophytesSolution.pdf
In what structure does meiosis occur in the bryophytesSolution.pdf
 
How was it established that particular phenotypes are inherited as a.pdf
How was it established that particular phenotypes are inherited as a.pdfHow was it established that particular phenotypes are inherited as a.pdf
How was it established that particular phenotypes are inherited as a.pdf
 
Hello. This program has to be done in Eclipse(Program used to write .pdf
Hello. This program has to be done in Eclipse(Program used to write .pdfHello. This program has to be done in Eclipse(Program used to write .pdf
Hello. This program has to be done in Eclipse(Program used to write .pdf
 
Given the following homogeneous ODE 2y + 12y + 68y = 0 with initial .pdf
Given the following homogeneous ODE  2y + 12y + 68y = 0 with initial .pdfGiven the following homogeneous ODE  2y + 12y + 68y = 0 with initial .pdf
Given the following homogeneous ODE 2y + 12y + 68y = 0 with initial .pdf
 
For an artist to be considered an outsider, he or she must first be.pdf
For an artist to be considered an outsider, he or she must first be.pdfFor an artist to be considered an outsider, he or she must first be.pdf
For an artist to be considered an outsider, he or she must first be.pdf
 
Find a article or news about Biological ScienceIdentify source (in.pdf
Find a article or news about Biological ScienceIdentify source (in.pdfFind a article or news about Biological ScienceIdentify source (in.pdf
Find a article or news about Biological ScienceIdentify source (in.pdf
 
Discuss in detail the importance of either ageneral standardstan.pdf
Discuss in detail the importance of either ageneral standardstan.pdfDiscuss in detail the importance of either ageneral standardstan.pdf
Discuss in detail the importance of either ageneral standardstan.pdf
 
Distinguish between a blastula, a blastomere, a blastopore, and a bla.pdf
Distinguish between a blastula, a blastomere, a blastopore, and a bla.pdfDistinguish between a blastula, a blastomere, a blastopore, and a bla.pdf
Distinguish between a blastula, a blastomere, a blastopore, and a bla.pdf
 
Compare and contrast an information systems development project in a.pdf
Compare and contrast an information systems development project in a.pdfCompare and contrast an information systems development project in a.pdf
Compare and contrast an information systems development project in a.pdf
 
can high level language can use an assembler.SolutionHigh-l.pdf
can high level language can use an assembler.SolutionHigh-l.pdfcan high level language can use an assembler.SolutionHigh-l.pdf
can high level language can use an assembler.SolutionHigh-l.pdf
 
Briefly describe a mechanim of virus entry into the cells and give o.pdf
Briefly describe a mechanim of virus entry into the cells and give o.pdfBriefly describe a mechanim of virus entry into the cells and give o.pdf
Briefly describe a mechanim of virus entry into the cells and give o.pdf
 
Why are traditional definitions of “interception” problematic when a.pdf
Why are traditional definitions of “interception” problematic when a.pdfWhy are traditional definitions of “interception” problematic when a.pdf
Why are traditional definitions of “interception” problematic when a.pdf
 
why is it important for macromolecular cellular structures to be fle.pdf
why is it important for macromolecular cellular structures to be fle.pdfwhy is it important for macromolecular cellular structures to be fle.pdf
why is it important for macromolecular cellular structures to be fle.pdf
 
Widgets ’R Us (WRU) is a medium-sized firm specializing in the desig.pdf
Widgets ’R Us (WRU) is a medium-sized firm specializing in the desig.pdfWidgets ’R Us (WRU) is a medium-sized firm specializing in the desig.pdf
Widgets ’R Us (WRU) is a medium-sized firm specializing in the desig.pdf
 
Which of the following entities can be homozygous (there may be mor.pdf
Which of the following entities can be homozygous (there may be mor.pdfWhich of the following entities can be homozygous (there may be mor.pdf
Which of the following entities can be homozygous (there may be mor.pdf
 
Where is the lysosomes locales in the cell Where is the lysosom.pdf
Where is the lysosomes locales in the cell Where is the lysosom.pdfWhere is the lysosomes locales in the cell Where is the lysosom.pdf
Where is the lysosomes locales in the cell Where is the lysosom.pdf
 
Which of the following is the most specific of security documents.pdf
Which of the following is the most specific of security documents.pdfWhich of the following is the most specific of security documents.pdf
Which of the following is the most specific of security documents.pdf
 
What is the consequence when a chromosome loses its telomeresSol.pdf
What is the consequence when a chromosome loses its telomeresSol.pdfWhat is the consequence when a chromosome loses its telomeresSol.pdf
What is the consequence when a chromosome loses its telomeresSol.pdf
 
We usually use when having a conversation with others. left hemisph.pdf
We usually use  when having a conversation with others.  left hemisph.pdfWe usually use  when having a conversation with others.  left hemisph.pdf
We usually use when having a conversation with others. left hemisph.pdf
 

Recently uploaded

Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 

Recently uploaded (20)

Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 

Need Help with this Java Assignment. Program should be done in JAVA .pdf

  • 1. Need Help with this Java Assignment. Program should be done in JAVA please Complete required scripts based on eclipse and troubleshoot the scripts until the tasks are done. 1. Implement a binary search tree (write the code for it), for a height of 2 or higher. Please show the output if you can Thank you in advance! Solution I have compiled this code and checked for errors, worked fine. Also after the code I have pasted the Output transcript to show what User Inputs I gave and what all outputs I got. // Java Program to Implement Binary Tree import java.util.Scanner; /* Define Class BinaryTreeNode */ class BinaryTreeNode { BinaryTreeNode left, right; int data; /* Constructor */ public BinaryTreeNode() { left = null; right = null; data = 0; } /* Constructor */ public BinaryTreeNode(int n) { left = null; right = null; data = n; } /* Function to set left node */ public void setLeft(BinaryTreeNode n) { left = n;
  • 2. } /* Function to set right node */ public void setRight(BinaryTreeNode n) { right = n; } /* Function to get left node */ public BinaryTreeNode getLeft() { return left; } /* Function to get right node */ public BinaryTreeNode getRight() { return right; } /* Function to set data to node */ public void setData(int d) { data = d; } /* Function to get data from node */ public int getData() { return data; } } /* Define anothe Class Operations to perform required operationns on Binary Tree*/ class Operations { private BinaryTreeNode root; /* Constructor */ public Operations() { root = null; }
  • 3. /* Function to check if tree is empty */ public boolean isEmpty() { return root == null; } /* Functions to insert data */ public void insert(int data) { root = insert(root, data); } /* Function to insert data recursively */ private BinaryTreeNode insert(BinaryTreeNode node, int data) { if (node == null) node = new BinaryTreeNode(data); else { if (node.getRight() == null) node.right = insert(node.right, data); else node.left = insert(node.left, data); } return node; } /* Function to count number of nodes */ public int countNodes() { return countNodes(root); } /* Function to count number of nodes recursively */ private int countNodes(BinaryTreeNode r) { if (r == null) return 0; else {
  • 4. int l = 1; l += countNodes(r.getLeft()); l += countNodes(r.getRight()); return l; } } /* Function to search for an element */ public boolean search(int val) { return search(root, val); } /* Function to search for an element recursively */ private boolean search(BinaryTreeNode r, int val) { if (r.getData() == val) return true; if (r.getLeft() != null) if (search(r.getLeft(), val)) return true; if (r.getRight() != null) if (search(r.getRight(), val)) return true; return false; } /* Function for inorder traversal */ public void inorder() { inorder(root); } private void inorder(BinaryTreeNode r) { if (r != null) { inorder(r.getLeft()); System.out.print(r.getData() +" "); inorder(r.getRight());
  • 5. } } /* Function for preorder traversal */ public void preorder() { preorder(root); } private void preorder(BinaryTreeNode r) { if (r != null) { System.out.print(r.getData() +" "); preorder(r.getLeft()); preorder(r.getRight()); } } /* Function for postorder traversal */ public void postorder() { postorder(root); } private void postorder(BinaryTreeNode r) { if (r != null) { postorder(r.getLeft()); postorder(r.getRight()); System.out.print(r.getData() +" "); } } } /* Class Main */ public class Main { public static void main(String[] args) {
  • 6. Scanner scan = new Scanner(System.in); /* Creating object of Operations */ Operations op = new Operations(); /* Perform tree operations */ System.out.println("Binary Tree Test "); char ch; do { System.out.println(" Operation to be performed on Binary Tree "); System.out.println("1.Insert "); System.out.println("2.Search"); System.out.println("3.Count Nodes"); System.out.println("4.Check Empty"); int choice = scan.nextInt(); switch (choice) { case 1 : System.out.println("Enter integer element to insert"); op.insert( scan.nextInt() ); break; case 2 : System.out.println("Enter integer element to search"); System.out.println("Search result : "+ op.search( scan.nextInt() )); break; case 3 : System.out.println("Nodes = "+ op.countNodes()); break; case 4 : System.out.println("Empty status = "+ op.isEmpty()); break; default : System.out.println("Wrong Entry "); break; } /* Display tree */ System.out.print(" Post order : ");
  • 7. op.postorder(); System.out.print(" Pre order : "); op.preorder(); System.out.print(" In order : "); op.inorder(); System.out.println(" Do you want to continue (Type y or n) "); ch = scan.next().charAt(0); } while (ch == 'Y'|| ch == 'y'); } } Output transcript