BinaryTree Problem


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: BinaryTree Problem

  1. #1
    Join Date
    Oct 2005
    Posts
    14

    Post 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 ()

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    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.

  3. #3
    Join Date
    Oct 2005
    Posts
    14
    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();
    }
    }
    }

  4. #4
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    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.

  5. #5
    Join Date
    Oct 2005
    Posts
    14
    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

  1. Problem with Search
    By Irina in forum ASP.NET
    Replies: 0
    Last Post: 11-29-2002, 11:47 PM
  2. Replies: 0
    Last Post: 12-13-2001, 01:06 PM
  3. a problem with font and language
    By Roseta in forum VB Classic
    Replies: 0
    Last Post: 11-14-2001, 04:24 AM
  4. Arabic problem view
    By Ayman in forum VB Classic
    Replies: 0
    Last Post: 04-03-2000, 02:08 AM
  5. Problem with CryptoAPI and JCE
    By Jason Bock in forum VB Classic
    Replies: 0
    Last Post: 03-21-2000, 07: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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center