Question

prefixRecursive public java.lang.String prefixRecursive​(ATree.Node current) Concatenates the tokens in the expression tree returned from the build(List)...


prefixRecursive
public java.lang.String prefixRecursive​(ATree.Node current)
Concatenates the tokens in the expression tree returned from the build(List) method in prefix order.
Accumulate the operator first, then the string from the left and right subtrees. Add an extra space after each token.

Our implementation of this method is ~2-6 lines of code.

Parameters:
current - the root node
Returns:
the tokens in prefix order



this question is about changing from an expression tree to a prefix order or preorder string.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi,

Hope I understood your requirement correctly. Please find below my answer.

Steps on PreOrder traversal algorithm

  1. visit the node
  2. visit the left subtree
  3. visit the right subtree

You can easily implement this pre-order traversal algorithm using recursion by printing the value of the node and then recursive calling the same preOrder() method with left subtree and right subtree, as shown in the following program:

Code:-

private void preOrder(TreeNode node)

{

if(node == null)

{

return;

}

System.out.println("%s ", node.data);

preOrder(node.left);

preOrder(node.right);

}

Add a comment
Know the answer?
Add Answer to:
prefixRecursive public java.lang.String prefixRecursive​(ATree.Node current) Concatenates the tokens in the expression tree returned from the build(List)...
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
  • Note: Once completed, run BXTDriver.class, then record the output to result.txt under your assignment folder. Without...

    Note: Once completed, run BXTDriver.class, then record the output to result.txt under your assignment folder. Without result.txt, you will receive zero(0) for this assignment. For this assignment, we will build a binary expression tree, display it, and evaluate it. This time, we will encapsulate the behavior in a BXT class. The driver class, tree node, as well as the BXT class are provided. Please implement appropriate methods in BXT class to build, display and evaluate a tree. Let's also allow...

  • Test Data would be as follows: Using java language. Build an expression tree. • When you...

    Test Data would be as follows: Using java language. Build an expression tree. • When you see a number: o you create a new node with the number as the data. o Then you push the new node to the stack. . When you see a binary operator: 0 You create a new Node for the operator. Then you pop and set the right child. Then you pop and set the left child o Then you push the new node...

  • Summary You will write an application to build a tree structure called Trie for a dictionary...

    Summary You will write an application to build a tree structure called Trie for a dictionary of English words, and use the Trie to generate completion lists for string searches. Trie Structure A Trie is a general tree, in that each node can have any number of children. It is used to store a dictionary (list) of words that can be searched on, in a manner that allows for efficient generation of completion lists. The word list is originally stored...

  • 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...

  • 1) Extend the Binary Search Tree ADT to include a public method leafCount that returns the...

    1) Extend the Binary Search Tree ADT to include a public method leafCount that returns the number of leaf nodes in the tree. 2) Extend the Binary Search Tree ADT to include a public method singleParent-Count that returns the number of nodes in the tree that have only one child. 3) The Binary search tree ADT is extended to include a boolean method similarTrees that receives references to two binary trees and determines whether the shapes of the trees are...

  • 1. Write a recursive function that returns the sum of all even integers in a LinkedBinaryTree. Yo...

    please explain each line of code! ( in python ) 1. Write a recursive function that returns the sum of all even integers in a LinkedBinaryTree. Your function should take one parameter, root node. You may assume that the tree only contains integers. You may not call any methods from the LinkedBinaryTree class. Specifically, you should traverse the tree in your function def binary tree even sum (root): Returns the sum of al1 even integers in the binary tree 2....

  • ECLIPSE Extend the Arithrmetic example from the course notes to include classes Minus, Mult and Divide....

    ECLIPSE Extend the Arithrmetic example from the course notes to include classes Minus, Mult and Divide. Each of these should override the sxall) method to perform the correct operations. For each dass, induding the ones I've already provided, write a toString() method that returns a String containing a text version of the part of the arithmetic problem in the subtree rooted at the current node. For example, If the expression tree is calling toString() for the "node should produce the...

  • JAVA, please You must write a robust program meaning that your program should not crash with...

    JAVA, please You must write a robust program meaning that your program should not crash with any given data. Data validation must be done any time that user enters an input. Write a program that 1. Gets an infix expression form the user and evaluate the expression using stack ADT a. Finds the postfix equivalent of the given infix expression b. Evaluate the created postfix expression. c. Note: your program should not crash when you enter an invalid expression such...

  • CAN SOMEONE PLEASE SOLVE THIS? THANK YOU. FINALTREE.JAVA: import java.util.LinkedList; public class FinalTree<E extends Comparable<? super...

    CAN SOMEONE PLEASE SOLVE THIS? THANK YOU. FINALTREE.JAVA: import java.util.LinkedList; public class FinalTree<E extends Comparable<? super E>> { private Node<E> root; private int size; public FinalTree() { root = null; size = 0; } public boolean isEmpty() { return size == 0; } public void add(E newItem) { if (isEmpty()) { root = new Node<E>(newItem); } else { Node<E> parent = null; Node<E> temp = root; int result = 0; while (temp != null) { parent = temp; result =...

  • Write a parser program for cSub using the method of recursive descent. The main program here...

    Write a parser program for cSub using the method of recursive descent. The main program here will effectively be the main program of the compiler as a whole. The input to the program will be a Csub source file, specified on the command line. The program will construct a parse tree for the program, with one interior node for every nonterminal in the derivation of the program, and one leaf node for each of the id, num, and real tokens...

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