Java Program: You will be traversing through an integer tree to print the data. Given main(), write the methods in the 'IntegerBinaryTree' class specified by the // TODO: sections. There are 6 methods in all to write.
Ex: If the input is:
70 86 60 90 49 62 81 85 38 -1
the output should be:
Enter whole numbers to insert into the tree, -1 to stop Inorder: 38 - 49 - 60 - 62 - 70 - 81 - 85 - 86 - 90 - Reverse Preorder: 70 - 86 - 90 - 81 - 85 - 60 - 62 - 49 - 38 - Reverse Postorder: 90 - 85 - 81 - 86 - 62 - 38 - 49 - 60 - 70 -
Main.java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
IntegerBinaryTree tree = new
IntegerBinaryTree();
Scanner scrn = new
Scanner(System.in);
System.out.println("Enter whole
numbers to insert into the tree, -1 to stop");
int num = scrn.nextInt();
while(num != -1) {
tree.insertNode(num);
num =
scrn.nextInt();
}
System.out.println();
tree.printInorder();
tree.printReversePreorder();
tree.printReversePostorder();
}
}
Node.java
public class Node {
private int element;
private Node left;
private Node right;
public Node(int ele) {
element = ele;
this.left = null;
this.right = null;
}
public int getElement() {
return element;
}
public Node getLeft() {
return left;
}
public Node getRight() {
return right;
}
public void setLeft(Node Left) {
left = Left;
}
public void setRight(Node Right) {
right = Right;
}
}
IntegerBinaryTree.java
public class IntegerBinaryTree {
private Node root = null;
public void insertNode(int value) {
if (root == null) { // empty, so
make root node
root = new
Node(value);
}
else {
Node pos =
root;
while(pos !=
null) {
if (value < pos.getElement()) {
if (pos.getLeft() == null)
{
pos.setLeft(new Node(value));
pos =
null;
}
else {
pos =
pos.getLeft();
}
}
else {
if (pos.getRight() == null)
{
pos.setRight(new Node(value));
pos =
null;
}
else {
pos =
pos.getRight();
}
}
}
}
}
// TODO: write the method printInorder
// It should check that the tree
isn't empty
// and prints "Tree is empty" if it
is
// otherwise prints "Inorder: ",
calls the Inorder method,
// and prints an empty
line
// TODO: write the method
printReversePreorder
// It should check that the tree
isn't empty
// and prints "Tree is empty" if it
is
// otherwise prints
"ReversePreorder: ", calls the reversePreorder method,
// and prints an empty line
// TODO: write the method
printReversePostorder
// It should check that the tree
isn't empty
// and prints "Tree is empty" if it
is
// otherwise prints
"ReversePreorder: ", calls the reversePostorder method,
// and prints an empty line
// TODO: write the inorder method
// that traverses the tree and prints the data
inorder
// TODO: write the method reversePreorder
// that traverses the tree and prints the data in
reverse Preorder (right before left)
// TODO: write the method
reversePostorder
// that traverses the tree and
prints the data in reverse Postorder (right before
left)
}
Java code for above problem
Node.java
public class Node {
private int element;
private Node left;
private Node right;
public Node(int ele) {
element = ele;
this.left = null;
this.right = null;
}
public int getElement() {
return element;
}
public Node getLeft() {
return left;
}
public Node getRight() {
return right;
}
public void setLeft(Node Left) {
left = Left;
}
public void setRight(Node Right) {
right = Right;
}
}
IntegerBinaryTree.java
public class IntegerBinaryTree {
private Node root = null;
public void insertNode(int value) {
if (root == null) { // empty,
so make root node
root =
new Node(value);
}
else {
Node
pos = root;
while(pos != null) {
if (value < pos.getElement()) {
if (pos.getLeft() == null) {
pos.setLeft(new Node(value));
pos = null;
}
else {
pos = pos.getLeft();
}
}
else {
if (pos.getRight() == null) {
pos.setRight(new Node(value));
pos = null;
}
else {
pos = pos.getRight();
}
}
}
}
}
public void printInorder() {
if(this.root==null) {
System.out.println("Tree is Empty");
return;
}
System.out.print("Inorder: ");
inorder(this.root);
System.out.println("\n");
}
public void inorder(Node node) {
if(node==null)
return;
inorder(node.getLeft());
System.out.print(node.getElement()+"
- ");
inorder(node.getRight());
}
public void printReversePreorder() {
if(this.root==null) {
System.out.println("Tree is Empty");
return;
}
System.out.print("Reverse Preorder:
");
reversePreorder(this.root);
System.out.println("\n");
}
public void reversePreorder(Node node) {
if(node==null)
return;
System.out.print(node.getElement()+"
- ");
reversePreorder(node.getRight());
reversePreorder(node.getLeft());
}
public void printReversePostorder() {
if(this.root==null) {
System.out.println("Tree is Empty");
return;
}
System.out.print("Reverse Postorder:
");
reversePostorder(this.root);
System.out.println("\n");
}
public void reversePostorder(Node node) {
if(node==null)
return;
reversePostorder(node.getRight());
reversePostorder(node.getLeft());
System.out.print(node.getElement()+"
- ");
}
}
Main.java
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
IntegerBinaryTree tree = new
IntegerBinaryTree();
Scanner scrn = new
Scanner(System.in);
System.out.println("Enter
whole numbers to insert into the tree, -1 to stop");
int num =
scrn.nextInt();
while(num != -1) {
tree.insertNode(num);
num =
scrn.nextInt();
}
System.out.println();
tree.printInorder();
tree.printReversePreorder();
tree.printReversePostorder();
}
}
Sample output
Mention in comments if any mistakes or errors are found. Thank you.
Get Answers For Free
Most questions answered within 1 hours.