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

Java code as well as the runtime and space requirements using Big O Given a bina

ID: 3873792 • Letter: J

Question

Java code as well as the runtime and space requirements using Big O

Given a binary tree node that looks like this: public class Node public int value; public Node left public Node right - This function should insert the provided value into the binary tree provided. static void insertValue(Node root, int value) / This function should return a sorted array containing the values present in the binary tree provided. This algorithm should run in linear time. " static int[l asSortedArray(Node root) ) +++Return the smallest value that exists in both arrays. Brute force. I static int minShared(lint0 array1, int] array2)) array1, int array2) ( Return the smallest value that exists in both arrays. Optimize for speed. Must be better than n*2 or m*2 where n and m are the lengths of the arrays. 'I static int minShared(intl array1, int array2) (

Explanation / Answer

public class BinaryTree {

public static void main(String[] args) {
    new BinaryTree().run();
}

static class Node {
    public int value;
public Node left;
    public Node right;

    public Node(int value) {
      this.value = value;
    }
}


public void insertValue(Node node, int value) {
    if (value < node.value) {
      if (node.left != null) {
        insertValue(node.left, value);
      } else {
        node.left = new Node(value);
      }
    } else if (value > node.value) {
      if (node.right != null) {
        insertValue(node.right, value);
      } else {
        node.right = new Node(value);
      }
    }
}

   public void print(Node node) {
    if (node != null) {
      print(node.left);
      System.out.println(" Traversed " + node.value);
      print(node.right);
    }
}

public void run() {
    // build the simple tree from chapter 11.
    Node root = new Node(5);
    insertValue(root, 1);
    insertValue(root, 8);
    insertValue(root, 6);
    insertValue(root, 3);
    insertValue(root, 9);
    System.out.println("Traversing tree in order");
    print(root);
  
}

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