Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In Java-- Binary Search Tree for decision tree just yes or no about the exsistan

ID: 3596972 • Letter: I

Question

In Java--

Binary Search Tree for decision tree
just yes or no about the exsistance of the node in tree and prints out the node info

must have:

string name idata - is the place on the popular list

left node - left child
right node - right child
methods to include insert, seach. use compareTo()
in a tester make: an american cars tree insert each car into tree search
for each car in the list if found print car
nd place on most popular list print results of the methods
create another tree foreign cars tree
insert each car into tree search for each car in the list
if found print car and place on most popular list print results of the methods

Explanation / Answer

Code :


import java.io.*;

//tree node class
class Node {
String idata;
Node lChild, rChild;

public Node(String data) {
idata = data;
lChild = rChild = null;
}
}


//binary search tree class
class BST {

Node root;
  
BST() {
root = null;
}
  
//inserts string
void insert(String data) {
root = insertUtil(root, data);
}
  
Node insertUtil(Node root, String data) {
if (root == null) {
root = new Node(data);
return root;
}
  
if (data.compareTo(root.idata) < 0)
root.lChild = insertUtil(root.lChild, data);
else if (data.compareTo(root.idata) > 0)
root.rChild = insertUtil(root.rChild, data);
  
return root;
}
  
//searches string
Boolean search(String data){
return searchUtil(root,data);
}
  
Boolean searchUtil(Node root,String data) {
  
if (root == null) {
return false;
}
  
if(data.compareTo(root.idata) == 0)
return true;
else if (data.compareTo(root.idata) < 0)
return searchUtil(root.lChild, data);
else
return searchUtil(root.rChild, data);
}

}

//Test class for bst
public class Tester{

public static void main(String[] args) {
  
//list of american,forein and popular cars
String[] americanCarsList = {"Ford","Chevrolet","Jeep","Dodge"};
String[] foreignCarsList = {"Honda","Audi","BMW","Mercedes"};
String[] popularCarsList = {"Chevrolet","Audi","Mercedes"};
  
//insert american cars in american cars tree
BST americanCarsTree = new BST();
for(int i=0;i<americanCarsList.length;i++)
americanCarsTree.insert(americanCarsList[i]);
  
//search if popular car exist in american cars tree
for(int i=0;i<popularCarsList.length;i++){
if(americanCarsTree.search(popularCarsList[i])){
System.out.println("Popular car "+popularCarsList[i]+" position "+i+" Found in american cars tree");
}
}
  
//insert foreign cars in foreign cars tree
BST foreignCarsTree = new BST();
for(int i=0;i<foreignCarsList.length;i++)
foreignCarsTree.insert(foreignCarsList[i]);
  
//search if popular car exist in foreign cars tree
for(int i=0;i<popularCarsList.length;i++){
if(foreignCarsTree.search(popularCarsList[i])){
System.out.println("Popular car "+popularCarsList[i]+" position "+i+" Found in foreign cars tree");
}
}
  
}
}


Output :

Popular car Chevrolet position 0 Found in american cars tree
Popular car Audi position 1 Found in foreign cars tree
Popular car Mercedes position 2 Found in foreign cars tree

**If you have any query , please feel free to comment with details.
**Happy leaning :)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote