Question

This is in Java Create a client class that does the following: Manually creates an instance...

This is in Java

Create a client class that does the following:

  • Manually creates an instance of a binary expression tree that represents the following expression:
  • (((5+2)*(2-1)/((2+9))+((7-2)-1))*8)
  • Do this by using methods such as addRoot, addLeft, addRight, set, and attach.
  • The element should be of type String
  • Note that you are manually building this specific expression tree, the pseudo code might look something like:
    • Create a LinkedBinaryTree
    • Add “*” as the root to the tree
    • Add “/” as the left child to the tree
    • Add “8” the right child to the tree
    • Add a left child to the root’s left child
    • Add a right child to the root’s left child
    • etc.

You may want to consider if it would be easier to build the tree in a top down fashion or in a bottom up fashion.

Once you have created the expression tree have your client print out the following:

  • The string that represents the expression:

      ( ( ( 5 + 2 ) * ( 2 – 1 ) / ( 2 + 9 ) + ( ( 7 – 2 ) – 1 ) ) * 8 )

  • The preOrder traversal of the tree
  • The inOrder traversal of the tree
  • The postOrder traversal of the tree
  • The breathFirst traversal of the tree

The parenthesized representation of the tree using Euler’s Tour

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the program. Please comment below for any queries i will help

public class PrintRoot extends EulerTour<Character, Character> {

    @Override

    public Character execute(BinaryTree<Character> T) {

        init(T);

        return eulerTour(T.root());

    }

    @Override

    protected void Left_Visited(Position<Character> vt, TourResult<Character> rt) {

        Character t_1 = new Character('T');

        Character t_2 = new Character('F');

            System.out.print("(");

    }

  protected void Below_visited(Position<Character> vt, TourResult<Character> rt)

  {

    Character t_3 = new Character('+');

      Character t_4 = new Character('vt');

      if(!vt.element().equals(t_4)&&!vt.element().equals(t_3))

          rt.out=vt.element();

            System.out.print(vt.element());

  }

  protected void rt_visited(Position<Character> vt, TourResult<Character> rt)

  {

      Character t_1 = new Character('T');

      Character t_2 = new Character('F');

      Character t_3 = new Character('+');

      Character t_4 = new Character('vt');

      if(vt.element().equals(t_4))

      {

       if(rt.out==null&&rt.left!=null&&rt.rt!=null){

        if(rt.left.equals(t_1) && rt.rt.equals(t_1))

            rt.out = t_1;

        else if(rt.left.equals(t_1) && rt.rt.equals(t_2))

            rt.out = t_1;

        else if(rt.left.equals(t_2) && rt.rt.equals(t_2))

             rt.out = t_2;

      }

      }

      if(vt.element().equals(t_3))

      {

          if(rt.out==null&&rt.left!=null&&rt.rt!=null){

        if(rt.left.equals(t_1) && rt.rt.equals(t_1))

            rt.out = t_1;

        else if(rt.left.equals(t_1) && rt.rt.equals(t_2))

           rt.out = t_2;

        else if(rt.left.equals(t_2) && rt.rt.equals(t_2))

             rt.out = t_2;

        }

      }

    else if(rt.rt==null || rt.left == null)

     rt.out = vt.element();

     System.out.print(")");

  }

}

Add a comment
Know the answer?
Add Answer to:
This is in Java Create a client class that does the following: Manually creates an instance...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Can someone help me in java Net Beans IDE witht the code andjava comments. Thanks...

    Can someone help me in java Net Beans IDE witht the code and java comments. ThanksMany of these classes will just need to be copied from previous assignments. When you implement the Tree interface you may import the java.util.Iterator class. When you implement the AbstractBinaryTree class you may import the java.util.ArrayList class and the java.util.List class.In this project you will be doing the following:Create a NetBeans project named lab07 and ensure it is imported into your SVN.In this lab assignment...

  • Draw the Binary Tree that is constructed by calling the following methods. BinaryTree T1; T1.addRoot(5); addLeft(T1.root(),...

    Draw the Binary Tree that is constructed by calling the following methods. BinaryTree T1; T1.addRoot(5); addLeft(T1.root(), 10); addRight(T1.root(), 15); BinaryTree T2; T2.addRoot(20); addLeft(T2.root(), 18); addRight(T2.root(), 26); BinaryTree T; T.addRoot(8); attach(T.root, T1, T2); Upload an image showing the tree diagram or show it as following, make sure your result is clear enough to tell the left child or right child or parent node relation. Details regarding the used method can be found under content->others->binary tree code->linkedBinaryTree 1 2 3 5

  • In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT {...

    In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT {    private BinaryTreeNode root;    /**    * Creates an empty binary tree.    */    public LinkedBinaryTree() {        root = null;    }    /**    * Creates a binary tree from an existing root.    */    public LinkedBinaryTree(BinaryTreeNode root) {        this.root = root;    }    /**    * Creates a binary tree with the specified element...

  • QUESTION 8   In the _____ traversal, the root is processed first, before its subtrees. breadth first...

    QUESTION 8   In the _____ traversal, the root is processed first, before its subtrees. breadth first preorder postorder inorder 0.10000 points    QUESTION 9 What kind of traversal does the following algorithm (in pseudo-code) describe? Algorithm traversal (root) if (root is not null)      traversal (leftSubTree)      process (root)      traversal (rightSubTree) end if end traversal breadth first preorder inorder postorder 0.10000 points    QUESTION 10 What kind of traversal does the following algorithm (in pseudo-code)  describe? Algorithm traversal (root) if...

  • JAVA CODE Topic: BST Animation For each task, submit the source code with detail comments. Objective:...

    JAVA CODE Topic: BST Animation For each task, submit the source code with detail comments. Objective: javafx GUI program for BST Animation. BSTAnimation.java, AbstractTree.java and Tree.java. Modify the BSTAnimation.java  (1) Add Show Preoreder and Show Postorder button. Insert these two buttons next to the Show Inorder button to display tree in selected order. (2) Currently removing node method is to replace the removed node by the highest node on left-subtree. Modify the method by using lowest node on the right-subtree instead....

  • C++ EXERCISE (DATA STRUCTURES). I just need a code for some functions that are missing. Please...

    C++ EXERCISE (DATA STRUCTURES). I just need a code for some functions that are missing. Please help me figure out. Thanks. C++ BST implementation (using a struct) Enter the code below, and then compile and run the program. After the program runs successfully, add the following functions: postorder() This function is similar to the inorder() and preorder() functions, but demonstrates postorder tree traversal. displayParentsWithTwo() This function is similar to the displayParents WithOne() function, but displays nodes having only two children....

  • Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to com...

    Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to completely test every method in the BST class to ensure the class meets all its requirements. You should read the Listing 25.5: TestBST.java for an idea of what your program should look like. Listing 25.4 BST.java public class BST> extends AbstractTree { protected TreeNode...

  • show all steps Review the following BST and create the inorder, preorder and postorder list. Try...

    show all steps Review the following BST and create the inorder, preorder and postorder list. Try to delete the value 17 and after deletion create the inorder, preorder and postorder list. Follow both algorithms.  If the node has a left child and a right child, 1) replace the node’s value with the largest value in the left subtree and delete that value’s node from the left subtree. Or 2) replace the node’s value with the smallest value in the right subtree...

  • show all steps-python Review the following BST and create the inorder, preorder and postorder list. Follow...

    show all steps-python Review the following BST and create the inorder, preorder and postorder list. Follow both algorithms.  If the node has a left child and a right child, 1) replace the node’s value with the largest value in the left subtree and delete that value’s node from the left subtree. Or 2) replace the node’s value with the smallest value in the right subtree and delete that value’s node from the right subtree. -What are the two possible values for...

  • show all steps-python Review the following BST and create the inorder, preorder and postorder list. Try...

    show all steps-python Review the following BST and create the inorder, preorder and postorder list. Try to delete the value 17 and after deletion create the inorder, preorder and postorder list. Follow both algorithms.  If the node has a left child and a right child, 1) replace the node’s value with the largest value in the left subtree and delete that value’s node from the left subtree. Or 2) replace the node’s value with the smallest value in the right subtree...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT