Question

Java Program: You will be traversing through an integer tree to print the data. Given main(),...

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)

}

Homework Answers

Answer #1

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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
You will be traversing through an integer tree to print the data. Given main(), write the...
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 -...
Java Program: You will be inserting values into a generic tree, then printing the values inorder,...
Java Program: You will be inserting values into a generic tree, then printing the values inorder, as well as printing the minimum and maximum values in the tree. Given main(), write the methods in the 'BSTree' class specified by the // TODO: sections. There are 5 TODOs in all to complete. Ex: If the input is like ferment bought tasty can making apples super improving juice wine -1 the output should be: Enter the words on separate lines to insert...
This is my current code and im having trouble inserting the findMin, findMax, and Search functions...
This is my current code and im having trouble inserting the findMin, findMax, and Search functions in main to output them. #include<iostream> using namespace std; /*Define the class for BST*/ class BST_TREE {    /*Structure of the tree*/    struct tree    {        int value;        tree* left;        tree* right;    };    tree* root;    /*Method to clear the tree*/    tree* Empty(tree* t)    {        if (t == NULL)       ...
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class....
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class. Make every Node object have a false isBlack field, all new node is red by default. In the end of the insert method, set the root node of your red black tree to be black. Implement the rotate() and recolor() functions, and create tests for them in a separate class. import java.util.LinkedList; public class BinarySearchTree<T extends Comparable<T>> { protected static class Node<T> { public...
In this lab, you will write a program that creates a binary search tree based on...
In this lab, you will write a program that creates a binary search tree based on user input. Then, the user will indicate what order to print the values in. **Please write in C code** Start with the bst.h and bst.c base code provided to you. You will need to modify the source and header file to complete this lab. bst.h: #ifndef BST_H #define BST_H typedef struct BSTNode { int value; struct BSTNode* left; struct BSTNode* right; } BSTNode; BSTNode*...
You are given a reference to the root node of a binary search tree, that implements...
You are given a reference to the root node of a binary search tree, that implements a dictionary data structure. Please print all the elements in depths 500 through 510, all in sorted order. A node in a binary search tree is at depth x, if it takes x hops to get from the root. So the root is at depth 0, the children of the root are at depth 1, and so on. The class TreeNode defines a single...
Here is a modification of the BST program that includes a recursive find method: BinarySearchTree2C.java (posted...
Here is a modification of the BST program that includes a recursive find method: BinarySearchTree2C.java (posted below) Implement the following methods using recursion: int depth() // returns the length of the deepest path from root to any leaf int node_count() // returns the number of nodes in the tree void insert(int n) // inserts value n into the tree BinarySearchTree2C clone() // returns a clone (deep copy) of the tree Add code to the main method to test these methods....
Copy the file SimplerBST.java from the week 1 examples package and rename the file and class...
Copy the file SimplerBST.java from the week 1 examples package and rename the file and class A3BST. Add two public methods: one named countTwins that takes no parameters and that, when called, traverses every node of the tree to count the number of nodes having two children. one named lessThanValueCount that takes a paremeter of generic type V, and that, when called, traverses every node of the tree to count the number of nodes whose value (not its key) is...
The Binary Search Tree implementation for bst.zip. The code in the destructor of the BST class...
The Binary Search Tree implementation for bst.zip. The code in the destructor of the BST class is empty. Complete the destructor so the memory allocated for each node in the BST is freed. Make a couple of different trees in your main method or in a function to test the destructor (the program should not crash upon exiting). bst.zip (includes the following files below in c++): bst.h: #pragma once #include #include "node.cpp" using namespace std; template class BST { public:...
I have written working code for this problem however it is not producing an output and...
I have written working code for this problem however it is not producing an output and it states is a wrong answer. Please just fix my code below and make it work for the sample test cases and all test cases. You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT