473,287 Members | 2,682 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

Help with a java program (binary tree)

Hey guys, is there anyone who could help me..?

I have file ExpressionBinaryTree.java :


Expand|Select|Wrap|Line Numbers
  1.  
  2. /** class ExpressionBinaryTree
  3. *   uses a binary tree to represent binary expressions
  4. *   does not implement BinaryTree - all iterators return String
  5. */
  6. package foundations;
  7. import java.util.*;
  8. public class ExpressionBinaryTreeE implements Container {
  9.     // Fields
  10.     private SearchTreeNode root = null;
  11.     private int size = 0;
  12.     private String postfixString;
  13.     // Constructors
  14.     /** Create an empty expression tree
  15.     */
  16.     public ExpressionBinaryTreeE () {
  17.     }
  18.     /** Create and initialize an expression tree on infix
  19.     */
  20.     public ExpressionBinaryTreeE (String infix) {
  21.         postfixString = (new FunctionEvaluation(infix)).postfix();
  22.         buildExpressionTree();
  23.     }
  24.     // Commands
  25.     /** Set a new value for infix
  26.     *   updates postfixString and rebuilds expression tree
  27.     */
  28.     public void setInfixString (String infix) {
  29.         postfixString = (new FunctionEvaluation(infix)).postfix();
  30.         buildExpressionTree();
  31.     }
  32.     /** Set a new value for postfixString
  33.     */
  34.     public void setPostfixString (String postfix) {
  35.         //  remove blanks then build expression tree
  36.         //   left as an exercise
  37.     }
  38.     /** Remove all objects from the container if found
  39.     */
  40.     public void makeEmpty () {
  41.         root = null;
  42.     }
  43.     // Queries
  44.     /** Return a reference to the root
  45.     */
  46.     public SearchTreeNode root () {
  47.         return root;
  48.     }
  49.     /** Return true if the container is empty
  50.     */
  51.     public boolean isEmpty () {
  52.         return root == null;
  53.     }
  54.     /** Return the number of objects in the container
  55.     *   postfixString has been trimmed
  56.     */
  57.     public int size () {
  58.         return size;
  59.     }
  60.     /** Return the infix string on elements in the tree
  61.     */
  62.     public String traverseInorder () {
  63.         // left as an exercise
  64.         return "";
  65.     }
  66.     /** return the prefix string on elements in the tree
  67.     */
  68.     public String traversePreorder () {
  69.         // left as an exercise
  70.         return "";
  71.     }
  72.     /** Return the postfix on elements in the tree
  73.     */
  74.     public String traversePostorder () {
  75.         //left as an exercise
  76.         return "";
  77.     }
  78.     // Internal methods
  79.     /** Build an expression tree from postfixString
  80.     *   - use a Stack of SearchTreeNode
  81.     *   throw NoSuchElementException for caught Stack error
  82.     */
  83.     private void buildExpressionTree () {
  84.         // left as an exercise
  85.     }
  86.     private boolean isOperand (char ch) {
  87.         // left as an exercise
  88.         return false;
  89.     }
  90.     private boolean isOperator (char ch) {
  91.         //left as an exercise
  92.         return false;
  93.     }
  94. }
  95.  
Also I have a supporting class (SearchTreeNode.java) for ExpressionBinaryTree and a test class ExpressionTest.java

