I need a running java code with comments ///////////////////////////////////////
ID: 3847090 • Letter: I
Question
I need a running java code with comments
///////////////////////////////////////////////
//////////////////////////////////////////
Option 1: Searching a Binary Tree Option 1 is to implement a method that will search for a value in the given binary tree. Implement a recursive method called search in the Binary Tree class. Your method should take an integer parameter, which is the value it is trying to find in the binary tree. Your method should return a boolean, which is true if there is any Node in the binary tree with data equal to that value, and false otherwise. You should method should be added to the BinaryTree class (see above) Here is the method header for the binary tree search to get you started: class Binary Tree public boolean search int value 1 Your code here!Explanation / Answer
Here is the code and output for the question. Please dont forget to rate the answer if it helped. Thank you very much
public class BinaryTreeTest {
public static void main(String[] args) {
Node root, left, right;
left = insertLR(new Node(5), new Node(8), new Node(3));
right = insertLR(new Node(11), new Node(82), new Node(35));
root = insertLR(new Node(840), left, right);
BinaryTree b = new BinaryTree(root);
if(b.search(11))
System.out.println("found 11");
else
System.out.println("did not find 11");
b.sort();
System.out.println("root: "+root.data + " root.left: "+root.left.data + " root.right = "+root.right.data);
System.out.println("left: "+left.data + " left's left: "+left.left.data + " left.right = "+left.right.data);
System.out.println("right: "+right.data + " right's left: "+right.left.data + " right's right = "+right.right.data);
}
public static Node insertLR(Node root, Node left, Node right) {
root.left = left;
root.right = right;
return root;
}
}
//////////////////////////////////////////
class BinaryTree {
Node head;
public BinaryTree() {
this.head = new Node(0);
}
public BinaryTree(Node root) {
this.head = root;
}
public boolean search( int value ) {
return search(head, value); // call the recursive private method
}
private boolean search(Node n, int value)
{
if(n == null)
return false;
if(n.data == value) //matched at node
return true;
else //if no match search left subtree or right subtree
return search(n.left, value) || search(n.right, value);
}
public void sort()
{
sort(head);
}
//sort the left and right children of a tree first
private void sort(Node n)
{
if(n == null || n.left==null || n.right==null)
return;
//take the 3 numbers x , y and z. find the smallest of those and put in
//left and compare between the other 2 elements and put the bigger of the
//remainig 2 in the right and the left out element in the node
int x = n.left.data, y = n.right.data, z = n.data;
if(x < y ) // x is smaller than y
{
if(x < z) // x is also smaller than z , so x is the smallest
{
n.left.data = x;
if(y < z) //compare the other 2
{
n.data = y;
n.right.data = z;
}
else
{
n.data = z;
n.right.data = y;
}
}
else //z < x < y
{
n.left.data = z;
n.data = x;
n.right.data = y;
}
}
else // y is smaller than x
{
if(y < z) // y is smaller than z also, so y is the smallest
{
n.left.data = y;
if(x < z) //comparet hte other 2 elements
{
n.data = x;
n.right.data = z;
}
else
{
n.data = z;
n.right.data = x;
}
}
else //z < y < x
{
n.left.data = z;
n.data = y;
n.right.data = x;
}
}
sort(n.left); //sort the left subtree
sort(n.right); // sort the right subtree
}
}
class Node {
int data;
Node left;
Node right;
public Node(int data) {
this(data,null,null);
}
public Node(int data, Node left, Node right) {
this.data = data;
this.left = left;
this.right = right;
}
}
output
found 11
after sorting
root: 11 root.left: 5 root.right = 82
left: 5 left's left: 3 left.right = 8
right: 82 right's left: 35 right's right = 840
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.