Coding with java In this lab, you will implement three tree-traversal methods. U
ID: 3866394 • Letter: C
Question
Coding with java
In this lab, you will implement three tree-traversal methods. Using the skeleton code provided, you must fill in the indicated methods in order to traverse the tree in an “in-order,” “pre-order,” and “post-order” fashion. A description of each of these can be found at the following wikipedia page: https://en.wikipedia.org/wiki/Tree_traversal
Tree. java:
public class Tree{
Node root;
public static void main(String[] args){
Tree t = new Tree();
t.add(16);
t.add(10);
t.add(19);
t.add(22);
t.add(1);
t.add(14);
t.add(15);
t.add(25);
t.add(18);
t.preOrder();
t.postOrder();
t.inOrder();
}
public void add(int n){
if (root == null) root = new Node(n);
else add(n, root);
}
public void add(int n, Node current){
if(n < current.data){
if (current.left == null) current.left = new Node(n);
else add(n, current.left);
} else if(n > current.data){
if(current.right == null) current.right = new Node(n);
else add(n, current.right);
}
}
public void preOrder(){
preOrder(root);
System.out.println();
}
public void postOrder(){
postOrder(root);
System.out.println();
}
public void inOrder(){
inOrder(root);
System.out.println();
}
//Fill in these three methods!
public void preOrder(Node current){
}
public void postOrder(Node current){
}
public void inOrder(Node current){
}
//////////////////////////////
private class Node{
int data;
Node left;
Node right;
public Node(int n){
data = n;
}
}
}
Explanation / Answer
Here you go champ!!
/**
*
* @author Sam
*/
public class Tree {
Node root;
public static void main(String[] args) {
Tree t = new Tree();
t.add(16);
t.add(10);
t.add(19);
t.add(22);
t.add(1);
t.add(14);
t.add(15);
t.add(25);
t.add(18);
t.preOrder();
t.postOrder();
t.inOrder();
}
public void add(int n) {
if (root == null) {
root = new Node(n);
} else {
add(n, root);
}
}
public void add(int n, Node current) {
if (n < current.data) {
if (current.left == null) {
current.left = new Node(n);
} else {
add(n, current.left);
}
} else if (n > current.data) {
if (current.right == null) {
current.right = new Node(n);
} else {
add(n, current.right);
}
}
}
public void preOrder() {
preOrder(root);
System.out.println();
}
public void postOrder() {
postOrder(root);
System.out.println();
}
public void inOrder() {
inOrder(root);
System.out.println();
}
//Fill in these three methods!
public void preOrder(Node current) {
System.out.println(current.data);
preOrder(current.left);
preOrder(current.right);
}
public void postOrder(Node current) {
postOrder(current.right);
postOrder(current.left);
System.out.println(current.data);
}
public void inOrder(Node current) {
inOrder(current.left);
System.out.println(current.data);
inOrder(current.right);
}
//////////////////////////////
private class Node {
int data;
Node left;
Node right;
public Node(int n) {
data = n;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.