Note: I do need this to follow my code I\'m giving below. Assisnment details : I
ID: 3761849 • Letter: N
Question
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
package node;
import java.util.Scanner;
public class Node {
private int data;
private Node leftChild;
private Node rightChild;
public Node(int data) {
this.data = data;
} // end public Node function
public int getData() {
return data;
} // end getData function
public Node getLeftChild() {
return leftChild;
} // end getLeftChild func
public void setLeftChild(Node leftChild) {
this.leftChild = leftChild;
} // end setLeftChild
public Node getRightChild() {
return rightChild;
} // end getRightChild
public void setRightChild(Node rightChild) {
this.rightChild = rightChild;
}
@Override
public String toString() {
String out = new String();
//out = .toString();
out = " ";
return out;
}
//Binary Search Tree Class:
class BinarySearchTree { // a java code can have any number of classes but only one public class
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) {
// the left sub tree is less than the root
// the right sub tree is greater than the root
} // end traverseAndAddNode
public void traverse(Scanner keyboard) {
if (root != null) {
System.out.println("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;
} // end switch
} /* end of if */ else {
System.out.println("This an empty tree!");
} // end else
}//end traverse func
private void inOrderTraversal(Node node) {
// implement inOrderTraversal here!
} // inorder Traversal
private void preOrderTraversal(Node node) {
// implement preOrderTraversal here!
} // end pre O
private void postOrderTraversal(Node node) {
// implement postOrderTraversal here!
} // end post order
} // end class BST
} // end public class Node - hence end of java code
//Tree Tester Class is currently nothing...just a main:
/* the following had been moved to another java file called TreeTester.java that is part of the same packeg Node
class TreeTester {
public static void main(String[] args) {
} // end main
} // end class TreeTester
*/
package node;
public class TreeTester {
public static void main(String[] args) {
System.out.println("From inside TreeTester.java");
} // end main
} // end public class TreeTester
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.