Question

please use java application to solve methods: 5,6,10,12,13 (no need for the rest)

Problem1: Create a new Java Application that has the following methods: 6 A method that generate a binary search tree (BST) w

0 0
Add a comment Improve this question Transcribed image text
Answer #1

ALL COMBINED METHODS:

import java.util.Queue;
import java.util.LinkedList;
// class defining BST NODE
class Node{

   int value;
   Node left, right;

   Node(int value) { //constructor for assigning new node
       this.value = value;
       left = right = null;
   }
}

class BinarySearchTree {

   static Node head;
   //method to insert the value in the bst
   Node insert(Node node, int value) {
      
//if tree is empty insert at root
       if (node == null) {
           return (new Node(value));
       } else {
          
           // Otherwise,find the correct place
           if (value <= node.value) {
               node.left = insert(node.left,value);
           } else {
               node.right = insert(node.right,value);
           }

      
           return node;
       }
   }

//pass bst to find the minimum value
   int minimum(Node node) {
       Node temp = node;

       //minimum value is at the leftmost place so we keep on going down
       //the tree till we find leftmost node
       while (temp.left != null) {
           temp = temp.left;
       }
       return (temp.value);
   }
  
   int maximum(Node node) {
       Node temp = node;

       //minimum value is at the rightmost place so we keep on going down
       //the tree till we find leftmost node
       while (temp.right != null) {
           temp = temp.right;
       }
       return (temp.value);
   }
   //recucrsive class to count the number of even nodes in BST
   void countEvenRec(Node node,Count c){
   if(node==null)
   return;
   if(node.value%2==0)
   c.c_even++;
   countEvenRec(node.left,c);
   countEvenRec(node.right,c);
  
  
}
static class Count{ // class to sstore static variable to count even nodes
static int c_even;
}

int countEven(Node node){ // method to call the recursive method to count number of even nodes
Count c =new Count();
countEvenRec(node,c);
return c.c_even;
}

boolean Identical_Bst(Node root1, Node root2)
{
// Check if both the trees are empty
if (root1 == null && root2 ==null)
return true;
// If any one of the tree is non-empty
// and other is empty, return false
else if (root1 !=null && root2 ==null)
return false;
else if (root1 == null && root2 !=null)
return false;
else {
//simultaneously check for both the trees
//if both have same value at current node then identical otherwise not
if (root1.value == root2.value && Identical_Bst(root1.left, root2.left)
&& Identical_Bst(root1.right, root2.right))
return true;
else
return false;
}
}


void BFS(Node root)
{
Queue<Node> q = new LinkedList<Node>(); //create a q of nodes
q.add(root);
while (!q.isEmpty())
{
  
// pollremoves the present head.
Node temp = q.poll();
System.out.print(temp.value + " ");
  
/*Enqueue left child */
if (temp.left != null) {
q.add(temp.left);
}
  
/*Enqueue right child */
if (temp.right != null) {
q.add(temp.right);
}
}
}
  
}
public class Main{
   public static void main(String[] args) {
       BinarySearchTree t = new BinarySearchTree();
       Node root = null;
       root = t.insert(root,10);
       t.insert(root,25);
       t.insert(root,6);
       t.insert(root,4);
       t.insert(root,22);
       BinarySearchTree t1 = new BinarySearchTree();
       Node root1 = null;
       root1 = t.insert(root1,10);
       t.insert(root1,25);
       t.insert(root1,6);
       t.insert(root1,4);
       t.insert(root1,22);

System.out.println("Maximum value of BST is " + t.maximum(root));
System.out.println("Minimum value of BST is " + t.minimum(root));
System.out.println("Number of even value in BST is " + t.countEven(root));
if(t.Identical_Bst(root,root1))
System.out.println("BST 1 and 2 are identical");
else
System.out.println("BST 1 and 2 are not identical");
System.out.println("BFS traversal of tree 1 is ");
       t.BFS(root);
   }
}

INDIVIDUAL METHODS :

5)

int maximum(Node node) {
       Node temp = node;

       //minimum value is at the rightmost place so we keep on going down
       //the tree till we find leftmost node
       while (temp.right != null) {
           temp = temp.right;
       }
       return (temp.value);
   }

6)

int minimum(Node node) {
       Node temp = node;

       //minimum value is at the leftmost place so we keep on going down
       //the tree till we find leftmost node
       while (temp.left != null) {
           temp = temp.left;
       }
       return (temp.value);
   }

10)

   //recucrsive class to count the number of even nodes in BST
   void countEvenRec(Node node,Count c){
   if(node==null)
   return;
   if(node.value%2==0)
   c.c_even++;
   countEvenRec(node.left,c);
   countEvenRec(node.right,c);
  
  
}
static class Count{ // class to sstore static variable to count even nodes
static int c_even;
}

int countEven(Node node){ // method to call the recursive method to count number of even nodes
Count c =new Count();
countEvenRec(node,c);
return c.c_even;
}

12)

boolean Identical_Bst(Node root1, Node root2)
{
// Check if both the trees are empty
if (root1 == null && root2 ==null)
return true;
// If any one of the tree is non-empty
// and other is empty, return false
else if (root1 !=null && root2 ==null)
return false;
else if (root1 == null && root2 !=null)
return false;
else {
//simultaneously check for both the trees
//if both have same value at current node then identical otherwise not
if (root1.value == root2.value && Identical_Bst(root1.left, root2.left)
&& Identical_Bst(root1.right, root2.right))
return true;
else
return false;
}
}

13)

void BFS(Node root)
{
Queue<Node> q = new LinkedList<Node>(); //create a q of nodes
q.add(root);
while (!q.isEmpty())
{
  
// pollremoves the present head.
Node temp = q.poll();
System.out.print(temp.value + " ");
  
/*Enqueue left child */
if (temp.left != null) {
q.add(temp.left);
}
  
/*Enqueue right child */
if (temp.right != null) {
q.add(temp.right);
}
}
}
  
}

Add a comment
Know the answer?
Add Answer to:
please use java application to solve methods: 5,6,10,12,13 (no need for the rest) Problem1: Create a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a new Java Application that has the following methods: A method that generate a binary...

    Create a new Java Application that has the following methods: A method that generate a binary search tree (BST) where the number of nodes and the range of the values are from a file. A recursive method to print the BST in preorder traversal A recursive method to print the BST in post-order traversal A recursive method to print the BST in in-order traversal A recursive method to count the number of all nodes in BST A method to find...

  • Problemi: Create a new Java Application that has the following methods: 1. A method that generate...

    Problemi: Create a new Java Application that has the following methods: 1. A method that generate a binary search tree (BST) where the number of nodes and the range of the values are from a file 2. A recursive method to print the BST in preorder traversal 3. A recursive method to print the BST in post-order traversal 4. A recursive method to print the BST in in-order traversal 5. A method to find the largest value in a BST...

  • *** using java application Q1. BST: 1. Write a method that takes an array A, as parameter (A contains the elements of a BST traversed in preorder) and returns the corresponding BST 2. Write a method...

    *** using java application Q1. BST: 1. Write a method that takes an array A, as parameter (A contains the elements of a BST traversed in preorder) and returns the corresponding BST 2. Write a method to list the nodes on the path from the root to a given node in the BST. 3. Write a recursive method to check if two BSTs are same. 4. Write a non-recursive method to check if two BSTs are same 5. Write a...

  • using java to write,show me the output. please write some common. You CAN NOT use inbuild...

    using java to write,show me the output. please write some common. You CAN NOT use inbuild functions for Tree ADT operations. using code below to finsih public class Main {    public static void main(String[] args) {        BinaryTree tree = new BinaryTree(); tree.root = new Node(1); tree.root.left = new Node(2); tree.root.right = new Node(3); tree.root.left.left = new Node(4); tree.root.left.right = new Node(5); tree.root.right.left = new Node(6); tree.root.right.right = new Node(7); tree.root.left.left.left = new Node(8); tree.root.left.left .right= new Node(9);...

  • You will be given several strings full of different keyboard characters, some of which are letters...

    You will be given several strings full of different keyboard characters, some of which are letters of the alphabet. Write a java program that creates a binary tree that uses only the alphabetical characters (a-z, A-Z) as the value within the leaf nodes using recursion and preorder traversal. All other values within the tree (either the root node or internal nodes) will be null. You may create any methods you see fit, so long as the final binary tree is...

  • 1. Write a recursive function that returns the sum of all even integers in a LinkedBinaryTree. Yo...

    please explain each line of code! ( in python ) 1. Write a recursive function that returns the sum of all even integers in a LinkedBinaryTree. Your function should take one parameter, root node. You may assume that the tree only contains integers. You may not call any methods from the LinkedBinaryTree class. Specifically, you should traverse the tree in your function def binary tree even sum (root): Returns the sum of al1 even integers in the binary tree 2....

  • Would appreciate the answer in the Java coding language please and thank you! 10d 10h left...

    Would appreciate the answer in the Java coding language please and thank you! 10d 10h left Java 7 1. Check the Structure Autocomplete Ready 1 > import java.io.*;... 10 ALL A binary tree uses a multi-node data structure where each node may have 0 to 2 child nodes, and has one stored value, its node number in this case. A tree may either be: 11 class Result { * Complete the 'isValid' function below. • An empty tree, the root...

  • java A University would like to implement its students registery as a binary search tree, calledStudentBST....

    java A University would like to implement its students registery as a binary search tree, calledStudentBST. Write an Student node class, called StudentNode, to hold the following information about an Student: - id (as a int) - gpa (as double) StudentNode should have constructors and methods (getters, setters, and toString()) to manage Write the StudentBST class, which is a binary search tree to hold objects of the class StudentNode. The key in each node is the id. : import.java.ArrayList; public...

  • In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT {...

    In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT {    private BinaryTreeNode root;    /**    * Creates an empty binary tree.    */    public LinkedBinaryTree() {        root = null;    }    /**    * Creates a binary tree from an existing root.    */    public LinkedBinaryTree(BinaryTreeNode root) {        this.root = root;    }    /**    * Creates a binary tree with the specified element...

  • Please help me with me. I did the first part to write the operations but in...

    Please help me with me. I did the first part to write the operations but in the driver its shows me errors, can't fix it. Help me. Need help with this to run the application. /** * BinaryTreeNode represents a node in a binary tree with a left and * right child. * * @author Java Foundations * @version 4.0 */ public class BinaryTreeNode { protected T element; protected BinaryTreeNode left, right; /** * Creates a new tree node with...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT