package Lab11; public class BinaryTree { private BinaryTreeNode root; public Bin
ID: 3734436 • Letter: P
Question
package Lab11;
public class BinaryTree {
private BinaryTreeNode root;
public BinaryTree(){
root=null;
}
public boolean isEmpty(){
return root==null;
}
public BinaryTreeNode insert(BinaryTreeNode node, int data)
{
//Implement the insert method here
}
public void insert(int data){
root=insert(root, data);
}
private int countNodes(BinaryTreeNode r){
if(r==null)
return 0;
else
{
int number=1;
number+=countNodes(r.getLeft());
number+=countNodes(r.getRight());
return number;
}
}
public int countNodes(){
return countNodes(root);
}
public boolean search(BinaryTreeNode r, int data){
if(r.getData()==data)
return true;
if(r.getLeft()!=null)
if(search(r.getLeft(),data))
return true;
if(r.getRight()!=null)
if(search(r.getRight(),data))
return true;
return false;
}
public boolean search(int val){
return search(root, val);
}
private void inorder(BinaryTreeNode r){
if(r!=null){
inorder(r.getLeft());
System.out.println(r.getData()+"");
inorder(r.getRight());
}
}
public void inorder(){
inorder(root);
}
private void preorder(BinaryTreeNode r){
if(r!=null){
System.out.println(r.getData()+"");
preorder(r.getLeft());
preorder(r.getRight());
}
}
public void preorder(){
preorder(root);
}
private void postorder(BinaryTreeNode r){
if(r!=null){
postorder(r.getLeft());
postorder(r.getRight());
System.out.println(r.getData()+"");
}
}
public void postorder(){
postorder(root);
}
}
package Lab11;
public class BinaryTreeNode {
BinaryTreeNode left;
BinaryTreeNode right;
int data;
public BinaryTreeNode(){
left=null;
right=null;
data=0;
}
public BinaryTreeNode(int value){
left=null;
right=null;
data=value;
}
public void setLeft(BinaryTreeNode node){
left=node;
}
public void setRight(BinaryTreeNode node){
right=node;
}
public void setData(int value){
data=value;
}
public BinaryTreeNode getLeft(){
return left;
}
public BinaryTreeNode getRight(){
return right;
}
public int getData(){
return data;
}
}
package Lab11;
import java.util.Scanner;
public class BinaryTreeTest {
public static void main(String[] args){
//produce the sequence here
//insert the data to the binary search tree here
//perform the sorting here.
}
}
whole Java code ,thanks
Sorting A Sequence Using the Binary Search Tree. Instructions: A) Goal: get experience with binary search tree. B) Task: 1) Produce a sequence of 15 random integers, print this sequence preceding with (Points:20/100) Unsorted" In the attached java class named BinarvTree, re-design the insert method to produce a binary search tree. 2) (Points: 60/100) 3) Using the inorder method to sort the sequence, print the sorted sequence (Points: 20/100) c) Procedure: the attached classes 1. download 2. implement the insert method. 3. Run the Test class.Explanation / Answer
public class BinaryTree {
private BinaryTreeNode root;
public BinaryTree(){
root=null;
}
public boolean isEmpty(){
return root==null;
}
public BinaryTreeNode insert(BinaryTreeNode node, int key)
{
/* If the tree is empty, return a new node */
if (node == null) {
node = new BinaryTreeNode(key);
return node;
}
/* Otherwise, recur down the tree */
if (key < node.getData())
node.setLeft(insert(node.getLeft(), key));
else if (key > node.getData())
node.setRight(insert(node.getRight(), key));
/* return the (unchanged) node pointer */
return node;
}
public void insert(int data){
root=insert(root, data);
}
private int countNodes(BinaryTreeNode r){
if(r==null)
return 0;
else
{
int number=1;
number+=countNodes(r.getLeft());
number+=countNodes(r.getRight());
return number;
}
}
public int countNodes(){
return countNodes(root);
}
public boolean search(BinaryTreeNode r, int data){
if(r.getData()==data)
return true;
if(r.getLeft()!=null)
if(search(r.getLeft(),data))
return true;
if(r.getRight()!=null)
if(search(r.getRight(),data))
return true;
return false;
}
public boolean search(int val){
return search(root, val);
}
private void inorder(BinaryTreeNode r){
if(r!=null){
inorder(r.getLeft());
System.out.println(r.getData()+"");
inorder(r.getRight());
}
}
public void inorder(){
inorder(root);
}
private void preorder(BinaryTreeNode r){
if(r!=null){
System.out.println(r.getData()+"");
preorder(r.getLeft());
preorder(r.getRight());
}
}
public void preorder(){
preorder(root);
}
private void postorder(BinaryTreeNode r){
if(r!=null){
postorder(r.getLeft());
postorder(r.getRight());
System.out.println(r.getData()+"");
}
}
public void postorder(){
postorder(root);
}
}
###############
import java.util.Random;
import java.util.Scanner;
public class BinaryTreeTest {
public static void main(String[] args){
BinaryTree tree = new BinaryTree();
//produce the sequence here
Random r = new Random();
System.out.print("Unsorted: ");
for(int i=0; i<15; i++) {
int rand = r.nextInt(100);
System.out.print(rand+" ");
//insert the data to the binary search tree here
tree.insert(rand);
}
System.out.println();
//perform the sorting here.
tree.inorder();
}
}
/*
Sample run:
Unsorted: 85 46 85 35 74 28 27 11 26 63 89 88 57 93 88
11
26
27
28
35
46
57
63
74
85
88
89
93
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.