Results 1 to 2 of 2
  1. #1
    virex is offline Member
    Join Date
    Mar 2010
    Posts
    1
    Rep Power
    0

    Default Problem calling multiple instances of a class

    Hi,

    i have a problem calling multiple instance of a class that i have coded (Tree, TreeNode)
    in the main method, the system would give the output c d j c d j even though both trees are obviously different trees.
    if i were to separate both postOrder() calls(each called after the tree has been pushed in to the stack)
    Java Code:
            Stack<Tree> alphaStack = new Stack<Tree>();
            TreeNode a = new TreeNode('i');
            Tree tree = new Tree(a);
            TreeNode newleft = new TreeNode('a');
            TreeNode newright = new TreeNode('b');
            tree.setLeft(a, newleft);
            tree.setRight(a, newright);
            alphaStack.push(tree);
            Tree.postOrder(alphaStack.pop().getRoot());
    
            TreeNode b = new TreeNode('j');
            Tree newtree = new Tree(b);
            TreeNode left = new TreeNode('c');
            TreeNode right = new TreeNode('d');
            newtree.setLeft(b, left);
            newtree.setRight(b, right);
            alphaStack.push(newtree);
    
            Tree.postOrder(alphaStack.pop().getRoot());
    the output would be a b i c d j.

    Does this mean that my class is not being duplicated but instead being reused when i make new Trees?


    Below is the code:
    Java Code:
    import java.util.Stack;
    
    public class mast_score {
    
        public static void main(String[] args){
            Stack<Tree> alphaStack = new Stack<Tree>();
            TreeNode a = new TreeNode('i');
            Tree tree = new Tree(a);
            TreeNode newleft = new TreeNode('a');
            TreeNode newright = new TreeNode('b');
            tree.setLeft(a, newleft);
            tree.setRight(a, newright);
            alphaStack.push(tree);
            
            TreeNode b = new TreeNode('j');
            Tree newtree = new Tree(b);
            TreeNode left = new TreeNode('c');
            TreeNode right = new TreeNode('d');
            newtree.setLeft(b, left);
            newtree.setRight(b, right);
            alphaStack.push(newtree);
    
            Tree.postOrder(alphaStack.pop().getRoot());
            Tree.postOrder(alphaStack.pop().getRoot());
       }
    }
    code for TreeNode
    Java Code:
    public class TreeNode{
        Object item;
    
        TreeNode parent;
        TreeNode left;
        TreeNode right;
    
        public TreeNode (Object item) {
            this.item = item;
            parent = null;
            left = null;
            right = null;
        }
    
        public TreeNode getParent(TreeNode current) throws ItemNotFoundException {
            if(current == null) throw new ItemNotFoundException("No parent");
            if(current.parent == null) throw new ItemNotFoundException("This is the root");
            else return current.parent;
        }
        public TreeNode getLeft(TreeNode current) throws ItemNotFoundException {
            if(current == null) throw new ItemNotFoundException("No left or right child");
            if(current.left == null) throw new ItemNotFoundException("No left child");
            else return current.left;
        }
    
        public TreeNode getRight(TreeNode current) throws ItemNotFoundException {
            if(current == null) throw new ItemNotFoundException("No left or right child");
            if(current.right == null) throw new ItemNotFoundException("No right child");
            else return current.right;
        }
    
        public Object getElement() throws ItemNotFoundException {
            if(this.item == null) throw new ItemNotFoundException("No such node");
            else return this.item;
        }
    }
    code for Tree class
    Java Code:
    import java.util.*;
    
    public class Tree {
    
        static TreeNode root;
        int size;
    
        public Tree() {
            root = null;
        }
    
        public Tree(TreeNode root) {
            Tree.root = root;
        }
    
        public TreeNode getRoot() {
            return this.root;
        }
    
        public int getLvl(TreeNode node) {
            return node.lvlCount;
        }
    
        public void setLeft(TreeNode node, TreeNode left) {
            node.left = left;
        }
    
        public void setRight(TreeNode node, TreeNode right) {
            node.right = right;
        }
        public static void postOrder(TreeNode root) {
            if (root != null) {
    
                postOrder(root.left);
                postOrder(root.right);
                System.out.print(root.item + " ");
    
            } else {
                return;
            }
    
        }
    
        public static int getSize(TreeNode root) {
            if (root != null) {
                return 1 + getSize(root.left) + getSize(root.right);
            } else {
                return 0;
            }
        }
    
        public static boolean isEmpty(Tree Tree) {
            return Tree.root == null;
        }
    }

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    805
    Rep Power
    6

    Default

    I have not gone thru the code fully.Might be the static is the one which maintains single copy and make the code to behave like this...

    Put some comments.No one will have time to go thru all the code.

    -Regards
    Ramya
    Last edited by RamyaSivakanth; 03-02-2010 at 03:18 PM.
    Ramya:cool:

Similar Threads

  1. Problem with class instances
    By sdwinder in forum New To Java
    Replies: 7
    Last Post: 10-21-2009, 01:25 AM
  2. calling action class multiple times
    By sindhu_shiva in forum Web Frameworks
    Replies: 0
    Last Post: 08-07-2009, 02:44 PM
  3. problem calling function from class to class
    By alin_ms in forum New To Java
    Replies: 3
    Last Post: 12-19-2008, 07:35 PM
  4. Calling a method for all instances of an object
    By rattle in forum New To Java
    Replies: 4
    Last Post: 04-30-2008, 02:10 PM
  5. Problem calling another class
    By adlb1300 in forum New To Java
    Replies: 3
    Last Post: 10-25-2007, 02:05 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
  •