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

Code Provided below. 1.Write an implementation of a binary tree data structure i

ID: 3914485 • Letter: C

Question

Code Provided below.

1.Write an implementation of a binary tree data structure in Java. A node should have an integer key. The implementation should include methods for inserting a node and a tree walk method. A tree walk method is supposed to output a list of keys sorted in increasing order. Test your program in the main method of one of your classes using the following tests (tests assume the implementation contains classes BinTree with methods insertNode and treeWalk and a class Node):

// testcase 2

                    System.out.println();

                    System.out.println("testcase 1:");

                    BinTree binTree2 = new BinTree();

                    binTree2.insertNode(new Node(3));

                    binTree2.insertNode(new Node(201));

                    binTree2.insertNode(new Node(60));

                    binTree2.insertNode(new Node(30));

                    binTree2.insertNode(new Node(45));

                    binTree2.treeWalk();

                   

                    // testcase 3

                    System.out.println();

                    System.out.println("testcase 2_1:");

                    BinTree binTree3 = new BinTree();

                    binTree3.insertNode(new Node(-10));

                    binTree3.insertNode(new Node(-150));

                    binTree3.insertNode(new Node(4));

                    binTree3.insertNode(new Node(300));

                    binTree3.insertNode(new Node(45));

                    binTree3.treeWalk();

                    binTree3.insertNode(new Node(-50));

                    binTree3.insertNode(new Node(200));

                    System.out.println();

                    System.out.println("testcase 2_2:");

                    binTree3.treeWalk();

The files of the problem should be archived into one archive file named BinTree<your initials>.

Classes should be in package binTree.

The archive file should contain:

UML class diagram for the program generated by the SDE plug-in

Commented source code (comments per each class and method)

API docs produced by javadoc

2. Write a program in Java that will output all prime numbers up to a given upper bound (integer N which is a program input) in a computationally efficient manner using Euler’s sieve algorithm (section in

The program should take an input value N and a filename as a command line parameter and write the output to the file with that name (either creating a new file or overwriting the old one if it exists).

Comments should be provided for the implementation of the method that implements the sieve. Comments should be in javadoc format so that to produce a simple API documentation automatically.

Code Starts here (I have the other parts of the code finished I just need this one):

public class BinTree {

private Node root;

public void insertNode(Node node){

root = insert(root, node);

}

private Node insert(Node tree, Node node)

// Inserts item into tree.

// Post: item is in tree; search property is maintained.

{

  

return tree;

}

public void treeWalk() {

inOrder(root);

}

private void inOrder(Node tree)

// Post: node keys printed in order.

{

}

}

Explanation / Answer

You have not provide your Node class, so I created one. Let me know if you have any issues running the code, I'll help.
If the answer helped, please do rate it. Thank you.

Node.java
-----------
public class Node {
private int key;
private Node left;
private Node right;

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


public int getKey() {
return key;
}
public void setKey(int key) {
this.key = key;
}
public Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public void setRight(Node right) {
this.right = right;
}


}

BinTree.java
-------------
public class BinTree {

private Node root;

public void insertNode(Node node){

root = insert(root, node);

}

private Node insert(Node tree, Node node)

// Inserts item into tree.
// Post: item is in tree; search property is maintained.
{

if(tree == null)
return node;
else if(node.getKey() < tree.getKey())
tree.setLeft(insert(tree.getLeft(), node));
else if(node.getKey() > tree.getKey())
tree.setRight(insert(tree.getRight(), node));
return tree;
}

public void treeWalk() {

inOrder(root);
System.out.println();
}

private void inOrder(Node tree)
// Post: node keys printed in order.
{
if(tree == null)
return;

inOrder(tree.getLeft());
System.out.print(tree.getKey() + " ");
inOrder(tree.getRight());
}

}

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