Thank you in advance! I\'m currently stuck making my binary search tree in java.
ID: 3761884 • Letter: T
Question
Thank you in advance! I'm currently stuck making my binary search tree in java. (made in eclipse)
****Note: I do need this to follow my code I'm giving below.****
Assisnment details : In the BinarySearchTree class implement 4 methods.
a) Implement the traverseAndAddNode method that will add a node to the Tree by comparing the data of each node. It is a recursive method, for more information please see the comment in the method.
b) Implement 3 different Traversal methods which will be used to traverse the tree.
i. For inOrderTraversal, print the data of each node using in-order traversal. You can use System.out.println() to print it.
ii. For preOrderTraversal, print the data of each node using pre-order traversal. You can use System.out.println() to print it.
iii. For postOrderTraversal, print the data of each node using post-order traversal. You can use System.out.println() to print it.
3. In the TreeTester class, do the following things.
a) Create a BinarySearchTree object.
b) Add 18, 15, 13, 16, 12, 20, 34, 56, 98, 20 to the tree.
c) Use the three different traversal methods to traverse the tree, if you are using In-order traversal you will be able to see the integers are sorted in order.
d) Write in the comment part in your code how each traverse can be useful.
This is what I currently have:
Node Class:
public class Node {
private int data;
private Node leftChild;
private Node rightChild;
public Node(int data) {
this.data = data;
}
public int getData() {
return data;
}
public Node getLeftChild() {
return leftChild;
}
public void setLeftChild(Node leftChild) {
this.leftChild = leftChild;
}
public Node getRightChild() {
return rightChild;
}
public void setRightChild(Node rightChild) {
this.rightChild = rightChild;
}
@Override
public String toString() {
String out = new String();
out = .toString();
return out;
}
}
Binary Search Tree Class:
import java.util.Scanner;
public class BinarySearchTree {
private Node root;
public void add(int data) {
Node nodeToAdd = new Node(data);
if (root == null) {
//always add the root first
root = nodeToAdd;
}
else {
traverseAndAddNode(root, nodeToAdd);
}
}
private void traverseAndAddNode(Node node, Node nodeToAdd) {
//this like almost all methods having to do with trees is recursive
// if the data of nodeToAdd is less than the data of node, traverse left child of node,
//else traverse right child
// we do this until the path we want to take is equal to null then we place the node there.
}
public void traverse(Scanner keyboard) {
if (root != null) {
System.out
.print("Please chose a way to traverse: 1 for preOrder 2 for inOrder 3 for postOrder:");
int choice = keyboard.nextInt();
Node nodeToTraverse = root;
switch (choice) {
case 1:
preOrderTraversal(nodeToTraverse);
break;
case 2:
inOrderTraversal(nodeToTraverse);
break;
case 3:
postOrderTraversal(nodeToTraverse);
break;
default:
System.out.println("Wrong Choice!");
break;
}
} else {
System.out.println("This an empty tree!");
}
}
private void inOrderTraversal(Node node) {
// implement inOrderTraversal here!
}
private void preOrderTraversal(Node node) {
// implement preOrderTraversal here!
}
private void postOrderTraversal(Node node) {
// implement postOrderTraversal here!
}
}
Tree Tester Class is currently nothing...just a main:
public class TreeTester {
public static void main(String[] args) {
}
}
Explanation / Answer
Please find the below implementation
1) public Node traverseAndAddNode(Node rootNode, int data)
{
if (rootNode == null)
{
rootNode = new Node(data);//putting data to the new node
}
if(rootNode!=null)
{
if (data <= rootNode.getData())
rootNode.leftChild = traverseAndAddNode(rootNode.leftChild, data);
else
rootNode.rightChild = traverseAndAddNode(rootNode.rightChild, data);
}
return rootNode;
}
2)
private void inOrderTraversal(Node rootnode)
{
// implement inOrderTraversal here!
if (rootnode != null)
{
inOrderTraversal(rootnode.getLeftChild());
System.out.print(rootnode.getData() +" ");
inOrderTraversal(rootnode.getRightChild());
}
}
3)
private void preOrderTraversal(Node rootnode)
{
// implement preOrderTraversal here!
if (rootnode != null)
{
System.out.print(rootnode.getData() +" ");
preOrderTraversal(rootnode.getLeftChild());
preOrderTraversal(rootnode.getRightChild());
}
}
4)
private void postOrderTraversal(Node rootnode)
{
// implement postOrderTraversal here!
if (rootnode != null)
{
postOrderTraversal(rootnode.getLeftChild());
postOrderTraversal(rootnode.getRightChild());
System.out.print(rootnode.getData() +" ");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.