J'ai un problème en appelant plusieurs instance d'une classe que j'ai codé (Tree, TreeNode) dans la méthode principale, le système donnerait la sortie cdjcdj même si les deux arbres sont évidemment différents arbres. si i étaient de séparer les deux appels postorder() (chacun appelé après que l'arbre a été poussé dans la pile)Problème appelant plusieurs instances d'une classe en 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());
la sortie serait un b i d j c. Est-ce que cela signifie que ma classe n'est pas dupliquée mais qu'elle est réutilisée lorsque je crée de nouveaux arbres?
est Ci-dessous le code:
Codeimportation java.util.Stack;
mast_score public class {
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());
}}
pour TreeNode
CodeTreeNode public class { item objet;
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; } }
pour la classe d'arbres
import java.util. *;
Arbre public class {
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; } }
oh wow qui l'a résolu, merci! – Esmond