473,503 Members | 8,959 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with a java program (binary tree)

2 New Member
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 7358
JosAH
11,448 Recognized Expert MVP
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
cioccolatina
2 New Member
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
17320
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
3382
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
9002
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
5279
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
4654
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
2693
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
1896
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
5261
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
1288
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
7095
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7294
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7361
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7015
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7470
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4693
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1523
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
749
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
403
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.