472,791 Members | 977 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,791 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 7305
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?

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.