2010-03-02 48 views
0

我有打電話,我在main方法編碼(樹,樹節點) 一類的多個實例的問題,系統會給儘管兩棵樹顯然是輸出cdjcdj不同的樹木。 如果我是分離兩個後序()調用(每個樹已被推到堆棧之後調用)問題調用類的多個實例中的Java

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

輸出將是A B I C dĴ。

這是否意味着我的班級沒有被複制,而是在我製作新的樹時被重用?

下面是代碼:

進口java.util.Stack中;

公共類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()); 

}}對於樹節點

代碼

公共類樹節點{ 對象項目;

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; 
} } 

的樹類代碼

進口的java.util。*;

公共類樹{

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; 
} } 

回答

6

你的問題是在這裏,在Tree類:

static TreeNode root; 

您應該刪除這個詞static,並與this.root取代Tree.root

添加關鍵字static會導致變量root在程序中的Tree的所有實例之間共享,這不是您想要的。

+0

哦哇,解決了它,謝謝! – Esmond 2010-03-02 14:26:00