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

Netbeans/ JAVA write this code: document every line. Create a class for a binary

ID: 3678191 • Letter: N

Question

Netbeans/ JAVA write this code: document every line.

Create a class for a binary tree named BT. Each node in the tree should have data, a reference to the node's left sub-tree, and a reference to the node's right sub-tree. For the purposes of this project, the data in the nodes can be integers. The binary tree should have a height property, a size property, which is the number of nodes in the tree, and the following methods: a null constructor a constructor that builds a tree from a specified array of integers. The tree does not need to be a balanced tree, preorder - iterate (list all nodes in order according to the preorder traversal of the tree) inorder - iterate (list all nodes in order according to the inorder traversal of the tree) postorder - iterate (list all nodes in order according to the postorder traversal of the tree) search - given a key value, tell us whether or not the integer is in the tree. max - return the maximum value in the tree min - return the minimum value in the tree Test each method to ensure that it works properly.

Explanation / Answer

public class Node<T>
{
public int value;
public Node left;
public Node right;

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

}

public class BinaryTree
{
public Node root;

public void insert(int value)
{
    Node node = new Node<>(value);

    if ( root == null )
    {
      root = node;
      return;
    }

    insert(root, node);

}

private void insert(Node latestRoot, Node node){

    if ( latestRoot.value > node.value){

      if ( latestRoot.left == null ){
        latestRoot.left = node;
        return;
      }
      else{
        insert(latestRoot.left, node);
      }
    }
    else{
      if (latestRoot.right == null){
        latestRoot.right = node;
        return;
      }
      else{
        insert(latestRoot.right, node);
      }
    }
}
}


public int Min(){
if ( root == null ){
    return 0;
}
Node currNode = root;
while(currNode.left != null){
    currNode = currNode.left;
}
return currNode.value;
}


public int Max(){
if ( root == null){
    return 0;
}

Node currNode = root;
while(currNode.right != null){
    currNode = currNode.right;
}
return currNode.value;
}


public void Inorder(){
InorderRec(root);
System.out.println("");
}


private void InorderRec(Node currRoot){
if ( currRoot == null ){
    return;
}
InorderRec(currRoot.left);
System.out.print(currRoot.value+", ");
InorderRec(currRoot.right);
}


public void Preorder() {
PreorderRec(root);
System.out.println("");
}


private void PreorderRec(Node currRoot) {
if (currRoot == null) {
    return;
}
System.out.print(currRoot.value + ", ");
PreorderRec(currRoot.left);
PreorderRec(currRoot.right);
}


public void Postorder() {
PostorderRec(root);
System.out.println("");
}


private void PostorderRec(Node currRoot) {
if (currRoot == null) {
    return;
}
PostorderRec(currRoot.left);
PostorderRec(currRoot.right);
System.out.print(currRoot.value + ", ");

}


public class Node<T> {
public int value;
public Node left;
public Node right;

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

}


public class BinaryTree {

public Node root;

public BinaryTree insert(int value) {
    Node node = new Node<>(value);

    if (root == null) {
      root = node;
      return this;
    }

    insert(root, node);
    return this;
}

private void insert(Node latestRoot, Node node) {

    if (latestRoot.value > node.value) {

      if (latestRoot.left == null) {
        latestRoot.left = node;
        return;
      } else {
        insert(latestRoot.left, node);
      }
    } else {
      if (latestRoot.right == null) {
        latestRoot.right = node;
        return;
      } else {
        insert(latestRoot.right, node);
      }
    }
}


public int Min() {
    if (root == null) {
      return 0;
    }
    Node currNode = root;
    while (currNode.left != null) {
      currNode = currNode.left;
    }
    return currNode.value;
}


public int Max() {
    if (root == null) {
      return 0;
    }

    Node currNode = root;
    while (currNode.right != null) {
      currNode = currNode.right;
    }
    return currNode.value;
}


public void Inorder() {
    InorderRec(root);
    System.out.println("");
}

private void InorderRec(Node currRoot) {
    if (currRoot == null) {
      return;
    }
    InorderRec(currRoot.left);
    System.out.print(currRoot.value + ", ");
    InorderRec(currRoot.right);
}


public void Preorder() {
    PreorderRec(root);
    System.out.println("");
}


private void PreorderRec(Node currRoot) {
    if (currRoot == null) {
      return;
    }
    System.out.print(currRoot.value + ", ");
    PreorderRec(currRoot.left);
    PreorderRec(currRoot.right);
}


public void Postorder() {
    PostorderRec(root);
    System.out.println("");
}


private void PostorderRec(Node currRoot) {
    if (currRoot == null) {
      return;
    }
    PostorderRec(currRoot.left);
    PostorderRec(currRoot.right);
    System.out.print(currRoot.value + ", ");

}
}

public class BinaryTree
{

public static void main(String[] args)
{
    BinaryTree bst = new BinaryTree();

    System.out.println("Inorder traversal");
    bst.Inorder();

    System.out.println("Preorder Traversal");
    bst.Preorder();

    System.out.println("Postorder Traversal");
    bst.Postorder();

    System.out.println("The minimum value in the BST: " + bst.Min());
    System.out.println("The maximum value in the BST: " + bst.Max());

}
}