this is in java, please help create two classes: a demo class and a class to con
ID: 3572332 • Letter: T
Question
this is in java, please help
create two classes: a demo class and a class to contain the search method that will traverse a binary tree and find a specified value. The demo class should create the binary tree with a minimum of 10 values. You may make this a binary search tree, or just a binary tree (that is to say the values are put into the tree ordered, or not ordered). This should be a complete tree. Create your tree as a linked list, not an array. The demo program will then ask the user to enter a value to search for, and then return a statement indicating if it found it or not. Name the demo file (the file with the main method) TreeSearch.java
Explanation / Answer
import java.util.Random;
import java.util.Scanner;
/* Class TreeSearch */
class TreeSearch
{
TreeSearch left, right;
int data;
/* Constructor */
public TreeSearch()
{
left = null;
right = null;
data = 0;
}
/* Constructor */
public TreeSearch(int n)
{
left = null;
right = null;
data = n;
}
/* Function to set the left node */
public void setLeft(TreeSearch n)
{
left = n;
}
/* Function to set the right node */
public void setRight(TreeSearch n)
{
right = n;
}
/* Function to get the left node */
public TreeSearch getLeft()
{
return left;
}
/* Function to get the right node */
public TreeSearch getRight()
{
return right;
}
/* Function to set data to the node */
public void setData(int d)
{
data = d;
}
/* Function to get data from the nodes */
public int getData()
{
return data;
}
}
/* Class Tree */
class Tree
{
private TreeSearch root;
/* Constructor */
public Tree()
{
root = null;
}
/* Function to check if the tree is empty */
public boolean isEmpty()
{
return root == null;
}
/* Functions to insert the data into the tree */
public void insert(int data)
{
root = insert(root, data);
}
/* Function for inserting data recursively */
private TreeSearch insert(TreeSearch node, int data)
{
if (node == null)
node = new TreeSearch(data);
else
{
if (data <= node.getData())
node.left = insert(node.left, data);
else
node.right = insert(node.right, data);
}
return node;
}
/* Function to count number of nodes */
public int countNodes()
{
return countNodes(root);
}
/* Function that counts number of nodes recursively */
private int countNodes(TreeSearch r)
{
if (r == null)
return 0;
else
{
int l = 1;
l += countNodes(r.getLeft());
l += countNodes(r.getRight());
return l;
}
}
/* to search an element */
public boolean search(int val)
{
return search(root, val);
}
/* Function to perform search of an element recursively */
private boolean search(TreeSearch r, int val)
{
boolean found = false;
while ((r !== null) && !found)
{
int rval = r.getData();
if (val < rval)
{
r = r.getLeft();
}
else if (val > rval)
{ r = r.getRight();
} else
{
found = true;
break;
}
found = search(r, val);
}
return found;
}
/* for inorder traversal */
public void inorder()
{
inorder(root);
}
private void inorder(TreeSearch r)
{
if (r != null)
{
inorder(r.getLeft());
System.out.print(r.getData() + " ");
inorder(r.getRight());
}
}
}
public class Search_Element_Tree
{
public static int N = 10;
public static void main(String args[])
{
Random random = new Random();
Tree bst = new Tree();
for (int i = 0; i < N; i++)
bst.insert(Math.abs(random.nextInt(100)));
System.out.print("Inorder traversal of the tree is : ");
bst.inorder();
System.out.println(" Enter the element that is to be searched: ");
Scanner sc = new Scanner(System.in);
System.out.println("Search result : " + bst.search(sc.nextInt()));
sc.close();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.