Expand|Select|Wrap|Line Numbers
  1. ** A test class for ExpressionBinaryTree (prefix input)
  2. */
  3. import foundations.*;
  4. import java.io.*;
  5. public class ExpressionTest{
  6.     public static void main(String[] args) throws IOException {
  7.         ExpressionBinaryTreeE expr = new ExpressionBinaryTreeE("a+b/(c-d)-e");
  8.         if (!expr.isEmpty()){
  9.             System.out.println("The number of terms in the tree is: " + expr.size());
  10.             System.out.println("Equiv infix string is:   " + expr.traverseInorder());
  11.             System.out.println("Equiv prefix string is:  " + expr.traversePreorder());
  12.             System.out.println("Equiv postfix string is: " + expr.traversePostorder());
  13.         }
  14.         System.out.println("Test makeEmpty()");
  15.         expr.makeEmpty();
  16.         System.out.println("The number of terms in the tree is: " + expr.size() + "\n");
  17.         expr.setInfixString("a*b/(c-d)*e");//"a*b-(c-d*e)+f/(g-h)" or "a+b-(c-d*e)"
  18.         if (!expr.isEmpty()){
  19.             System.out.println("The number of terms in the tree is: " + expr.size());
  20.             System.out.println("Equiv infix string is:   " + expr.traverseInorder());
  21.             System.out.println("Equiv prefix string is:  " + expr.traversePreorder());
  22.             System.out.println("Equiv postfix string is: " + expr.traversePostorder());
  23.         }
  24.         System.out.println("Test makeEmpty()");
  25.         expr.makeEmpty();
  26.         System.out.println("The number of terms in the tree is: " + expr.size() + "\n");
  27.         expr.setInfixString("a+(b-c)-d/e");//"a*b-(c-d*e)+f/(g-h)" or "a+b-(c-d*e)"
  28.         if (!expr.isEmpty()){
  29.             System.out.println("The number of terms in the tree is: " + expr.size());
  30.             System.out.println("Equiv infix string is:   " + expr.traverseInorder());
  31.             System.out.println("Equiv prefix string is:  " + expr.traversePreorder());
  32.             System.out.println("Equiv postfix string is: " + expr.traversePostorder());
  33.         }
  34.         System.out.println("Test makeEmpty()");
  35.         expr.makeEmpty();
  36.         System.out.println("The number of terms in the tree is: " + expr.size() + "\n");
  37.         expr.setInfixString("a*b-(c-d*e)+f/(g-h)");//"a*b-(c-d*e)+f/(g-h)" or "a+b-(c-d*e)"
  38.         if (!expr.isEmpty()){
  39.             System.out.println("The number of terms in the tree is: " + expr.size());
  40.             System.out.println("Equiv infix string is:   " + expr.traverseInorder());
  41.             System.out.println("Equiv prefix string is:  " + expr.traversePreorder());
  42.             System.out.println("Equiv postfix string is: " + expr.traversePostorder());
  43.         }
  44.         System.out.println("Test makeEmpty()");
  45.         expr.makeEmpty();
  46.         System.out.println("The number of terms in the tree is: " + expr.size() + "\n");
  47.         BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
  48.         String postfix = "";
  49.         do {
  50.             System.out.print("Enter a postfix string: ");
  51.             postfix = keyboard.readLine();
  52.             expr.setPostfixString(postfix);
  53.             if (!expr.isEmpty()){
  54.                 System.out.println("The number of terms in the tree is: " + expr.size());
  55.                 System.out.println("Equiv infix string is:   " + expr.traverseInorder());
  56.                 System.out.println("Equiv prefix string is:  " + expr.traversePreorder());
  57.                 System.out.println("Equiv postfix string is: " + expr.traversePostorder());
  58.             }
  59.             System.out.println("Test makeEmpty()");
  60.             expr.makeEmpty();
  61.             System.out.println("The number of terms in the tree is: " + expr.size() + "\n");
  62.         } while (postfix.length() >= 3);
  63.     }
  64. }
  65.  
  66.  
Batch file Go. bat compiles all source files and runs the test class:

Expand|Select|Wrap|Line Numbers
  1. javac ExpressionTest.java
  2. java ExpressionTest
  3. del *.class
  4. del foundations\*.class
  5.  
I am required to complete the details for class ExpressionBinaryTree. Additional statements may be added to class ExpressionTest to expand on the testing performed.

Any ideas....?
Nov 16 '08 #1
2 7342
JosAH
11,448 Expert 8TB
I am required to complete the details for class ExpressionBinaryTree. Additional statements may be added to class ExpressionTest to expand on the testing performed.

Any ideas....?
Yup, I'd say that additional statements have to be added to that class because
as it is now it is an empty class, i.e. there are no useful things in it at all.

You have to write an entire parser; an AST (Abstract Syntax Tree) and a few
methods that transfer that AST to postfix, prefix and infix form. We won't do
it for you but if you're stuck we're very willing to help so get going.

kind regards,

Jos
Nov 16 '08 #2
Thank You Jos!! :) Helped me a lot.
Nov 17 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

54
by: Spammay Blockay | last post by:
I've been tasked with doing technical interviews at my company, and I have generally ask a range of OO, Java, and "good programming technique" concepts. However, one of my favorite exercises I...
1
by: Jerry Khoo | last post by:
hello, everybody, i am kinda new here, nice to meet u all. Now, i am > cs students and are now facing difficult problems in understanding > what a binary tree is, how it works, and the algorithm...
4
by: Tarique Jawed | last post by:
Alright I needed some help regarding a removal of a binary search tree. Yes its for a class, and yes I have tried working on it on my own, so no patronizing please. I have most of the code working,...
66
by: genestarwing | last post by:
QUESTION: Write a program that opens and read a text file and records how many times each word occurs in the file. Use a binary search tree modified to store both a word and the number of times it...
1
by: satan | last post by:
I need to write the definition of the method leavesCount that takes as a parameter a reference of the root node of a binary tree and returns the numbers of leaves in the binary tree. This what i get...
7
by: LoneHunter01 | last post by:
When I try to compile the below files, the complier says: the WordNode h in BST.cpp: "WordNode" does not name a type also in BST.cpp: "WordNode" undeclared, first use of this function So, any...
4
memoman
by: memoman | last post by:
Can any body help me in that program ??? mail me if anybody could reach any -helpfull- thing Write a C++ program that namely insert, delete, and search in a fixed record length file (containing...
5
gekko3558
by: gekko3558 | last post by:
I am writing a simple binary search tree (nodes are int nodes) with a BSTNode class and a BST class. I have followed the instructions from my C++ book, and now I am trying to get a remove method...
3
by: KLonergan | last post by:
Hello everyone, I'm trying to recreate the C# dictionary class using binary trees for chaining. I have my binary tree class complete, and all I really need now is to connect my array with numerous...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.