-
BinaryTree Problem
Could you please tell me what is wrong with this code.The program is supposed to take an infix equation and create a tree that represents it and then convert into pre or post fix expression. My problem lies in creating the tree its self for now. The last 2 errors occur in my driver class, so I do not think that they are as relavant to the problem of creating the tree, but I will have to address that problem at a later time.
Any help or ideas would be greatly appreciated.
//code used to create the tree
public void createTree()
{
BinaryTree tree = new BinaryTree();
StringTokenizer st = new StringTokenizer(equation, "");
if(st.nextToken() != "+" || st.nextToken() != "/" || st.nextToken() != "-"
|| st.nextToken() != "*" || st.nextToken() != "(" || st.nextToken() != ")")
{
tree.attachLeft(st.nextToken());
}
if(st.nextToken() == "+" || st.nextToken() == "/" || st.nextToken() == "-" || st.nextToken() == "*")
{
tree.setRootItem(st.nextToken());
if(st.nextToken() != "+" || st.nextToken() != "/" || st.nextToken() != "-"
|| st.nextToken() != "*" || st.nextToken() != "(" || st.nextToken() != ")")
{
tree.attachRight(st.nextToken());
BinaryTree tree1 = new BinaryTree(root, left, right);
tree.attachLeftSubtree(tree1);
}
if(st.nextToken() == "+" || st.nextToken() == "/" || st.nextToken() == "-" || st.nextToken() == "*")
{
tree.setRootItem(st.nextToken());
if(st.nextToken() != "+" || st.nextToken() != "/" || st.nextToken() != "-"
|| st.nextToken() != "*" || st.nextToken() != "(" || st.nextToken() != ")")
{
tree.attachRight(st.nextToken());
BinaryTree tree2 = new BinaryTree(root, tree1, right);
tree.attachLeftSubtree(tree2);
}//end if
}//end if
}//end if
}//end createTree
//errors thrown when compilation is attempted
6 errors found:
1.File: C:\My Documents\Data Structures\HW6\PreInPostTree.java [line: 41]
Error: cannot find symbol
symbol : constructor BinaryTree(java.lang.String,java.lang.String,java.lang.String)
location: class BinaryTree
2.File: C:\My Documents\Data Structures\HW6\PreInPostTree.java [line: 41]
Error: internal error; cannot instantiate BinaryTree.<init> at BinaryTree to ()
3.File: C:\My Documents\Data Structures\HW6\PreInPostTree.java [line: 53]
Error: cannot find symbol
symbol : constructor BinaryTree(java.lang.String,BinaryTree,java.lang.String)
location: class BinaryTree
4.File: C:\My Documents\Data Structures\HW6\PreInPostTree.java [line: 53]
Error: internal error; cannot instantiate BinaryTree.<init> at BinaryTree to ()
5.File: C:\My Documents\Data Structures\HW6\PreInPostTreeDemo.java [line: 6]
Error: cannot find symbol
symbol : constructor PreInPostTree()
location: class PreInPostTree
6.File: C:\My Documents\Data Structures\HW6\PreInPostTreeDemo.java [line: 6]
Error: internal error; cannot instantiate PreInPostTree.<init> at PreInPostTree to ()
-
It looks to me that there is no relationship between the code you posted and the errors you posted. The errors relate to the constructor of the BinaryTree structure or the "PreInPostTree" structure. Can't offer any suggestion from what you've posted.
-
oh woops now thats a stupid thing to over look. The first code I posted is in the PreInPostTree class, the rest of it is just the method to get the equation from the user.
If anyone is still willing to give me a hand with this, I would be ever thankfull for the help.
//here is the BinaryTree class
public class BinaryTree extends BinaryTreeBasis
{
public BinaryTree()
{
}
public BinaryTree(Object rootItem)
{
super(rootItem);
}
public BinaryTree(Object rootItem, BinaryTree leftTree, BinaryTree rightTree)
{
root = new TreeNode(rootItem, null, null);
attachLeftSubtree(leftTree);
attachRightSubtree(rightTree);
}
public void setRootItem(Object newItem)
{
if(root != null)
{
root.setItem(newItem);
}
else
{
root = new TreeNode(newItem, null, null);
}
}
public void attachLeft(Object newItem)
{
if(!isEmpty() && root.getLeft() == null)
{
root.setLeft(new TreeNode(newItem, null, null));
}
}
public void attachRight(Object newItem)
{
if(!isEmpty() && root.getRight() == null)
{
root.setRight(new TreeNode(newItem, null, null));
}
}
public void attachLeftSubtree(BinaryTree leftTree) throws TreeException
{
if(isEmpty())
{
throw new TreeException("TreeException: Empty tree");
}
else if(root.getLeft() != null)
{
throw new TreeException("TreeException: " + " Can not overwrite left subtree");
}
else
{
root.setLeft(leftTree.root);
leftTree.makeEmpty();
}
}
public void attachRightSubtree(BinaryTree rightTree)
{
if(isEmpty())
{
throw new TreeException("TreeException: Empty tree");
}
else if(root.getRight() != null)
{
throw new TreeException("TreeException: " + " Can not overwrite left subtree");
}
else
{
root.setRight(rightTree.root);
rightTree.makeEmpty();
}
}
protected BinaryTree(TreeNode rootNode)
{
root = rootNode;
}
public BinaryTree detachLeftSubtree() throws TreeException
{
if(isEmpty())
{
throw new TreeException("TreeException: Empty tree");
}
else
{
BinaryTree leftTree;
leftTree = new BinaryTree(root.getLeft());
root.setLeft(null);
return leftTree;
}
}
public BinaryTree detachRightSubtree() throws TreeException
{
if(isEmpty())
{
throw new TreeException("TreeException: Empty tree");
}
else
{
BinaryTree rightTree;
rightTree = new BinaryTree(root.getRight());
root.setRight(null);
return rightTree;
}
}
}
//this is the BinaryTreeBasis class
public abstract class BinaryTreeBasis
{
protected TreeNode root;
public BinaryTreeBasis()
{
root = null;
}
public BinaryTreeBasis(Object rootItem)
{
root = new TreeNode(rootItem, null, null);
}
public boolean isEmpty()
{
return root == null;
}
public void makeEmpty()
{
root = null;
}
public Object getRootItem() throws TreeException
{
if(root == null)
{
throw new TreeException("TreeException: Empty tree");
}
else
{
return root.getItem();
}
}
}
-
You have not declared or initialized the variables "root", "left", "right". So, when your method uses these names as arguments to the constructor, the compiler is seeing these as strings and your constructor signature does not call for Strings as formal parameters for the constructor.
Define these variables and your error messages should go away.
-
OK, thanks that did fix my problem. I think I will be able to finish it from here, but then again I may have another question.
Thanks again,
Java?
Similar Threads
-
By Irina in forum ASP.NET
Replies: 0
Last Post: 11-29-2002, 10:47 PM
-
Replies: 0
Last Post: 12-13-2001, 12:06 PM
-
By Roseta in forum VB Classic
Replies: 0
Last Post: 11-14-2001, 03:24 AM
-
By Ayman in forum VB Classic
Replies: 0
Last Post: 04-03-2000, 01:08 AM
-
By Jason Bock in forum VB Classic
Replies: 0
Last Post: 03-21-2000, 06:48 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks