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

im having a trouble in java coding. Coding a binary tree with nodes, searching f

ID: 3697300 • Letter: I

Question

im having a trouble in java coding. Coding a binary tree with nodes, searching for given object in tree and returning the node. i was trying to search in a preorder way however confused in how to stop the search and return "the node".

my main class is

Tree tree=new Tree();
tree.getRoot().addLeftChild("a");
tree.getRoot().addRightChild("b");
tree.findNode("a").addLeftChild("a-1");
tree.findNode("a").addRightChild("a-2");
tree.findNode("b").addRightChild("b-1");
tree.findNode("b").addLeftChild("b-2");
e.preorder(tree.getRoot());

and Node class is

private void findRecursive(Node t,String str){//searching str starting from t
  
   if(t.getData()==str)//want to save node to findNode
    findNode=t;
   if(t.isLeft())//true iff left is not empty//preorder way search
    this.findRecursive(t.getLeftChild(), str);
   if(t.isRight())//true if right is not empty
    this.findRecursive(t.getRightChild(), str);
}
  
  
public Node find(String data){//returning node matching the data
  findRecursive(root, data);
  return findNode//save node to findNode in return;
}

im noob in coding so please explain the method and show me the example code plz.

Explanation / Answer

// Java program to demonstrate insert operation in binary search tree
class BinarySearchTree {

   /* Class containing left and right child of current node and key value*/
   class Node {
       int key;
       Node left, right;

       public Node(int item) {
           key = item;
           left = right = null;
       }
   }

   // Root of BST
   Node root;

   // Constructor
   BinarySearchTree() {
       root = null;
   }

   // This method mainly calls insertRec()
   void insert(int key) {
   root = insertRec(root, key);
   }

   /* A recursive function to insert a new key in BST */
   Node insertRec(Node root, int key) {

       /* If the tree is empty, return a new node */
       if (root == null) {
           root = new Node(key);
           return root;
       }

       /* Otherwise, recur down the tree */
       if (key < root.key)
           root.left = insertRec(root.left, key);
       else if (key > root.key)
           root.right = insertRec(root.right, key);

       /* return the (unchanged) node pointer */
       return root;
   }

   // This method mainly calls PreorederRec()
   void preorder() {
preorderRec(root);
   }

   // A utility function to do inorder traversal of BST
   void preorderRec(Node root) {
       if (root != null) {
System.out.println(root.key);

preorderRec(root.left);
preorderRec(root.right);
       }
   }

// A utility function to search a given key in BST

public Node search(Node root, int key)

{

    // Base Cases: root is null or key is present at root

    if (root==null || root.key==key)

        return root;

    // val is greater than root's key

    if (root.key > key)

        return search(root.left, key);

    // val is less than root's key

    return search(root.right, key);

}

   // Driver Program to test above functions
   public static void main(String[] args) {
       BinarySearchTree tree = new BinarySearchTree();

       /* Let us create following BST
           50
       /  
       30   70
       / /
   20 40 60 80 */
       tree.insert(50);
       tree.insert(30);
       tree.insert(20);
       tree.insert(40);
       tree.insert(70);
       tree.insert(60);
       tree.insert(80);

       // print inorder traversal of the BST
tree.inorder();

tree.search(50,40);
   }
}

NOTE: Dear student your code is not complete. thatswhy I am providing it in my way.